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