source: trunk/indexers/lucene-gs/src/org/greenstone/LuceneWrapper/LuceneQueryResult.java@ 13686

Last change on this file since 13686 was 13686, checked in by kjdon, 17 years ago

package has changed to org.greenstone.LuceneWrapper to be consistent with other indexer packages

  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/**********************************************************************
2 *
3 * LuceneQueryResult.java
4 *
5 * Copyright 2007 The New Zealand Digital Library Project
6 *
7 * A component of the Greenstone digital library software
8 * from the New Zealand Digital Library Project at the
9 * University of Waikato, New Zealand.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 *********************************************************************/
26
27package org.greenstone.LuceneWrapper;
28
29import java.util.Vector;
30
31/** a QueryResult class for a lucene search
32 *
33 */
34public class LuceneQueryResult {
35
36 public static final int NO_ERROR = 0;
37 public static final int PARSE_ERROR = 1;
38 public static final int TOO_MANY_CLAUSES_ERROR = 2;
39 public static final int IO_ERROR = 3;
40 public static final int OTHER_ERROR = 4;
41
42 /** the list of DocInfo */
43 protected Vector docs_=null;
44 /** the list of TermInfo */
45 protected Vector terms_=null;
46 /** the list of stopwords found in the query */
47 protected Vector stopwords_ = null;
48 /** the total number of docs found - not necessarily the size of docs_*/
49 protected int total_num_docs_=0;
50 /** the start result number if we are retrieving only a portion of the results */
51 protected int start_results_ = 0;
52 /** the end result number if we are retrieving only a portion of the results */
53 protected int end_results_ = 0;
54 /** whether an error has occurred and what kind it is*/
55 protected int error_ = NO_ERROR;
56
57 LuceneQueryResult() {
58 docs_ = new Vector();
59 terms_ = new Vector();
60 stopwords_ = new Vector();
61 }
62
63 /** clear the info from the last query - should be called before setting any new docs/terms */
64 public void clear() {
65 total_num_docs_=0;
66 docs_.clear();
67 terms_.clear();
68 stopwords_.clear();
69 error_ = NO_ERROR;
70 }
71
72 /** returns the result as a String - useful for printing out results */
73 public String toString() {
74
75 String result = "";
76 result += "docs (ranks): ";
77 for (int i=0; i<docs_.size(); i++) {
78 result += ((DocInfo)docs_.elementAt(i)).toString()+", ";
79 }
80 result += "\nterms: ";
81 for (int i=0; i<terms_.size(); i++) {
82 result += ((TermInfo)terms_.elementAt(i)).toString()+", ";
83 }
84 result += "\nactual number of docs found = "+total_num_docs_;
85
86 return result;
87 }
88 /** a shorter representation - just terms and total docs - not the
89 individual docnums and ranks */
90 public String toShortString() {
91 String result = "";
92 result += "\nterms: ";
93 for (int i=0; i<terms_.size(); i++) {
94 result += ((TermInfo)terms_.elementAt(i)).toString()+", ";
95 }
96 result += "\nactual number of docs found = "+total_num_docs_;
97 return result;
98 }
99
100 public void setTotalDocs(int num) {
101 total_num_docs_=num;
102 }
103
104 public void setStartResults(int start) {
105 start_results_ = start;
106 }
107
108 public void setEndResults(int end) {
109 end_results_ = end;
110 }
111
112 public void addDoc(long doc, float rank) {
113 DocInfo doc_info = new DocInfo(doc, rank);
114 docs_.add(doc_info);
115 }
116
117 public void addTerm(String term, String field, int match, int freq) {
118 TermInfo ti = new TermInfo();
119 ti.term_=term;
120 ti.field_=field;
121 ti.match_docs_=match;
122 ti.term_freq_=freq;
123 terms_.add(ti);
124 }
125 public void addStopWord(String stopword) {
126 stopwords_.add(stopword);
127 }
128 public Vector getDocs() {
129 return docs_;
130 }
131
132 public int getError() {
133 return error_;
134 }
135
136 public String getErrorString() {
137 if (error_ == PARSE_ERROR) {
138 return "PARSE_EXCEPTION";
139 }
140 if (error_ == TOO_MANY_CLAUSES_ERROR) {
141 return "TOO_MANY_CLAUSES";
142 }
143 if (error_ == IO_ERROR) {
144 return "IO_ERROR";
145 }
146 if (error_ == NO_ERROR) {
147 return "NO_ERROR";
148 }
149 return "UNKNOWN";
150 }
151
152 public Vector getTerms() {
153 return terms_;
154 }
155
156 public Vector getStopWords() {
157 return stopwords_;
158 }
159 public int getTotalDocs() {
160 return total_num_docs_;
161 }
162
163 public void setError(int error) {
164 error_ = error;
165 }
166
167 public String getXMLString() {
168 StringBuffer buffer = new StringBuffer();
169
170 // terms
171 buffer.append("<QueryTermsInfo num=\"" + terms_.size() + "\"/>\n");
172 for (int i=0; i<terms_.size(); i++) {
173 buffer.append(((TermInfo)terms_.elementAt(i)).toXMLString()+"\n");
174 }
175
176 // stopwords
177 for (int i=0; i<stopwords_.size(); i++) {
178 buffer.append("<StopWord value=\"" + (String)stopwords_.elementAt(i)+"\" />\n");
179 }
180
181 // results
182 buffer.append("<MatchingDocsInfo num=\"" + total_num_docs_ + "\"/>\n");
183 buffer.append("<StartResults num=\"" + start_results_ + "\"/>\n");
184 buffer.append("<EndResults num=\"" + end_results_ + "\"/>\n");
185
186 for (int i=0; i< docs_.size(); i++) {
187 buffer.append(((DocInfo)docs_.elementAt(i)).toXMLString()+"\n");
188 }
189
190 return buffer.toString();
191 }
192
193
194 public class TermInfo {
195
196 /** the term itself */
197 public String term_=null;
198 /** the field for which this term was queried */
199 public String field_=null;
200 /** the number of documents containing this term */
201 public int match_docs_=0;
202 /** overall term freq for this term */
203 public int term_freq_=0;
204
205 public TermInfo() {
206 }
207
208 /** output the class as a string */
209 public String toString() {
210 String result="";
211 result +="<"+field_+">\""+term_+" docs("+match_docs_;
212 result +=")freq("+term_freq_+")";
213 return result;
214 }
215
216 /** output as an XML element */
217 public String toXMLString() {
218 return "<Term value=\"" + xmlSafe(term_) + "\" field=\"" + field_ + "\" freq=\"" + term_freq_ + "\" />";
219 }
220 }
221
222 public class DocInfo {
223
224 public long num_=0;
225 public float rank_=0;
226
227 public DocInfo(long doc, float rank) {
228 num_=doc;
229 rank_=rank;
230 }
231 public String toString(){
232 return ""+num_+"("+rank_+")";
233 }
234 public String toXMLString() {
235 return "<Match id=\"" + num_ + "\" rank=\"" + rank_ +"\" />";
236 }
237 }
238
239 // where should this go???
240
241 public static String xmlSafe(String text) {
242 text = text.replaceAll("&","&amp;amp;");
243 text = text.replaceAll("<","&amp;lt;");
244 text = text.replaceAll(">","&amp;gt;");
245 text = text.replaceAll("'","&amp;#039;");
246 text = text.replaceAll("\\\"","&amp;quot;");
247 return text;
248 }
249
250}
Note: See TracBrowser for help on using the repository browser.