1 | /*
|
---|
2 | * GS2LuceneSearch.java
|
---|
3 | * Copyright (C) 2006 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * the Free Software Foundation; either version 2 of the License, or
|
---|
7 | * (at your option) any later version.
|
---|
8 | *
|
---|
9 | * This program is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | *
|
---|
14 | * You should have received a copy of the GNU General Public License
|
---|
15 | * along with this program; if not, write to the Free Software
|
---|
16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.greenstone.gsdl3.service;
|
---|
20 |
|
---|
21 | // Greenstone classes
|
---|
22 | import java.io.File;
|
---|
23 | import java.util.HashMap;
|
---|
24 | import java.util.Iterator;
|
---|
25 | import java.util.Map;
|
---|
26 | import java.util.Set;
|
---|
27 | import java.util.Vector;
|
---|
28 |
|
---|
29 | import org.apache.log4j.Logger;
|
---|
30 | import org.greenstone.LuceneWrapper3.GS2LuceneQuery;
|
---|
31 | import org.greenstone.LuceneWrapper3.LuceneQueryResult;
|
---|
32 | import org.greenstone.gsdl3.util.GSFile;
|
---|
33 | import org.greenstone.gsdl3.util.GSXML;
|
---|
34 | import org.w3c.dom.Element;
|
---|
35 |
|
---|
36 | public class GS2LuceneSearch extends SharedSoleneGS2FieldSearch
|
---|
37 | {
|
---|
38 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2LuceneSearch.class.getName());
|
---|
39 |
|
---|
40 | private GS2LuceneQuery lucene_src = null;
|
---|
41 |
|
---|
42 | public GS2LuceneSearch()
|
---|
43 | {
|
---|
44 | this.lucene_src = new GS2LuceneQuery();
|
---|
45 | }
|
---|
46 |
|
---|
47 | public void cleanUp()
|
---|
48 | {
|
---|
49 | super.cleanUp();
|
---|
50 | this.lucene_src.cleanUp();
|
---|
51 | }
|
---|
52 |
|
---|
53 | /** methods to handle actually doing the query */
|
---|
54 |
|
---|
55 | /** do any initialisation of the query object */
|
---|
56 | protected boolean setUpQueryer(HashMap params)
|
---|
57 | {
|
---|
58 | String indexdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar + "index" + File.separatorChar;
|
---|
59 |
|
---|
60 | String index = "didx";
|
---|
61 | String physical_index_language_name = null;
|
---|
62 | String physical_sub_index_name = null;
|
---|
63 | int maxdocs = 100;
|
---|
64 | int hits_per_page = 20;
|
---|
65 | int start_page = 1;
|
---|
66 | // set up the query params
|
---|
67 | Set entries = params.entrySet();
|
---|
68 | Iterator i = entries.iterator();
|
---|
69 | while (i.hasNext())
|
---|
70 | {
|
---|
71 | Map.Entry m = (Map.Entry) i.next();
|
---|
72 | String name = (String) m.getKey();
|
---|
73 | String value = (String) m.getValue();
|
---|
74 |
|
---|
75 | if (name.equals(MAXDOCS_PARAM) && !value.equals(""))
|
---|
76 | {
|
---|
77 | maxdocs = Integer.parseInt(value);
|
---|
78 | }
|
---|
79 | else if (name.equals(HITS_PER_PAGE_PARAM))
|
---|
80 | {
|
---|
81 | hits_per_page = Integer.parseInt(value);
|
---|
82 | }
|
---|
83 | else if (name.equals(START_PAGE_PARAM))
|
---|
84 | {
|
---|
85 | start_page = Integer.parseInt(value);
|
---|
86 |
|
---|
87 | }
|
---|
88 | else if (name.equals(MATCH_PARAM))
|
---|
89 | {
|
---|
90 | if (value.equals(MATCH_PARAM_ALL))
|
---|
91 | {
|
---|
92 | this.lucene_src.setDefaultConjunctionOperator("AND");
|
---|
93 | }
|
---|
94 | else
|
---|
95 | {
|
---|
96 | this.lucene_src.setDefaultConjunctionOperator("OR");
|
---|
97 | }
|
---|
98 | }
|
---|
99 | else if (name.equals(RANK_PARAM))
|
---|
100 | {
|
---|
101 | if (value.equals(RANK_PARAM_RANK_VALUE))
|
---|
102 | {
|
---|
103 | value = null;
|
---|
104 | }
|
---|
105 | this.lucene_src.setSortField(value);
|
---|
106 | }
|
---|
107 | else if (name.equals(LEVEL_PARAM))
|
---|
108 | {
|
---|
109 | if (value.toUpperCase().equals("SEC"))
|
---|
110 | {
|
---|
111 | index = "sidx";
|
---|
112 | }
|
---|
113 | else
|
---|
114 | {
|
---|
115 | index = "didx";
|
---|
116 | }
|
---|
117 | }
|
---|
118 | else if (name.equals(INDEX_SUBCOLLECTION_PARAM))
|
---|
119 | {
|
---|
120 | physical_sub_index_name = value;
|
---|
121 | }
|
---|
122 | else if (name.equals(INDEX_LANGUAGE_PARAM))
|
---|
123 | {
|
---|
124 | physical_index_language_name = value;
|
---|
125 | } // ignore any others
|
---|
126 | }
|
---|
127 | // set up start and end results if necessary
|
---|
128 | int start_results = 1;
|
---|
129 | if (start_page != 1)
|
---|
130 | {
|
---|
131 | start_results = ((start_page - 1) * hits_per_page) + 1;
|
---|
132 | }
|
---|
133 | int end_results = hits_per_page * start_page;
|
---|
134 | this.lucene_src.setStartResults(start_results);
|
---|
135 | this.lucene_src.setEndResults(end_results);
|
---|
136 |
|
---|
137 | if (index.equals("sidx") || index.equals("didx"))
|
---|
138 | {
|
---|
139 | if (physical_sub_index_name != null)
|
---|
140 | {
|
---|
141 | index += physical_sub_index_name;
|
---|
142 | }
|
---|
143 | if (physical_index_language_name != null)
|
---|
144 | {
|
---|
145 | index += physical_index_language_name;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | this.lucene_src.setIndexDir(indexdir + index);
|
---|
150 | this.lucene_src.initialise();
|
---|
151 | return true;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** do the query */
|
---|
155 | protected Object runQuery(String query)
|
---|
156 | {
|
---|
157 | try
|
---|
158 | {
|
---|
159 | LuceneQueryResult lqr = this.lucene_src.runQuery(query);
|
---|
160 | return lqr;
|
---|
161 | }
|
---|
162 | catch (Exception e)
|
---|
163 | {
|
---|
164 | logger.error("Exception happened in runQuery(): ", e);
|
---|
165 | }
|
---|
166 |
|
---|
167 | return null;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /** get the total number of docs that match */
|
---|
171 | protected long numDocsMatched(Object query_result)
|
---|
172 | {
|
---|
173 | return ((LuceneQueryResult) query_result).getTotalDocs();
|
---|
174 | }
|
---|
175 |
|
---|
176 | /** get the list of doc ids */
|
---|
177 | protected String[] getDocIDs(Object query_result)
|
---|
178 | {
|
---|
179 | Vector docs = ((LuceneQueryResult) query_result).getDocs();
|
---|
180 | String[] doc_nums = new String[docs.size()];
|
---|
181 | for (int d = 0; d < docs.size(); d++)
|
---|
182 | {
|
---|
183 | String doc_num = ((LuceneQueryResult.DocInfo) docs.elementAt(d)).id_;
|
---|
184 | doc_nums[d] = doc_num;
|
---|
185 | }
|
---|
186 | return doc_nums;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** get the list of doc ranks */
|
---|
190 | protected String[] getDocRanks(Object query_result)
|
---|
191 | {
|
---|
192 | Vector docs = ((LuceneQueryResult) query_result).getDocs();
|
---|
193 | String[] doc_ranks = new String[docs.size()];
|
---|
194 | for (int d = 0; d < docs.size(); d++)
|
---|
195 | {
|
---|
196 | doc_ranks[d] = Float.toString(((LuceneQueryResult.DocInfo) docs.elementAt(d)).rank_);
|
---|
197 | }
|
---|
198 | return doc_ranks;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /** add in term info if available */
|
---|
202 | protected boolean addTermInfo(Element term_list, HashMap params, Object query_result)
|
---|
203 | {
|
---|
204 | String query_level = (String) params.get(LEVEL_PARAM); // the current query level
|
---|
205 |
|
---|
206 | Vector terms = ((LuceneQueryResult) query_result).getTerms();
|
---|
207 | for (int t = 0; t < terms.size(); t++)
|
---|
208 | {
|
---|
209 | LuceneQueryResult.TermInfo term_info = (LuceneQueryResult.TermInfo) terms.get(t);
|
---|
210 |
|
---|
211 | Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
|
---|
212 | term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_);
|
---|
213 | term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_);
|
---|
214 | term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_);
|
---|
215 | term_elem.setAttribute(FIELD_ATT, term_info.field_);
|
---|
216 | term_list.appendChild(term_elem);
|
---|
217 | }
|
---|
218 |
|
---|
219 | Vector stopwords = ((LuceneQueryResult) query_result).getStopWords();
|
---|
220 | for (int t = 0; t < stopwords.size(); t++)
|
---|
221 | {
|
---|
222 | String stopword = (String) stopwords.get(t);
|
---|
223 |
|
---|
224 | Element stopword_elem = this.doc.createElement(GSXML.STOPWORD_ELEM);
|
---|
225 | stopword_elem.setAttribute(GSXML.NAME_ATT, stopword);
|
---|
226 | term_list.appendChild(stopword_elem);
|
---|
227 | }
|
---|
228 |
|
---|
229 | return true;
|
---|
230 | }
|
---|
231 | }
|
---|