1 | package org.greenstone.gsdl3.service;
|
---|
2 |
|
---|
3 | // Greenstone classes
|
---|
4 | import org.greenstone.gdbm.*;
|
---|
5 | import org.greenstone.gsdl3.util.*;
|
---|
6 |
|
---|
7 | // XML classes
|
---|
8 | import org.w3c.dom.Element;
|
---|
9 | import org.w3c.dom.Document;
|
---|
10 | import org.w3c.dom.NodeList;
|
---|
11 |
|
---|
12 | import java.util.HashMap;
|
---|
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 | *
|
---|
28 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
29 | * @version $Revision: 5257 $
|
---|
30 | */
|
---|
31 |
|
---|
32 | public class LuceneSearch
|
---|
33 | extends ServiceRack {
|
---|
34 |
|
---|
35 | // the services on offer
|
---|
36 | // these strings must match what is found in the properties file
|
---|
37 | protected static final String TEXT_QUERY_SERVICE = "TextQuery";
|
---|
38 |
|
---|
39 | protected static final String QUERY_PARAM = "query";
|
---|
40 | protected static final String WORK_PARAM = "work";
|
---|
41 |
|
---|
42 | public boolean configure(Element info, Element extra_info) {
|
---|
43 |
|
---|
44 | Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
45 | tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
46 | tq_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
|
---|
47 | this.short_service_info.appendChild(tq_service);
|
---|
48 |
|
---|
49 | return true;
|
---|
50 |
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected Element getServiceDescription(String service, String lang, String subset) {
|
---|
54 |
|
---|
55 | if (!service.equals(TEXT_QUERY_SERVICE)) {
|
---|
56 | return null;
|
---|
57 | }
|
---|
58 | Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
59 | tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
60 | tq_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
|
---|
61 | return tq_service;
|
---|
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 |
|
---|
74 | // Get the parameters of the request
|
---|
75 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
76 | if (param_list == null) {
|
---|
77 | System.err.println("Error: TextQuery request had no paramList.");
|
---|
78 | return result; // Return the empty result
|
---|
79 | }
|
---|
80 |
|
---|
81 | // Process the request parameters
|
---|
82 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
83 |
|
---|
84 | // Make sure a query has been specified
|
---|
85 | String query_string = (String) params.get(QUERY_PARAM);
|
---|
86 | if (query_string == null || query_string.equals("")) {
|
---|
87 | System.err.println("Error: TextQuery request had no query string.");
|
---|
88 | return result; // Return the empty result
|
---|
89 | }
|
---|
90 |
|
---|
91 | try {
|
---|
92 | String index_dir = GSFile.collectionIndexDir(this.site_home, this.cluster_name);
|
---|
93 | index_dir += File.separator+"idx";
|
---|
94 | Searcher searcher = new IndexSearcher(index_dir);
|
---|
95 | Analyzer analyzer = new StandardAnalyzer();
|
---|
96 |
|
---|
97 | Term term = new Term("content", query_string);
|
---|
98 |
|
---|
99 | Query query = new TermQuery(term);
|
---|
100 |
|
---|
101 |
|
---|
102 | System.out.println("Searching for: " + query.toString("content"));
|
---|
103 |
|
---|
104 | Hits hits = searcher.search(query);
|
---|
105 | System.out.println(hits.length() + " total matching documents");
|
---|
106 |
|
---|
107 | for (int i=0; i<hits.length(); i++) {
|
---|
108 | org.apache.lucene.document.Document luc_doc = hits.doc(i);
|
---|
109 | String node_id = luc_doc.get("nodeID");
|
---|
110 | Element node = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
111 | node.setAttribute(GSXML.NODE_ID_ATT, node_id);
|
---|
112 | doc_node_list.appendChild(node);
|
---|
113 | }
|
---|
114 | } catch (Exception e) {
|
---|
115 | e.printStackTrace();
|
---|
116 | }
|
---|
117 |
|
---|
118 | return result;
|
---|
119 |
|
---|
120 | }
|
---|
121 |
|
---|
122 | }
|
---|