source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/LuceneSearch.java@ 13927

Last change on this file since 13927 was 13927, checked in by kjdon, 17 years ago

now uses a default index if no index param is specified for a query

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