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

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

a results class for when we are doing queries through Java rather than via the command line

  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 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.nzdl.gsdl.LuceneWrap;
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
41 /** the list of DocInfo */
42 protected Vector docs_=null;
43 /** the list of TermInfo */
44 protected Vector terms_=null;
45 /** the list of stopwords found in the query */
46 protected Vector stopwords_ = null;
47 /** the total number of docs found - not necessarily the size of docs_*/
48 protected int total_num_docs_=0;
49 /** the start result number if we are retrieving only a portion of the results */
50 protected int start_results_ = 0;
51 /** the end result number if we are retrieving only a portion of the results */
52 protected int end_results_ = 0;
53 /** whether an error has occurred and what kind it is*/
54 protected int error_ = NO_ERROR;
55
56 LuceneQueryResult() {
57 docs_ = new Vector();
58 terms_ = new Vector();
59 stopwords_ = new Vector();
60 }
61
62 /** clear the info from the last query - should be called before setting any new docs/terms */
63 public void clear() {
64 total_num_docs_=0;
65 docs_.clear();
66 terms_.clear();
67 stopwords_.clear();
68 error_ = NO_ERROR;
69 }
70
71 /** returns the result as a String - useful for printing out results */
72 public String toString() {
73
74 String result = "";
75 result += "docs (ranks): ";
76 for (int i=0; i<docs_.size(); i++) {
77 result += ((DocInfo)docs_.elementAt(i)).toString()+", ";
78 }
79 result += "\nterms: ";
80 for (int i=0; i<terms_.size(); i++) {
81 result += ((TermInfo)terms_.elementAt(i)).toString()+", ";
82 }
83 result += "\nactual number of docs found = "+total_num_docs_;
84
85 return result;
86 }
87 /** a shorter representation - just terms and total docs - not the
88 individual docnums and ranks */
89 public String toShortString() {
90 String result = "";
91 result += "\nterms: ";
92 for (int i=0; i<terms_.size(); i++) {
93 result += ((TermInfo)terms_.elementAt(i)).toString()+", ";
94 }
95 result += "\nactual number of docs found = "+total_num_docs_;
96 return result;
97 }
98
99 public void setTotalDocs(int num) {
100 total_num_docs_=num;
101 }
102
103 public void setStartResults(int start) {
104 start_results_ = start;
105 }
106
107 public void setEndResults(int end) {
108 end_results_ = end;
109 }
110
111 public void addDoc(long doc, float rank) {
112 DocInfo doc_info = new DocInfo(doc, rank);
113 docs_.add(doc_info);
114 }
115
116 public void addTerm(String term, String field, int match, int freq) {
117 TermInfo ti = new TermInfo();
118 ti.term_=term;
119 ti.field_=field;
120 ti.match_docs_=match;
121 ti.term_freq_=freq;
122 terms_.add(ti);
123 }
124 public void addStopWord(String stopword) {
125 stopwords_.add(stopword);
126 }
127 public Vector getDocs() {
128 return docs_;
129 }
130
131 public int getError() {
132 return error_;
133 }
134
135 public String getErrorString() {
136 if (error_ == PARSE_ERROR) {
137 return "PARSE_EXCEPTION";
138 }
139 if (error_ == TOO_MANY_CLAUSES_ERROR) {
140 return "TOO_MANY_CLAUSES";
141 }
142 if (error_ == IO_ERROR) {
143 return "IO_ERROR";
144 }
145 if (error_ == NO_ERROR) {
146 return "NO_ERROR";
147 }
148 return "UNKNOWN";
149 }
150
151 public Vector getTerms() {
152 return terms_;
153 }
154
155 public Vector getStopWords() {
156 return stopwords_;
157 }
158 public int getTotalDocs() {
159 return total_num_docs_;
160 }
161
162 public void setError(int error) {
163 error_ = error;
164 }
165
166 public String getXMLString() {
167 StringBuffer buffer = new StringBuffer();
168
169 // terms
170 buffer.append("<QueryTermsInfo num=\"" + terms_.size() + "\"/>\n");
171 for (int i=0; i<terms_.size(); i++) {
172 buffer.append(((TermInfo)terms_.elementAt(i)).toXMLString()+"\n");
173 }
174
175 // stopwords
176 for (int i=0; i<stopwords_.size(); i++) {
177 buffer.append("<StopWord value=\"" + (String)stopwords_.elementAt(i)+"\" />\n");
178 }
179
180 // results
181 buffer.append("<MatchingDocsInfo num=\"" + total_num_docs_ + "\"/>\n");
182 buffer.append("<StartResults num=\"" + start_results_ + "\"/>\n");
183 buffer.append("<EndResults num=\"" + end_results_ + "\"/>\n");
184
185 for (int i=0; i< docs_.size(); i++) {
186 buffer.append(((DocInfo)docs_.elementAt(i)).toXMLString()+"\n");
187 }
188
189 return buffer.toString();
190 }
191
192
193 public class TermInfo {
194
195 /** the term itself */
196 public String term_=null;
197 /** the field for which this term was queried */
198 public String field_=null;
199 /** the number of documents containing this term */
200 public int match_docs_=0;
201 /** overall term freq for this term */
202 public int term_freq_=0;
203
204 public TermInfo() {
205 }
206
207 /** output the class as a string */
208 public String toString() {
209 String result="";
210 result +="<"+field_+">\""+term_+" docs("+match_docs_;
211 result +=")freq("+term_freq_+")";
212 return result;
213 }
214
215 /** output as an XML element */
216 public String toXMLString() {
217 return "<Term value=\"" + xmlSafe(term_) + "\" field=\"" + field_ + "\" freq=\"" + term_freq_ + "\" />";
218 }
219 }
220
221 public class DocInfo {
222
223 public long num_=0;
224 public float rank_=0;
225
226 public DocInfo(long doc, float rank) {
227 num_=doc;
228 rank_=rank;
229 }
230 public String toString(){
231 return ""+num_+"("+rank_+")";
232 }
233 public String toXMLString() {
234 return "<Match id=\"" + num_ + "\" rank=\"" + rank_ +"\" />";
235 }
236 }
237
238 // where should this go???
239
240 public static String xmlSafe(String text) {
241 text = text.replaceAll("&","&amp;amp;");
242 text = text.replaceAll("<","&amp;lt;");
243 text = text.replaceAll(">","&amp;gt;");
244 text = text.replaceAll("'","&amp;#039;");
245 text = text.replaceAll("\\\"","&amp;quot;");
246 return text;
247 }
248
249}
Note: See TracBrowser for help on using the repository browser.