1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * SolrQueryWrapper.java
|
---|
4 | *
|
---|
5 | * Copyright 2004 The New Zealand Digital Library Project
|
---|
6 | *
|
---|
7 | * A component of the Greenstone digital library software
|
---|
8 | * from the New Zealand Digital Library Project at the
|
---|
9 | * University of Waikato, New Zealand.
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | *
|
---|
25 | *********************************************************************/
|
---|
26 | package org.greenstone.gsdl3.util;
|
---|
27 |
|
---|
28 | import org.apache.log4j.Logger;
|
---|
29 | import org.apache.solr.client.solrj.SolrServer;
|
---|
30 | import org.apache.solr.client.solrj.SolrServerException;
|
---|
31 | import org.apache.solr.client.solrj.response.QueryResponse;
|
---|
32 | import org.apache.solr.common.SolrDocument;
|
---|
33 | import org.apache.solr.common.SolrDocumentList;
|
---|
34 | import org.apache.solr.common.params.ModifiableSolrParams;
|
---|
35 | import org.greenstone.LuceneWrapper3.SharedSoleneQuery;
|
---|
36 | import org.greenstone.LuceneWrapper3.SharedSoleneQueryResult;
|
---|
37 |
|
---|
38 | public class SolrQueryWrapper extends SharedSoleneQuery
|
---|
39 | {
|
---|
40 |
|
---|
41 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.SolrQueryWrapper.class.getName());
|
---|
42 |
|
---|
43 | protected int max_docs = 100;
|
---|
44 |
|
---|
45 | SolrServer solr_core = null;
|
---|
46 |
|
---|
47 | public SolrQueryWrapper()
|
---|
48 | {
|
---|
49 | super();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void setMaxDocs(int max_docs)
|
---|
53 | {
|
---|
54 | this.max_docs = max_docs;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public void setSolrCore(SolrServer solr_core)
|
---|
58 | {
|
---|
59 | this.solr_core = solr_core;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public boolean initialise()
|
---|
63 | {
|
---|
64 |
|
---|
65 | if (solr_core == null)
|
---|
66 | {
|
---|
67 | utf8out.println("Solr Core not loaded in ");
|
---|
68 | utf8out.flush();
|
---|
69 | return false;
|
---|
70 | }
|
---|
71 | return true;
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | public SharedSoleneQueryResult runQuery(String query_string)
|
---|
76 | {
|
---|
77 |
|
---|
78 | if (query_string == null || query_string.equals(""))
|
---|
79 | {
|
---|
80 | utf8out.println("The query word is not indicated ");
|
---|
81 | utf8out.flush();
|
---|
82 | return null;
|
---|
83 | }
|
---|
84 |
|
---|
85 | SolrQueryResult solr_query_result = new SolrQueryResult();
|
---|
86 | solr_query_result.clear();
|
---|
87 |
|
---|
88 | ModifiableSolrParams solrParams = new ModifiableSolrParams();
|
---|
89 | solrParams.set("q", query_string);
|
---|
90 | solrParams.set("start", start_results);
|
---|
91 | solrParams.set("rows", (end_results - start_results) + 1);
|
---|
92 | solrParams.set("fl", "docOID score");
|
---|
93 |
|
---|
94 | try
|
---|
95 | {
|
---|
96 | QueryResponse solrResponse = solr_core.query(solrParams);
|
---|
97 |
|
---|
98 | SolrDocumentList hits = solrResponse.getResults();
|
---|
99 |
|
---|
100 | if (hits != null)
|
---|
101 | {
|
---|
102 |
|
---|
103 | logger.info("*** hits size = " + hits.size());
|
---|
104 | logger.info("*** num docs found = " + hits.getNumFound());
|
---|
105 |
|
---|
106 | logger.info("*** start results = " + start_results);
|
---|
107 | logger.info("*** end results = " + end_results);
|
---|
108 | logger.info("*** max docs = " + max_docs);
|
---|
109 |
|
---|
110 | // numDocsFound is the total number of mactching docs in the collection
|
---|
111 | // as opposed to the number of documents returned in the hits list
|
---|
112 |
|
---|
113 | solr_query_result.setTotalDocs((int) hits.getNumFound());
|
---|
114 |
|
---|
115 | solr_query_result.setStartResults(start_results);
|
---|
116 | solr_query_result.setEndResults(start_results + hits.size());
|
---|
117 |
|
---|
118 | // Output the matching documents
|
---|
119 | for (int i = 0; i < hits.size(); i++)
|
---|
120 | {
|
---|
121 | SolrDocument doc = hits.get(i);
|
---|
122 |
|
---|
123 | // Need to think about how to support document term frequency. Make zero for now
|
---|
124 | int doc_term_freq = 0;
|
---|
125 | String docOID = (String) doc.get("docOID");
|
---|
126 | Float score = (Float) doc.get("score");
|
---|
127 |
|
---|
128 | logger.info("**** docOID = " + docOID);
|
---|
129 | logger.info("**** score = " + score);
|
---|
130 |
|
---|
131 | solr_query_result.addDoc(docOID, score.floatValue(), doc_term_freq);
|
---|
132 | }
|
---|
133 | }
|
---|
134 | else
|
---|
135 | {
|
---|
136 | solr_query_result.setTotalDocs(0);
|
---|
137 |
|
---|
138 | solr_query_result.setStartResults(0);
|
---|
139 | solr_query_result.setEndResults(0);
|
---|
140 | }
|
---|
141 | }
|
---|
142 | catch (SolrServerException server_exception)
|
---|
143 | {
|
---|
144 | solr_query_result.setError(SolrQueryResult.SERVER_ERROR);
|
---|
145 | }
|
---|
146 |
|
---|
147 | return solr_query_result;
|
---|
148 | }
|
---|
149 |
|
---|
150 | public void cleanUp()
|
---|
151 | {
|
---|
152 | super.cleanUp();
|
---|
153 | }
|
---|
154 | }
|
---|