[5257] | 1 | package org.greenstone.gsdl3.service;
|
---|
| 2 |
|
---|
| 3 | // Greenstone classes
|
---|
[25763] | 4 | import java.io.File;
|
---|
| 5 | import java.io.Serializable;
|
---|
| 6 | import java.util.ArrayList;
|
---|
[5257] | 7 | import java.util.HashMap;
|
---|
| 8 |
|
---|
[25763] | 9 | import org.apache.log4j.Logger;
|
---|
[29143] | 10 | import org.apache.lucene.index.DirectoryReader;
|
---|
[25763] | 11 | import org.apache.lucene.index.IndexReader;
|
---|
| 12 | import org.apache.lucene.index.Term;
|
---|
[5257] | 13 | import org.apache.lucene.search.IndexSearcher;
|
---|
| 14 | import org.apache.lucene.search.Query;
|
---|
| 15 | import org.apache.lucene.search.TermQuery;
|
---|
[24724] | 16 | import org.apache.lucene.search.TopDocs;
|
---|
[25763] | 17 | import org.apache.lucene.store.Directory;
|
---|
[24724] | 18 | import org.apache.lucene.store.FSDirectory;
|
---|
[25763] | 19 | import org.greenstone.gsdl3.util.GSFile;
|
---|
| 20 | import org.greenstone.gsdl3.util.GSXML;
|
---|
[28966] | 21 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
| 22 |
|
---|
| 23 | import org.w3c.dom.Document;
|
---|
[25763] | 24 | import org.w3c.dom.Element;
|
---|
| 25 | import org.w3c.dom.NodeList;
|
---|
[5257] | 26 |
|
---|
[25762] | 27 | public class LuceneSearch extends AbstractTextSearch
|
---|
| 28 | {
|
---|
| 29 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.LuceneSearch.class.getName());
|
---|
[13124] | 30 |
|
---|
[25762] | 31 | protected static final String INDEX_ELEM = "index";
|
---|
[24722] | 32 |
|
---|
[25762] | 33 | protected ArrayList<String> index_ids;
|
---|
[24722] | 34 |
|
---|
[25762] | 35 | public LuceneSearch()
|
---|
| 36 | {
|
---|
| 37 | index_ids = new ArrayList<String>();
|
---|
[13927] | 38 | }
|
---|
[24722] | 39 |
|
---|
[25762] | 40 | public boolean configure(Element info, Element extra_info)
|
---|
| 41 | {
|
---|
| 42 | if (!super.configure(info, extra_info))
|
---|
| 43 | {
|
---|
| 44 | return false;
|
---|
| 45 | }
|
---|
[24722] | 46 |
|
---|
[25762] | 47 | default_index = "idx";
|
---|
| 48 |
|
---|
| 49 | // cache index info read from config file
|
---|
| 50 | Element index_list = (Element) GSXML.getChildByTagName(this.config_info, INDEX_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 51 | if (index_list != null)
|
---|
| 52 | {
|
---|
| 53 | NodeList indexes = index_list.getElementsByTagName(INDEX_ELEM);
|
---|
| 54 | int len = indexes.getLength();
|
---|
| 55 | // now add even if there is only one
|
---|
| 56 | for (int i = 0; i < len; i++)
|
---|
| 57 | {
|
---|
| 58 | Element index = (Element) indexes.item(i);
|
---|
| 59 | index_ids.add(index.getAttribute(GSXML.NAME_ATT));
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | else
|
---|
| 63 | {
|
---|
| 64 | // there is only one index, so we assume the default
|
---|
| 65 | index_ids.add(this.default_index);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | return true;
|
---|
[24722] | 69 | }
|
---|
| 70 |
|
---|
[25762] | 71 | protected void getIndexData(ArrayList<String> index_ids, ArrayList<String> index_names, String lang)
|
---|
| 72 | {
|
---|
| 73 | // copying exercise for index_ids,
|
---|
| 74 | for (int i = 0; i < this.index_ids.size(); i++)
|
---|
| 75 | {
|
---|
| 76 | index_ids.add(this.index_ids.get(i));
|
---|
| 77 | }
|
---|
[24722] | 78 |
|
---|
[25762] | 79 | // But need to work out display name from scratch as this uses
|
---|
| 80 | // the 'lang' parameter
|
---|
| 81 |
|
---|
| 82 | Element index_list = (Element) GSXML.getChildByTagName(this.config_info, INDEX_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 83 | if (index_list != null)
|
---|
| 84 | {
|
---|
| 85 | NodeList indexes = index_list.getElementsByTagName(INDEX_ELEM);
|
---|
| 86 | int len = indexes.getLength();
|
---|
| 87 | // now add even if there is only one
|
---|
| 88 | for (int i = 0; i < len; i++)
|
---|
| 89 | {
|
---|
| 90 | Element index = (Element) indexes.item(i);
|
---|
| 91 | index_names.add(GSXML.getDisplayText(index, GSXML.DISPLAY_TEXT_NAME, lang, "en"));
|
---|
| 92 |
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | else
|
---|
| 96 | {
|
---|
| 97 | // there is only one index, so we assume the default
|
---|
| 98 | index_names.add("default index");
|
---|
| 99 | }
|
---|
[5963] | 100 | }
|
---|
[5257] | 101 |
|
---|
[25762] | 102 | protected void initResultElement(Element result, Element doc_node_list, Element metadata_list)
|
---|
| 103 | {
|
---|
| 104 | // Create a new (empty) result message
|
---|
| 105 | result.setAttribute(GSXML.FROM_ATT, QUERY_SERVICE);
|
---|
| 106 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
| 107 | result.appendChild(doc_node_list);
|
---|
| 108 | result.appendChild(metadata_list);
|
---|
| 109 | }
|
---|
[24722] | 110 |
|
---|
[25762] | 111 | protected boolean hasParamList(Element request, Element metadata_list)
|
---|
| 112 | {
|
---|
| 113 | // Get the parameters of the request
|
---|
| 114 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 115 | if (param_list == null)
|
---|
| 116 | {
|
---|
| 117 | logger.error("TextQuery request had no paramList.");
|
---|
[28966] | 118 | GSXML.addMetadata(metadata_list, "numDocsMatched", "0");
|
---|
[25762] | 119 | return false; // signal that an empty result should be return
|
---|
| 120 | }
|
---|
[24722] | 121 |
|
---|
[25762] | 122 | return true;
|
---|
[5257] | 123 | }
|
---|
| 124 |
|
---|
[25762] | 125 | protected boolean hasQueryString(Element param_list, Element metadata_list)
|
---|
| 126 | {
|
---|
| 127 | // Process the request parameters to make sure a query has been specified
|
---|
| 128 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
| 129 | String query_string = (String) params.get(QUERY_PARAM);
|
---|
[24722] | 130 |
|
---|
[25762] | 131 | if (query_string == null || query_string.equals(""))
|
---|
| 132 | {
|
---|
| 133 | logger.error("TextQuery request had no query string.");
|
---|
[28966] | 134 | GSXML.addMetadata(metadata_list, "numDocsMatched", "0");
|
---|
[25762] | 135 | return false; // signal that an empty result should be return
|
---|
| 136 | }
|
---|
[5257] | 137 |
|
---|
[25762] | 138 | return true;
|
---|
[5257] | 139 | }
|
---|
| 140 |
|
---|
[25762] | 141 | /** Process a text query - implemented by concrete subclasses */
|
---|
| 142 | protected Element processTextQuery(Element request)
|
---|
| 143 | {
|
---|
[28966] | 144 | Document result_doc = XMLConverter.newDOM();
|
---|
| 145 | Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
| 146 | Element doc_node_list = result_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 147 | Element metadata_list = result_doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25762] | 148 | initResultElement(result, doc_node_list, metadata_list);
|
---|
[24722] | 149 |
|
---|
[25762] | 150 | if (!hasParamList(request, metadata_list))
|
---|
| 151 | {
|
---|
| 152 | return result;
|
---|
| 153 | }
|
---|
[24722] | 154 |
|
---|
[25762] | 155 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 156 | if (!hasQueryString(param_list, metadata_list))
|
---|
| 157 | {
|
---|
| 158 | return result;
|
---|
| 159 | }
|
---|
[24722] | 160 |
|
---|
[25762] | 161 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
| 162 | String query_string = (String) params.get(QUERY_PARAM);
|
---|
[24722] | 163 |
|
---|
[25762] | 164 | // Get the index
|
---|
| 165 | String index = (String) params.get(INDEX_PARAM);
|
---|
| 166 | if (index == null || index.equals(""))
|
---|
| 167 | {
|
---|
| 168 | index = this.default_index; // assume the default
|
---|
| 169 | }
|
---|
[24722] | 170 |
|
---|
[25762] | 171 | try
|
---|
| 172 | {
|
---|
| 173 | String index_dir = GSFile.collectionIndexDir(this.site_home, this.cluster_name);
|
---|
| 174 | index_dir += File.separator + index;
|
---|
| 175 | Directory index_dir_dir = FSDirectory.open(new File(index_dir));
|
---|
[29143] | 176 | IndexReader reader = DirectoryReader.open(index_dir_dir); //deprecated: IndexReader.open(index_dir_dir);
|
---|
| 177 | IndexSearcher searcher = new IndexSearcher(reader);
|
---|
[24722] | 178 |
|
---|
[25762] | 179 | Term term = new Term("content", query_string);
|
---|
[24722] | 180 |
|
---|
[25762] | 181 | Query query = new TermQuery(term);
|
---|
[24724] | 182 |
|
---|
[25762] | 183 | TopDocs hits = searcher.search(query, Integer.MAX_VALUE);
|
---|
[24724] | 184 |
|
---|
[28966] | 185 | GSXML.addMetadata(metadata_list, "numDocsMatched", "" + hits.scoreDocs.length);
|
---|
[25762] | 186 |
|
---|
| 187 | for (int i = 0; i < hits.scoreDocs.length; i++)
|
---|
| 188 | {
|
---|
| 189 | int lucene_doc_num = hits.scoreDocs[i].doc;
|
---|
| 190 | org.apache.lucene.document.Document luc_doc = reader.document(lucene_doc_num);
|
---|
| 191 | String node_id = luc_doc.get("nodeID");
|
---|
[28966] | 192 | Element node = result_doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
[25762] | 193 | node.setAttribute(GSXML.NODE_ID_ATT, node_id);
|
---|
| 194 | doc_node_list.appendChild(node);
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 | catch (Exception e)
|
---|
| 198 | {
|
---|
| 199 | e.printStackTrace();
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | return result;
|
---|
[5257] | 203 | }
|
---|
| 204 | }
|
---|