source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/CrossCollectionSearch.java@ 30150

Last change on this file since 30150 was 30150, checked in by kjdon, 9 years ago

doing a simplified paging for cross coll searching. We give a maxdocs arg, and this many docs are returned per collection. Then the results are merged.

  • Property svn:keywords set to Author Date Id Revision
File size: 20.2 KB
Line 
1/*
2 * CrossCollectionSearch.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
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 */
19package org.greenstone.gsdl3.service;
20
21import java.util.ArrayList;
22import java.util.HashMap;
23import java.util.Iterator;
24import java.util.Map;
25import java.util.Set;
26
27import org.apache.log4j.Logger;
28import org.greenstone.gsdl3.util.GSPath;
29import org.greenstone.gsdl3.util.GSXML;
30import org.greenstone.gsdl3.util.UserContext;
31import org.greenstone.gsdl3.util.XMLConverter;
32
33import org.w3c.dom.Document;
34import org.w3c.dom.Element;
35import org.w3c.dom.Node;
36import org.w3c.dom.NodeList;
37
38/**
39 *
40 */
41
42public class CrossCollectionSearch extends ServiceRack
43{
44
45 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.CrossCollectionSearch.class.getName());
46 protected static final String QUERY_PARAM = "query";
47 protected static final String COLLECTION_PARAM = "collection";
48 protected static final String MAXDOCS_PARAM = "maxDocs"; // matches standard maxDocs, but in this case, means max docs per collection
49 protected static final String HITS_PER_PAGE_PARAM = "hitsPerPage";
50 protected static final String MAXDOCS_DEFAULT = "20";
51 // the services on offer - these proxy the actual collection ones
52 protected static final String TEXT_QUERY_SERVICE = "TextQuery";
53 protected static final String DOCUMENT_METADATA_RETRIEVE_SERVICE = "DocumentMetadataRetrieve";
54
55 protected String[] coll_ids_list = null;
56 protected String[] coll_ids_list_no_all = null;
57 // maps lang to coll names list
58 protected HashMap<String, String[]> coll_names_map = null;
59
60 //protected String[] coll_names_list = null;
61
62 /** constructor */
63 public CrossCollectionSearch()
64 {
65 }
66
67 public boolean configure(Element info, Element extra_info)
68 {
69 // any parameters? colls to include??
70 logger.info("Configuring CrossCollectionSearch...");
71 // query service
72 Element ccs_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
73 ccs_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
74 ccs_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
75 this.short_service_info.appendChild(ccs_service);
76
77 // metadata service
78 Element dmr_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
79 dmr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
80 dmr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
81 this.short_service_info.appendChild(dmr_service);
82
83 // get any format info
84 Element format_info = (Element) GSXML.getChildByTagName(info, GSXML.FORMAT_ELEM);
85 if (format_info != null)
86 {
87 this.format_info_map.put(TEXT_QUERY_SERVICE, this.desc_doc.importNode(format_info, true));
88 }
89 else
90 {
91 // add in a default format statement
92 String format_string = "<format xmlns:gsf='" + GSXML.GSF_NAMESPACE + "' xmlns:xsl='" + GSXML.XSL_NAMESPACE + "'><gsf:template match='documentNode'><td>poos<a><xsl:attribute name='href'>?a=d&amp;c=<xsl:value-of select='@collection'/>&amp;d=<xsl:value-of select='@nodeID'/><xsl:if test=\"@nodeType='leaf'\">&amp;sib=1</xsl:if>&amp;dt=<xsl:value-of select='@docType'/>&amp;p.a=q&amp;p.s=" + TEXT_QUERY_SERVICE + "&amp;p.c=";
93 if (this.cluster_name != null)
94 {
95 format_string += this.cluster_name;
96 }
97 format_string += "</xsl:attribute><gsf:icon/></a></td><td><gsf:metadata name='Title'/> (<xsl:value-of select='@collection'/>) </td></gsf:template></format>";
98 this.format_info_map.put(TEXT_QUERY_SERVICE, this.desc_doc.importNode(this.converter.getDOM(format_string).getDocumentElement(), true));
99 }
100 return true;
101 }
102
103 protected Element getServiceDescription(Document doc, String service, String lang, String subset)
104 {
105 if (service.equals(TEXT_QUERY_SERVICE))
106 {
107
108 Element ccs_service = doc.createElement(GSXML.SERVICE_ELEM);
109 ccs_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
110 ccs_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
111
112 // display info
113 if (subset == null || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER))
114 {
115 ccs_service.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_NAME, getTextString(TEXT_QUERY_SERVICE + ".name", lang)));
116 ccs_service.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_SUBMIT, getTextString(TEXT_QUERY_SERVICE + ".submit", lang)));
117 ccs_service.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getTextString(TEXT_QUERY_SERVICE + ".description", lang)));
118 }
119 // param info
120 if (subset == null || subset.equals(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER))
121 {
122 Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
123 // collection list
124 if (coll_ids_list == null)
125 {
126 initCollectionList(lang);
127 }
128 if (!coll_names_map.containsKey(lang))
129 {
130 addCollectionNames(lang);
131 }
132 Element param = GSXML.createParameterDescription(doc, COLLECTION_PARAM, getTextString("param." + COLLECTION_PARAM, lang), GSXML.PARAM_TYPE_ENUM_MULTI, "all", coll_ids_list, coll_names_map.get(lang));
133 param_list.appendChild(param);
134 // max docs param
135 param = GSXML.createParameterDescription(doc, MAXDOCS_PARAM, getTextString("param." + MAXDOCS_PARAM, lang), GSXML.PARAM_TYPE_INTEGER, MAXDOCS_DEFAULT, null, null);
136 param_list.appendChild(param);
137 // query param
138 param = GSXML.createParameterDescription(doc, QUERY_PARAM, getTextString("param." + QUERY_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
139 param_list.appendChild(param);
140 ccs_service.appendChild(param_list);
141 }
142
143 logger.debug("service description=" + this.converter.getPrettyString(ccs_service));
144 return ccs_service;
145 }
146 // these ones are probably never called, but put them here just in case
147 Element service_elem = doc.createElement(GSXML.SERVICE_ELEM);
148 service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
149 service_elem.setAttribute(GSXML.NAME_ATT, service);
150 return service_elem;
151
152 }
153
154 protected Element processTextQuery(Element request)
155 {
156 // Create a new (empty) result message
157 Document result_doc = XMLConverter.newDOM();
158 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
159 result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE);
160 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
161
162 UserContext userContext = new UserContext(request);
163
164 // Get the parameters of the request
165 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
166 if (param_list == null)
167 {
168 logger.error("TextQuery request had no paramList.");
169 return result; // Return the empty result
170 }
171
172 // get the collection list
173 String[] colls_list = coll_ids_list_no_all;
174 Element coll_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, COLLECTION_PARAM);
175 if (coll_param != null)
176 {
177 String coll_list = GSXML.getValue(coll_param);
178 if (!coll_list.equals("all") && !coll_list.equals(""))
179 {
180 colls_list = coll_list.split(",");
181 }
182 }
183
184 String maxdocs = MAXDOCS_DEFAULT;
185 Element maxdocs_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, MAXDOCS_PARAM);
186 if (maxdocs_param != null) {
187 maxdocs = GSXML.getValue(maxdocs_param);
188 }
189
190 Document msg_doc = XMLConverter.newDOM();
191 Element query_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
192 // we are sending the same request to each collection - build up the to
193 // attribute for the request
194 StringBuffer to_att = new StringBuffer();
195 for (int i = 0; i < colls_list.length; i++)
196 {
197 if (i > 0)
198 {
199 to_att.append(",");
200 }
201 to_att.append(GSPath.appendLink(colls_list[i], "TextQuery"));
202
203 }
204 // send the query to all colls
205 Element query_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_PROCESS, to_att.toString(), userContext);
206 query_message.appendChild(query_request);
207 // should we add params individually?
208 Element new_param_list = msg_doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
209 query_request.appendChild(new_param_list);
210 new_param_list.appendChild(msg_doc.importNode(GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, QUERY_PARAM), true));
211
212 // for cross coll search, we only want maxdocs from each collection
213 // some colls use maxdocs, some use hits per page so lets send both
214 new_param_list.appendChild(GSXML.createParameter(msg_doc, MAXDOCS_PARAM, maxdocs));
215 new_param_list.appendChild(GSXML.createParameter(msg_doc, HITS_PER_PAGE_PARAM, maxdocs));
216 Element query_result = (Element) this.router.process(query_message);
217
218 // create the doc list for the response
219 Element doc_node_list = result_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
220 result.appendChild(doc_node_list);
221
222 NodeList responses = query_result.getElementsByTagName(GSXML.RESPONSE_ELEM);
223 int num_docs = 0;
224 for (int k = 0; k < responses.getLength(); k++)
225 {
226 String coll_name = GSPath.removeLastLink(((Element) responses.item(k)).getAttribute(GSXML.FROM_ATT));
227 NodeList nodes = ((Element) responses.item(k)).getElementsByTagName(GSXML.DOC_NODE_ELEM);
228 if (nodes == null || nodes.getLength() == 0)
229 continue;
230 num_docs += nodes.getLength();
231 Element last_node = null;
232 Element this_node = null;
233 for (int n = 0; n < nodes.getLength(); n++)
234 {
235 this_node = (Element) nodes.item(n);
236 this_node.setAttribute("collection", coll_name);
237 if (k == 0)
238 {
239
240 doc_node_list.appendChild(result_doc.importNode(this_node, true));
241 }
242 else
243 {
244 if (last_node == null)
245 {
246 last_node = (Element) GSXML.getChildByTagName(doc_node_list, GSXML.DOC_NODE_ELEM);
247 }
248 last_node = GSXML.insertIntoOrderedList(doc_node_list, GSXML.DOC_NODE_ELEM, last_node, this_node, "rank", true);
249 }
250
251 }
252 }
253 // just send back num docs returned. Too hard to work out number of matches etc as each index type
254 // does it differently
255 GSXML.addMetadata(metadata_list, "numDocsReturned", "" + num_docs);
256 return result;
257 }
258
259 // protected Element processAdvTextQuery(Element request)
260 // {
261
262 // }
263 protected boolean initCollectionList(String lang)
264 {
265 UserContext userContext = new UserContext();
266 userContext.setLanguage(lang);
267 userContext.setUserID("");
268
269 // first, get the message router info
270 Document msg_doc = XMLConverter.newDOM();
271 Element coll_list_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
272 Element coll_list_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext); // uid
273 coll_list_message.appendChild(coll_list_request);
274 logger.debug("coll list request = " + this.converter.getPrettyString(coll_list_request));
275 Element coll_list_response = (Element) this.router.process(coll_list_message);
276 if (coll_list_response == null)
277 {
278 logger.error("couldn't query the message router!");
279 return false;
280 }
281 logger.debug("coll list response = " + this.converter.getPrettyString(coll_list_response));
282 // second, get some info from each collection. we want the coll name
283 // and whether its got a text query service
284
285 NodeList colls = coll_list_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
286 // we can send the same request to multiple collections at once by using a comma separated list
287 Element metadata_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
288 StringBuffer colls_sb = new StringBuffer();
289 for (int i = 0; i < colls.getLength(); i++)
290 {
291 Element c = (Element) colls.item(i);
292 String name = c.getAttribute(GSXML.NAME_ATT);
293 if (i != 0)
294 {
295 colls_sb.append(",");
296 }
297 colls_sb.append(name);
298 //Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, name, userContext);
299 //metadata_message.appendChild(metadata_request);
300 }
301
302 Element metadata_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE, colls_sb.toString(), userContext);
303 metadata_message.appendChild(metadata_request);
304 logger.debug("metadata request = " + this.converter.getPrettyString(metadata_message));
305 Element metadata_response = (Element) this.router.process(metadata_message);
306 logger.debug("metadata response = " + this.converter.getPrettyString(metadata_response));
307 NodeList coll_responses = metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
308 ArrayList<String> valid_colls = new ArrayList<String>();
309 ArrayList<String> valid_coll_names = new ArrayList<String>();
310 for (int i = 0; i < coll_responses.getLength(); i++)
311 {
312 Element response = (Element) coll_responses.item(i);
313 Element coll = (Element) GSXML.getChildByTagName(response, GSXML.COLLECTION_ELEM);
314 Element service_list = (Element) GSXML.getChildByTagName(coll, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
315 if (service_list == null)
316 continue;
317 Element query_service = GSXML.getNamedElement(service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, TEXT_QUERY_SERVICE); // should be AbstractTextSearch.TEXT_QUERY_SERVICE
318 if (query_service == null)
319 continue;
320 // use the name of the response in case we are talking to a remote collection, not the name of the collection.
321 String coll_id = response.getAttribute(GSXML.FROM_ATT);
322 String coll_name = GSXML.getDisplayText(coll, GSXML.DISPLAY_TEXT_NAME, lang, "en");
323 valid_colls.add(coll_id);
324 valid_coll_names.add(coll_name);
325 }
326
327 this.coll_names_map = new HashMap<String, String[]>();
328
329 // ids no all has the list without 'all' option.
330 this.coll_ids_list_no_all = new String[1];
331 this.coll_ids_list_no_all = valid_colls.toArray(coll_ids_list_no_all);
332
333 valid_colls.add(0, "all");
334 valid_coll_names.add(0, getTextString("param." + COLLECTION_PARAM + ".all", lang));
335
336 this.coll_ids_list = new String[1];
337 this.coll_ids_list = valid_colls.toArray(coll_ids_list);
338
339 String[] coll_names_list = new String[1];
340 coll_names_list = valid_coll_names.toArray(coll_names_list);
341 this.coll_names_map.put(lang, coll_names_list);
342 return true;
343 }
344
345 protected void addCollectionNames(String lang)
346 {
347
348 UserContext userContext = new UserContext();
349 userContext.setLanguage(lang);
350 userContext.setUserID("");
351
352 ArrayList<String> coll_names = new ArrayList<String>();
353 coll_names.add(getTextString("param." + COLLECTION_PARAM + ".all", lang));
354
355 // need to request MR for collection descriptions
356 Document msg_doc = XMLConverter.newDOM();
357 Element metadata_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
358
359 // get a comma separated list of coll ids to send to MR
360 // the first item is the place holder for 'all'
361 StringBuffer colls_sb = new StringBuffer();
362 for (int i = 1; i < coll_ids_list.length; i++)
363 {
364 if (i != 1)
365 {
366 colls_sb.append(",");
367 }
368 colls_sb.append(coll_ids_list[i]);
369 }
370 Element metadata_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE, colls_sb.toString(), userContext);
371 // param_list to request just displayTextList
372 Element param_list = msg_doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
373 Element param = GSXML.createParameter(msg_doc, GSXML.SUBSET_PARAM, GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
374 param_list.appendChild(param);
375 metadata_request.appendChild(param_list);
376 metadata_message.appendChild(metadata_request);
377 logger.debug("coll names metadata request = " + this.converter.getPrettyString(metadata_message));
378 Element metadata_response = (Element) this.router.process(metadata_message);
379 logger.debug("coll names metadata response = " + this.converter.getPrettyString(metadata_response));
380 NodeList coll_responses = metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
381 for (int i = 0; i < coll_responses.getLength(); i++)
382 {
383 Element response = (Element) coll_responses.item(i);
384 Element coll = (Element) GSXML.getChildByTagName(response, GSXML.COLLECTION_ELEM);
385 String coll_name = GSXML.getDisplayText(coll, GSXML.DISPLAY_TEXT_NAME, lang, "en");
386 coll_names.add(coll_name);
387 }
388
389 String[] coll_names_list = new String[1];
390 coll_names_list = coll_names.toArray(coll_names_list);
391 this.coll_names_map.put(lang, coll_names_list);
392
393 }
394
395 protected Element processDocumentMetadataRetrieve(Element request)
396 {
397 // Create a new (empty) result message
398 Document result_doc = XMLConverter.newDOM();
399 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
400 result.setAttribute(GSXML.FROM_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
401 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
402
403 UserContext userContext = new UserContext(request);
404 // Get the parameters of the request
405 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
406 if (param_list == null)
407 {
408 logger.error("DocumentMetadataRetrieve request had no paramList.");
409 return result; // Return the empty result
410 }
411
412 NodeList query_doc_list = request.getElementsByTagName(GSXML.DOC_NODE_ELEM);
413 if (query_doc_list.getLength() == 0)
414 {
415 logger.error("DocumentMetadataRetrieve request had no documentNodes.");
416 return result; // Return the empty result
417 }
418
419 // the resulting doc node list
420 Element result_node_list = result_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
421 result.appendChild(result_node_list);
422
423
424 // organise the nodes into collection lists
425 HashMap<String, Node> coll_map = new HashMap<String, Node>();
426
427 for (int i = 0; i < query_doc_list.getLength(); i++)
428 {
429 Element doc_node = (Element) query_doc_list.item(i);
430 String coll_name = doc_node.getAttribute("collection");
431 Element coll_items = (Element) coll_map.get(coll_name);
432 if (coll_items == null)
433 {
434 coll_items = result_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
435 coll_map.put(coll_name, coll_items);
436 }
437 coll_items.appendChild(result_doc.importNode(doc_node, true));
438 }
439
440 // create teh individual requests
441 Document msg_doc = XMLConverter.newDOM();
442 Element meta_request_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
443 // get all the metadata params
444 Element new_param_list = msg_doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
445 Element param = GSXML.createParameter(msg_doc, "metadata", "Title");
446 new_param_list.appendChild(param);
447
448 Set mapping_set = coll_map.entrySet();
449 Iterator iter = mapping_set.iterator();
450
451 while (iter.hasNext())
452 {
453 Map.Entry e = (Map.Entry) iter.next();
454 String cname = (String) e.getKey();
455 Element doc_nodes = (Element) e.getValue();
456 Element meta_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_PROCESS, GSPath.appendLink(cname, DOCUMENT_METADATA_RETRIEVE_SERVICE), userContext);
457 meta_request.appendChild(msg_doc.importNode(doc_nodes, true));
458 meta_request.appendChild(new_param_list.cloneNode(true));
459 meta_request_message.appendChild(meta_request);
460
461 }
462
463 Node meta_result_node = this.router.process(meta_request_message);
464 Element meta_result = GSXML.nodeToElement(meta_result_node);
465
466 // now need to put the doc nodes back in the right order
467 // go through the original list again. keep an element pointer to
468 // the next element in each collections list
469 NodeList meta_responses = meta_result.getElementsByTagName(GSXML.RESPONSE_ELEM);
470 for (int i = 0; i < meta_responses.getLength(); i++)
471 {
472 String collname = GSPath.removeLastLink(((Element) meta_responses.item(i)).getAttribute(GSXML.FROM_ATT));
473 Element first_elem = (Element) GSXML.getNodeByPath(meta_responses.item(i), "documentNodeList/documentNode");
474 coll_map.put(collname, first_elem);
475 }
476
477 for (int i = 0; i < query_doc_list.getLength(); i++)
478 {
479 Element doc_node = (Element) query_doc_list.item(i);
480 Element new_node = (Element) result_doc.importNode(doc_node, false);
481 result_node_list.appendChild(new_node);
482 String coll_name = doc_node.getAttribute("collection");
483
484 Element meta_elem = (Element) coll_map.get(coll_name);
485 GSXML.mergeMetadataLists(new_node, meta_elem);
486 coll_map.put(coll_name, meta_elem.getNextSibling());
487 }
488 return result;
489 }
490}
Note: See TracBrowser for help on using the repository browser.