1 | package org.greenstone.gsdl3.service;
|
---|
2 |
|
---|
3 | // Greenstone classes
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 |
|
---|
6 | // XML classes
|
---|
7 | import org.w3c.dom.Element;
|
---|
8 | import org.w3c.dom.Document;
|
---|
9 | import org.w3c.dom.NodeList;
|
---|
10 |
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.ArrayList;
|
---|
13 |
|
---|
14 | import org.apache.lucene.analysis.Analyzer;
|
---|
15 | import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
---|
16 | import org.apache.lucene.document.*; //Document;
|
---|
17 | import org.apache.lucene.search.Searcher;
|
---|
18 | import org.apache.lucene.search.IndexSearcher;
|
---|
19 | import org.apache.lucene.search.Query;
|
---|
20 | import org.apache.lucene.search.Hits;
|
---|
21 | import org.apache.lucene.queryParser.QueryParser;
|
---|
22 | import org.apache.lucene.search.TermQuery;
|
---|
23 | import org.apache.lucene.index.Term;
|
---|
24 |
|
---|
25 | import java.io.File;
|
---|
26 |
|
---|
27 | import org.apache.log4j.*;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | *
|
---|
31 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
32 | * @version $Revision: 13270 $
|
---|
33 | */
|
---|
34 |
|
---|
35 | public class LuceneSearch
|
---|
36 | extends AbstractSearch {
|
---|
37 |
|
---|
38 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.LuceneSearch.class.getName());
|
---|
39 |
|
---|
40 | protected static final String INDEX_ELEM = "index";
|
---|
41 |
|
---|
42 | protected void getIndexData(ArrayList index_ids, ArrayList index_names, String lang)
|
---|
43 | {
|
---|
44 | // the index info - read from config file - cache it??
|
---|
45 | Element index_list = (Element)GSXML.getChildByTagName(this.config_info, INDEX_ELEM+GSXML.LIST_MODIFIER);
|
---|
46 | if (index_list != null) {
|
---|
47 | NodeList indexes = index_list.getElementsByTagName(INDEX_ELEM);
|
---|
48 | int len = indexes.getLength();
|
---|
49 | // now add even if there is only one
|
---|
50 | for (int i=0; i<len; i++) {
|
---|
51 | Element index = (Element)indexes.item(i);
|
---|
52 | index_ids.add(index.getAttribute(GSXML.NAME_ATT));
|
---|
53 | index_names.add(GSXML.getDisplayText(index, GSXML.DISPLAY_TEXT_NAME, lang, "en"));
|
---|
54 |
|
---|
55 | }
|
---|
56 | } else {
|
---|
57 | // there is only one index, so we assume the default
|
---|
58 | index_ids.add("idx");
|
---|
59 | index_names.add("Default index");
|
---|
60 | }
|
---|
61 |
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** Process a text query - implemented by concrete subclasses */
|
---|
65 | protected Element processTextQuery(Element request) {
|
---|
66 |
|
---|
67 | // Create a new (empty) result message
|
---|
68 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
69 | result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE);
|
---|
70 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
71 | Element doc_node_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
72 | result.appendChild(doc_node_list);
|
---|
73 | Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
74 | result.appendChild(metadata_list);
|
---|
75 | // Get the parameters of the request
|
---|
76 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
77 | if (param_list == null) {
|
---|
78 | logger.error("TextQuery request had no paramList.");
|
---|
79 | GSXML.addMetadata(this.doc, metadata_list, "numDocsMatched", "0");
|
---|
80 | return result; // Return the empty result
|
---|
81 | }
|
---|
82 |
|
---|
83 | // Process the request parameters
|
---|
84 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
85 |
|
---|
86 | // Make sure a query has been specified
|
---|
87 | String query_string = (String) params.get(QUERY_PARAM);
|
---|
88 | if (query_string == null || query_string.equals("")) {
|
---|
89 | logger.error("TextQuery request had no query string.");
|
---|
90 | GSXML.addMetadata(this.doc, metadata_list, "numDocsMatched", "0");
|
---|
91 | return result; // Return the empty result
|
---|
92 | }
|
---|
93 |
|
---|
94 | // Get the index
|
---|
95 | String index = (String) params.get(INDEX_PARAM);
|
---|
96 | try {
|
---|
97 | String index_dir = GSFile.collectionIndexDir(this.site_home, this.cluster_name);
|
---|
98 | index_dir += File.separator+index;
|
---|
99 | Searcher searcher = new IndexSearcher(index_dir);
|
---|
100 | Analyzer analyzer = new StandardAnalyzer();
|
---|
101 |
|
---|
102 | Term term = new Term("content", query_string);
|
---|
103 |
|
---|
104 | Query query = new TermQuery(term);
|
---|
105 |
|
---|
106 | Hits hits = searcher.search(query);
|
---|
107 | GSXML.addMetadata(this.doc, metadata_list, "numDocsMatched", ""+hits.length());
|
---|
108 |
|
---|
109 | for (int i=0; i<hits.length(); i++) {
|
---|
110 | org.apache.lucene.document.Document luc_doc = hits.doc(i);
|
---|
111 | String node_id = luc_doc.get("nodeID");
|
---|
112 | Element node = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
113 | node.setAttribute(GSXML.NODE_ID_ATT, node_id);
|
---|
114 | doc_node_list.appendChild(node);
|
---|
115 | }
|
---|
116 | } catch (Exception e) {
|
---|
117 | e.printStackTrace();
|
---|
118 | }
|
---|
119 |
|
---|
120 | return result;
|
---|
121 |
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | }
|
---|