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 |
|
---|
27 | package org.greenstone.LuceneWrapper;
|
---|
28 |
|
---|
29 | import java.util.Vector;
|
---|
30 |
|
---|
31 | /** a QueryResult class for a lucene search
|
---|
32 | *
|
---|
33 | */
|
---|
34 | public 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(String id, float rank)
|
---|
113 | {
|
---|
114 | docs_.add(new DocInfo(id, rank));
|
---|
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 |
|
---|
223 | public class DocInfo
|
---|
224 | {
|
---|
225 | public String id_ = "";
|
---|
226 | public float rank_ = 0;
|
---|
227 |
|
---|
228 | public DocInfo (String id, float rank)
|
---|
229 | {
|
---|
230 | id_ = id;
|
---|
231 | rank_ = rank;
|
---|
232 | }
|
---|
233 |
|
---|
234 | public String toString()
|
---|
235 | {
|
---|
236 | return "" + id_ + " (" + rank_ + ")";
|
---|
237 | }
|
---|
238 |
|
---|
239 | public String toXMLString()
|
---|
240 | {
|
---|
241 | return "<Match id=\"" + id_ + "\" rank=\"" + rank_ + "\" />";
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | // where should this go???
|
---|
247 | public static String xmlSafe(String text) {
|
---|
248 | text = text.replaceAll("&","&amp;");
|
---|
249 | text = text.replaceAll("<","&lt;");
|
---|
250 | text = text.replaceAll(">","&gt;");
|
---|
251 | text = text.replaceAll("'","&#039;");
|
---|
252 | text = text.replaceAll("\\\"","&quot;");
|
---|
253 | return text;
|
---|
254 | }
|
---|
255 |
|
---|
256 | }
|
---|