1 | package org.greenstone.gsdl3.service;
|
---|
2 |
|
---|
3 |
|
---|
4 | // Based on LuceneSearch, but not thoroughly tested
|
---|
5 |
|
---|
6 | // Greenstone classes
|
---|
7 | import org.greenstone.gsdl3.util.*;
|
---|
8 | import org.greenstone.util.GlobalProperties;
|
---|
9 |
|
---|
10 | // XML classes
|
---|
11 | import org.w3c.dom.Element;
|
---|
12 | import org.w3c.dom.Document;
|
---|
13 | import org.w3c.dom.NodeList;
|
---|
14 |
|
---|
15 | import java.util.HashMap;
|
---|
16 | import java.util.ArrayList;
|
---|
17 |
|
---|
18 |
|
---|
19 | import org.apache.solr.client.solrj.SolrQuery;
|
---|
20 | import org.apache.solr.client.solrj.SolrServer;
|
---|
21 | import org.apache.solr.client.solrj.SolrServerException;
|
---|
22 | import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
|
---|
23 | import org.apache.solr.client.solrj.response.QueryResponse;
|
---|
24 | import org.apache.solr.common.SolrDocument;
|
---|
25 | import org.apache.solr.common.SolrDocumentList;
|
---|
26 | import org.apache.solr.common.params.ModifiableSolrParams;
|
---|
27 | import org.apache.solr.common.params.SolrParams;
|
---|
28 | import org.apache.solr.common.util.NamedList;
|
---|
29 | import org.apache.solr.servlet.SolrRequestParsers;
|
---|
30 | import org.apache.solr.core.CoreContainer;
|
---|
31 |
|
---|
32 | import java.io.File;
|
---|
33 | import java.util.Collection;
|
---|
34 |
|
---|
35 |
|
---|
36 | import org.apache.log4j.*;
|
---|
37 |
|
---|
38 |
|
---|
39 | public class SolrSearch extends LuceneSearch {
|
---|
40 |
|
---|
41 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.SolrSearch.class.getName());
|
---|
42 |
|
---|
43 | static protected CoreContainer solr_cores = null;
|
---|
44 | protected HashMap solr_server;
|
---|
45 |
|
---|
46 | public SolrSearch()
|
---|
47 | {
|
---|
48 | solr_server = new HashMap();
|
---|
49 |
|
---|
50 | if (solr_cores == null) {
|
---|
51 | // Share one CoreContainer across all sites/collections
|
---|
52 | try {
|
---|
53 |
|
---|
54 | String gsdl3_home = GlobalProperties.getGSDL3Home();
|
---|
55 | String solr_ext_name = GlobalProperties.getProperty("gsdlext.solr.dirname","solr");
|
---|
56 |
|
---|
57 | String solr_home_str = GSFile.extHome(gsdl3_home,solr_ext_name);
|
---|
58 | File solr_home = new File(solr_home_str);
|
---|
59 | File solr_xml = new File( solr_home,"solr.xml" );
|
---|
60 |
|
---|
61 | solr_cores = new CoreContainer(solr_home_str,solr_xml);
|
---|
62 | }
|
---|
63 | catch (Exception e) {
|
---|
64 | e.printStackTrace();
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | // Overriding the cleanUp() method here, so as to parallel the structure of GS2SolrSearch
|
---|
70 | // which also calls shutdown() on its CoreContainer object in GS2SolrSearch.cleanUp().
|
---|
71 | // However, this class has not yet been tested, so it's not certain that this method is
|
---|
72 | // required here.
|
---|
73 | public void cleanUp() {
|
---|
74 | super.cleanUp();
|
---|
75 | solr_cores.shutdown();
|
---|
76 | }
|
---|
77 |
|
---|
78 | public boolean configure(Element info, Element extra_info) {
|
---|
79 | if (!super.configure(info, extra_info)){
|
---|
80 | return false;
|
---|
81 | }
|
---|
82 |
|
---|
83 | // initialize required number of SolrCores based on values
|
---|
84 | // in 'index_ids' that are set by LuceneSearch::configure()
|
---|
85 |
|
---|
86 | String site_name = this.router.getSiteName();
|
---|
87 | String coll_name = this.cluster_name;
|
---|
88 |
|
---|
89 | for (int i=0; i<index_ids.size(); i++) {
|
---|
90 |
|
---|
91 | String idx_name = (String)index_ids.get(i);
|
---|
92 | String core_name = site_name + "-" + coll_name + "-" + idx_name;
|
---|
93 |
|
---|
94 | EmbeddedSolrServer solr_core
|
---|
95 | = new EmbeddedSolrServer(solr_cores,core_name);
|
---|
96 |
|
---|
97 | solr_server.put(core_name,solr_core);
|
---|
98 | }
|
---|
99 |
|
---|
100 | return true;
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected void getIndexData(ArrayList index_ids, ArrayList index_names, String lang)
|
---|
104 | {
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** Process a text query - implemented by concrete subclasses */
|
---|
108 | protected Element processTextQuery(Element request) {
|
---|
109 |
|
---|
110 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
111 | Element doc_node_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
112 | Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
113 | initResultElement(result,doc_node_list,metadata_list);
|
---|
114 |
|
---|
115 | if (!hasParamList(request,metadata_list)) {
|
---|
116 | return result;
|
---|
117 | }
|
---|
118 |
|
---|
119 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
120 | if (!hasQueryString(param_list,metadata_list)) {
|
---|
121 | return result;
|
---|
122 | }
|
---|
123 |
|
---|
124 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
125 | String query_string = (String) params.get(QUERY_PARAM);
|
---|
126 |
|
---|
127 |
|
---|
128 | // Get the index
|
---|
129 | String index = (String) params.get(INDEX_PARAM);
|
---|
130 | if (index == null || index.equals("")) {
|
---|
131 | index = this.default_index; // assume the default
|
---|
132 | }
|
---|
133 |
|
---|
134 | try {
|
---|
135 |
|
---|
136 | ModifiableSolrParams solrParams = new ModifiableSolrParams();
|
---|
137 | solrParams.set("q", query_string);
|
---|
138 | //solrParams.set("start", start);
|
---|
139 | //solrParams.set("rows", nbDocuments);
|
---|
140 |
|
---|
141 | String site_name = this.router.getSiteName();
|
---|
142 | String coll_name = this.cluster_name;
|
---|
143 |
|
---|
144 | String core_name = site_name + "-" + coll_name + "-" + index;
|
---|
145 |
|
---|
146 | EmbeddedSolrServer solr_core = (EmbeddedSolrServer)solr_server.get(core_name);
|
---|
147 |
|
---|
148 | QueryResponse solrResponse = solr_core.query(solrParams);
|
---|
149 |
|
---|
150 | SolrDocumentList hits = solrResponse.getResults();
|
---|
151 |
|
---|
152 | if (hits != null) {
|
---|
153 | // or should this be docs.getNumFound() ??
|
---|
154 | GSXML.addMetadata(this.doc, metadata_list, "numDocsMatched", ""+hits.size());
|
---|
155 |
|
---|
156 | System.err.println(hits.getNumFound() + " documents found, "
|
---|
157 | + hits.size() + " returned : ");
|
---|
158 |
|
---|
159 | for (int i = 0; i < hits.size(); i++) {
|
---|
160 | SolrDocument solr_doc = hits.get(i);
|
---|
161 |
|
---|
162 | String node_id = (String)solr_doc.get("docOID");
|
---|
163 | Element node = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
164 | node.setAttribute(GSXML.NODE_ID_ATT, node_id);
|
---|
165 | doc_node_list.appendChild(node);
|
---|
166 |
|
---|
167 | System.out.println("\t" + solr_doc.toString());
|
---|
168 | }
|
---|
169 | }
|
---|
170 | } catch (Exception e) {
|
---|
171 | e.printStackTrace();
|
---|
172 | }
|
---|
173 |
|
---|
174 | return result;
|
---|
175 |
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | }
|
---|