1 | /*
|
---|
2 | * NzdlQuery.java
|
---|
3 | * Copyright (C) 2001 New Zealand Digital Library Project
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 |
|
---|
20 | //the package we're in
|
---|
21 | package org.nzdl.gsdl.service;
|
---|
22 |
|
---|
23 | import java.util.HashMap;
|
---|
24 | import java.util.Map;
|
---|
25 | import java.util.Set;
|
---|
26 | import java.util.Collection;
|
---|
27 |
|
---|
28 | import org.nzdl.gsdl.util.NzdlConstants;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * NzdlQuery is an object that holds the options for a query and is
|
---|
32 | * easily configured by the user.
|
---|
33 | *
|
---|
34 | * @author Stuart Yeates ([email protected])
|
---|
35 | * @author Aziz Mahoui ([email protected])
|
---|
36 | * @author Gordon Paynter ([email protected])
|
---|
37 | * @author Brett Sheeran ([email protected]) (comments)
|
---|
38 | * @version $Revision: 2688 $
|
---|
39 | */
|
---|
40 |
|
---|
41 | public class NzdlQuery extends java.lang.Object
|
---|
42 | implements java.io.Serializable{
|
---|
43 |
|
---|
44 |
|
---|
45 | private Map m_Options = null;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Creates an instance of NzdlQuery with an empty query string.
|
---|
49 | * This can then be used as a constructor parameter when creating a
|
---|
50 | * {@link NzdlRequest NzdlRequest} object for servicing by a
|
---|
51 | * {@link NzdlService NzdlService} object.
|
---|
52 | * Default values for a NzdlQuery object are: maxDocs 200, startResults= 1,
|
---|
53 | * endResults= 10, queryType= "ranked", caseFolding= true, stemming= false,
|
---|
54 | * queryTerm= "".
|
---|
55 | */
|
---|
56 | public NzdlQuery() {
|
---|
57 | m_Options = new HashMap();
|
---|
58 | setQueryTerm ( NzdlConstants.DEFAULT_QUERY_TERM );
|
---|
59 | setQueryType ( NzdlConstants.DEFAULT_QUERY_TYPE );
|
---|
60 | setCaseFolding ( NzdlConstants.DEFAULT_CASE_FOLDING );
|
---|
61 | setStemming ( NzdlConstants.DEFAULT_STEMMING );
|
---|
62 | setStartResults( NzdlConstants.DEFAULT_START_RESULTS );
|
---|
63 | setEndResults ( NzdlConstants.DEFAULT_END_RESULTS );
|
---|
64 | setMaxDocs ( NzdlConstants.DEFAULT_MAX_DOCS );
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Creates an instance of NzdlQuery with a value for query string. Has the
|
---|
69 | * same default values as NzdlQuery(), with the exception of term which is
|
---|
70 | * loaded into the query string field.
|
---|
71 | * @param term the query string
|
---|
72 | */
|
---|
73 | public NzdlQuery( String _term ) {
|
---|
74 | m_Options = new HashMap();
|
---|
75 | setQueryTerm ( _term );
|
---|
76 | setQueryType ( NzdlConstants.DEFAULT_QUERY_TYPE );
|
---|
77 | setCaseFolding ( NzdlConstants.DEFAULT_CASE_FOLDING );
|
---|
78 | setStemming ( NzdlConstants.DEFAULT_STEMMING );
|
---|
79 | setStartResults( NzdlConstants.DEFAULT_START_RESULTS );
|
---|
80 | setEndResults ( NzdlConstants.DEFAULT_END_RESULTS );
|
---|
81 | setMaxDocs ( NzdlConstants.DEFAULT_MAX_DOCS );
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Sets the expression to be queried. Note: this can also be done using the
|
---|
86 | * constructor.
|
---|
87 | * @param term the query expression string
|
---|
88 | */
|
---|
89 | public void setQueryTerm(String _term) {
|
---|
90 | m_Options.put("Term", _term);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Sets the query type as either "ranked" or "boolean". "ranked"
|
---|
95 | * orders results by suitability. "boolean" allows use of operators such as
|
---|
96 | * !, &, |. Default is "ranked"
|
---|
97 | * @param type "ranked" or "boolean"
|
---|
98 | */
|
---|
99 | public void setQueryType(String _type) {
|
---|
100 | m_Options.put("QueryType", _type);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Sets query to ignore case. Default is true.
|
---|
105 | * @param case if false then sets query to be case sensitive. If true
|
---|
106 | * then sets query to be case insenstive.
|
---|
107 | *
|
---|
108 | * removed the string version - surely we only need a boolean method here
|
---|
109 | * Dave
|
---|
110 | *
|
---|
111 | public void setCaseFolding(String _case) {
|
---|
112 | m_Options.put("CaseFold", _case);
|
---|
113 | }
|
---|
114 | */
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Sets query to ignore case. Default is true.
|
---|
118 | * @param case if false then sets query to be case sensitive. If true
|
---|
119 | * then sets query to be case insenstive.
|
---|
120 | */
|
---|
121 |
|
---|
122 | public void setCaseFolding(boolean _case) {
|
---|
123 | if (_case == false)
|
---|
124 | m_Options.put("CaseFold", "false");
|
---|
125 | else
|
---|
126 | m_Options.put("CaseFold", "true");
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Sets query to ignore word endings. Default is "false."
|
---|
131 | * @param stem if "true", sets query to strip endings such as "...ing",
|
---|
132 | * "...ed". If "false", sets query to only match whole words.
|
---|
133 | *
|
---|
134 | public void setStemming(String _stem) {
|
---|
135 | m_Options.put("Stem", _stem);
|
---|
136 | }
|
---|
137 | *
|
---|
138 | * removed the string version - surely we only need a boolean method here
|
---|
139 | * Dave
|
---|
140 | */
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Sets query to ignore word endings. Default is "false."
|
---|
144 | * @param stem if "true", sets query to strip endings such as "...ing",
|
---|
145 | * "...ed". If "false", sets query to only match whole words.
|
---|
146 | */
|
---|
147 | public void setStemming(boolean _stem) {
|
---|
148 | if (_stem == true)
|
---|
149 | m_Options.put("Stem", "true");
|
---|
150 | else
|
---|
151 | m_Options.put("Stem", "false");
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Sets the maximum number of documents that can be found by a query.
|
---|
156 | * Default is 200.
|
---|
157 | * @param max The maximum permitted number of documents to be found
|
---|
158 | * by the query
|
---|
159 | */
|
---|
160 | public void setMaxDocs(int _max) {
|
---|
161 | m_Options.put("Maxdocs", new Integer(_max));
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Sets the start number of the result set. The result set is a subset
|
---|
166 | * of the maximum number of documents that will be found by the query.
|
---|
167 | * Default is 1.
|
---|
168 | * @param start the number of the first document, relative to the found
|
---|
169 | * documents
|
---|
170 | */
|
---|
171 | public void setStartResults(int _start) {
|
---|
172 | m_Options.put("StartResults", new Integer(_start));
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Sets the end number of the result set. The result set is a subset of the
|
---|
177 | * maximum number documents that could be found by that query.
|
---|
178 | * Default is 10.
|
---|
179 | * @param end the number of the last document relative to the found
|
---|
180 | * documents
|
---|
181 | */
|
---|
182 | public void setEndResults(int _end) {
|
---|
183 | m_Options.put("EndResults", new Integer(_end));
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Returns whether query is "ranked" or "boolean". "ranked" orders results by
|
---|
188 | * suitability. "boolean" allows use of operators such as !, &, |. Default is
|
---|
189 | * "ranked"
|
---|
190 | * @return Either "ranked" or "boolean"
|
---|
191 | */
|
---|
192 | public String getQueryType() {
|
---|
193 | return (String)m_Options.get("QueryType");
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Returns the query string expression.
|
---|
198 | * @return the query string
|
---|
199 | */
|
---|
200 | public String getQueryTerm() {
|
---|
201 | return (String)m_Options.get("Term");
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Returns "true" if query is case insenstive. Default is "true"
|
---|
206 | * @return "true" if query is case insenstive, "false" if the query is case
|
---|
207 | * senstive.
|
---|
208 | */
|
---|
209 | public String getCaseFolding() {
|
---|
210 | return (String)m_Options.get("CaseFold");
|
---|
211 | }
|
---|
212 |
|
---|
213 | public boolean isCaseFolding() {
|
---|
214 | return ((String)m_Options.get("CaseFold") == "true");
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Returns "true" if query ignores word endings. Default is "false".
|
---|
219 | * @return "true" if query strips endings such as "...ing" or "...ed" , false
|
---|
220 | * if query only matches whole words.
|
---|
221 | */
|
---|
222 | public String getStemming() {
|
---|
223 | return (String)m_Options.get("Stem");
|
---|
224 | }
|
---|
225 |
|
---|
226 | public boolean isStemming() {
|
---|
227 | return ((String)m_Options.get("Stem") == "true");
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Returns the current setting for the maximum number of documents that
|
---|
233 | * may be found by a query. Default is 200.
|
---|
234 | * @return The maximum number of documents to be found
|
---|
235 | */
|
---|
236 | public int getMaxDocs() {
|
---|
237 | return ((Integer) m_Options.get("Maxdocs")).intValue();
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Returns the start number of the result set. The result set is a subset of
|
---|
242 | * the maximum number of documents that could be found by the query.
|
---|
243 | * Default is 1.
|
---|
244 | * @return The number of the first document relative to the documents that
|
---|
245 | * were found to match the query
|
---|
246 | */
|
---|
247 | public int getStartResults() {
|
---|
248 | return ((Integer) m_Options.get("StartResults")).intValue();
|
---|
249 | }
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Returns the end number of the result set. The result set is a subset of
|
---|
253 | * the maximum number documents that could be found by the query.
|
---|
254 | * Default is 10.
|
---|
255 | * @return The number of the last document relative to the documents that
|
---|
256 | * were found to match the query.
|
---|
257 | */
|
---|
258 | public int getEndResults() {
|
---|
259 | return ((Integer) m_Options.get("EndResults")).intValue();
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Returns the set of query terms.
|
---|
264 | * @return This set: "EndResults", "QueryType", "Term", "MaxDocs",
|
---|
265 | * "StartResults", "Stem", "CaseFold"
|
---|
266 | */
|
---|
267 | public Set queryKeySet() {
|
---|
268 | return m_Options.keySet();
|
---|
269 | }
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Returns a collection of the current query values.
|
---|
273 | * @return For a default query this collection: 10, "ranked", "", 200, 1
|
---|
274 | * , false, true
|
---|
275 | */
|
---|
276 | public Collection queryValues() {
|
---|
277 | return m_Options.values();
|
---|
278 | }
|
---|
279 |
|
---|
280 | }
|
---|
281 |
|
---|