source: trunk/java-client/org/nzdl/gsdl/service/NzdlQuery.java@ 3518

Last change on this file since 3518 was 3518, checked in by cs025, 21 years ago

Added proper handling of Booleans in getMetadata

  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 KB
Line 
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
21package org.nzdl.gsdl.service;
22
23import java.util.HashMap;
24import java.util.Map;
25import java.util.Set;
26import java.util.Collection;
27
28import 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: 3518 $
39 */
40
41public 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 setMetadata ( NzdlConstants.DEFAULT_QUERY_META);
66 setIndex (new String(""));
67 }
68
69 /**
70 * Creates an instance of NzdlQuery with a value for query string. Has the
71 * same default values as NzdlQuery(), with the exception of term which is
72 * loaded into the query string field.
73 * @param term the query string
74 */
75 public NzdlQuery( String _term ) {
76 m_Options = new HashMap();
77 setQueryTerm ( _term );
78 setQueryType ( NzdlConstants.DEFAULT_QUERY_TYPE );
79 setCaseFolding ( NzdlConstants.DEFAULT_CASE_FOLDING );
80 setStemming ( NzdlConstants.DEFAULT_STEMMING );
81 setStartResults( NzdlConstants.DEFAULT_START_RESULTS );
82 setEndResults ( NzdlConstants.DEFAULT_END_RESULTS );
83 setMaxDocs ( NzdlConstants.DEFAULT_MAX_DOCS );
84 setIndex (new String(""));
85 }
86
87 /** sets index to be queried.
88 */
89 public void setIndex(String _index) {
90 m_Options.put("Index", _index);
91 }
92
93
94 /**
95 * Sets the expression to be queried. Note: this can also be done using the
96 * constructor.
97 * @param term the query expression string
98 */
99 public void setQueryTerm(String _term) {
100 m_Options.put("Term", _term);
101 }
102
103 /**
104 * Sets the query type as either "ranked" or "boolean". "ranked"
105 * orders results by suitability. "boolean" allows use of operators such as
106 * !, &, |. Default is "ranked"
107 * @param type "ranked" or "boolean"
108 */
109 public void setQueryType(String _type) {
110 m_Options.put("QueryType", _type);
111 }
112
113 /**
114 * Sets query to ignore case. Default is true.
115 * @param case if false then sets query to be case sensitive. If true
116 * then sets query to be case insenstive.
117 *
118 * removed the string version - surely we only need a boolean method here
119 * Dave
120 *
121 public void setCaseFolding(String _case) {
122 m_Options.put("CaseFold", _case);
123 }
124 */
125
126 /**
127 * Sets query to ignore case. Default is true.
128 * @param case if false then sets query to be case sensitive. If true
129 * then sets query to be case insenstive.
130 */
131
132 public void setCaseFolding(boolean _case) {
133 if (_case == false)
134 m_Options.put("CaseFold", "false");
135 else
136 m_Options.put("CaseFold", "true");
137 }
138
139 /**
140 * Sets query to ignore word endings. Default is "false."
141 * @param stem if "true", sets query to strip endings such as "...ing",
142 * "...ed". If "false", sets query to only match whole words.
143 *
144 public void setStemming(String _stem) {
145 m_Options.put("Stem", _stem);
146 }
147 *
148 * removed the string version - surely we only need a boolean method here
149 * Dave
150 */
151
152 /**
153 * Sets query to ignore word endings. Default is "false."
154 * @param stem if "true", sets query to strip endings such as "...ing",
155 * "...ed". If "false", sets query to only match whole words.
156 */
157 public void setStemming(boolean _stem) {
158 if (_stem == true)
159 m_Options.put("Stem", "true");
160 else
161 m_Options.put("Stem", "false");
162 }
163
164 /**
165 * Sets the maximum number of documents that can be found by a query.
166 * Default is 200.
167 * @param max The maximum permitted number of documents to be found
168 * by the query
169 */
170 public void setMaxDocs(int _max) {
171 m_Options.put("Maxdocs", new Integer(_max));
172 }
173
174 /**
175 * Sets the start number of the result set. The result set is a subset
176 * of the maximum number of documents that will be found by the query.
177 * Default is 1.
178 * @param start the number of the first document, relative to the found
179 * documents
180 */
181 public void setStartResults(int _start) {
182 m_Options.put("StartResults", new Integer(_start));
183 }
184
185 /**
186 * Sets the end number of the result set. The result set is a subset of the
187 * maximum number documents that could be found by that query.
188 * Default is 10.
189 * @param end the number of the last document relative to the found
190 * documents
191 */
192 public void setEndResults(int _end) {
193 m_Options.put("EndResults", new Integer(_end));
194 }
195
196 /**
197 * Set the retrieval of extended Metadata information in the result set.
198 * The default is off (false)
199 * @param whether to retrieve metadata.
200 */
201 public void setMetadata(boolean _set)
202 { m_Options.put("Metadata", new Boolean(_set));
203 }
204
205 /**
206 * Returns whether query is "ranked" or "boolean". "ranked" orders results by
207 * suitability. "boolean" allows use of operators such as !, &, |. Default is
208 * "ranked"
209 * @return Either "ranked" or "boolean"
210 */
211 public String getQueryType() {
212 return (String)m_Options.get("QueryType");
213 }
214
215 /**
216 * Returns the query string expression.
217 * @return the query string
218 */
219 public String getQueryTerm() {
220 return (String)m_Options.get("Term");
221 }
222 /** Returns the index to be queried
223 * @return the index
224 */
225 public String getIndex() {
226 return (String)m_Options.get("Index");
227 }
228
229 /**
230 * Returns "true" if query is case insenstive. Default is "true"
231 * @return "true" if query is case insenstive, "false" if the query is case
232 * senstive.
233 */
234 public String getCaseFolding() {
235 return (String)m_Options.get("CaseFold");
236 }
237
238 public boolean isCaseFolding() {
239 return ((String)m_Options.get("CaseFold") == "true");
240 }
241
242 /**
243 * Returns "true" if query ignores word endings. Default is "false".
244 * @return "true" if query strips endings such as "...ing" or "...ed" , false
245 * if query only matches whole words.
246 */
247 public String getStemming() {
248 return (String)m_Options.get("Stem");
249 }
250
251 public boolean isStemming() {
252 return ((String)m_Options.get("Stem") == "true");
253 }
254
255
256 /**
257 * Returns the current setting for the maximum number of documents that
258 * may be found by a query. Default is 200.
259 * @return The maximum number of documents to be found
260 */
261 public int getMaxDocs() {
262 return ((Integer) m_Options.get("Maxdocs")).intValue();
263 }
264
265 /**
266 * Returns the start number of the result set. The result set is a subset of
267 * the maximum number of documents that could be found by the query.
268 * Default is 1.
269 * @return The number of the first document relative to the documents that
270 * were found to match the query
271 */
272 public int getStartResults() {
273 return ((Integer) m_Options.get("StartResults")).intValue();
274 }
275
276 /**
277 * Returns the end number of the result set. The result set is a subset of
278 * the maximum number documents that could be found by the query.
279 * Default is 10.
280 * @return The number of the last document relative to the documents that
281 * were found to match the query.
282 */
283 public int getEndResults() {
284 return ((Integer) m_Options.get("EndResults")).intValue();
285 }
286
287 /**
288 * Get the retrieval of extended Metadata information in the result set.
289 * The default is off (false)
290 * @return whether to retrieve metadata.
291 */
292 public boolean getMetadata()
293 {
294 if (m_Options.get("Metadata") == null) return false;
295 return ((Boolean) m_Options.get("Metadata")).booleanValue();
296 }
297
298 /**
299 * Returns the set of query terms.
300 * @return This set: "EndResults", "QueryType", "Term", "MaxDocs",
301 * "StartResults", "Stem", "CaseFold"
302 */
303 public Set queryKeySet() {
304 return m_Options.keySet();
305 }
306
307 /**
308 * Returns a collection of the current query values.
309 * @return For a default query this collection: 10, "ranked", "", 200, 1
310 * , false, true
311 */
312 public Collection queryValues() {
313 return m_Options.values();
314 }
315
316}
317
Note: See TracBrowser for help on using the repository browser.