1 | /*
|
---|
2 | * SharedSoleneGS2FieldSearch.java -- shared base code for Solr and Lucene
|
---|
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.util.ArrayList;
|
---|
23 |
|
---|
24 | import org.apache.log4j.Logger;
|
---|
25 | import org.greenstone.LuceneWrapper3.SharedSoleneQuery;
|
---|
26 | import org.greenstone.gsdl3.util.GSXML;
|
---|
27 | import org.w3c.dom.Document;
|
---|
28 | import org.w3c.dom.Element;
|
---|
29 | import org.w3c.dom.NodeList;
|
---|
30 |
|
---|
31 | // Shared code for Solr and Lucene GS2FieldSearch
|
---|
32 |
|
---|
33 | public abstract class SharedSoleneGS2FieldSearch extends AbstractGS2FieldSearch
|
---|
34 | {
|
---|
35 | protected static final String RANK_PARAM_RANK_VALUE = "rank";
|
---|
36 | protected static final String SORT_ELEM = "sort";
|
---|
37 |
|
---|
38 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.SharedSoleneGS2FieldSearch.class.getName());
|
---|
39 |
|
---|
40 | protected SharedSoleneQuery solene_src = null;
|
---|
41 |
|
---|
42 | public SharedSoleneGS2FieldSearch()
|
---|
43 | {
|
---|
44 | // Lucene/Solr uses double operators, not single
|
---|
45 | AND_OPERATOR = "&&";
|
---|
46 | OR_OPERATOR = "||";
|
---|
47 |
|
---|
48 | does_paging = true;
|
---|
49 | does_chunking = true;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /** configure this service */
|
---|
53 | public boolean configure(Element info, Element extra_info)
|
---|
54 | {
|
---|
55 | if (!super.configure(info, extra_info))
|
---|
56 | {
|
---|
57 | return false;
|
---|
58 | }
|
---|
59 |
|
---|
60 | // the search element
|
---|
61 | Element config_search = (Element) GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM);
|
---|
62 | Document owner = info.getOwnerDocument();
|
---|
63 | // get out the sort fields
|
---|
64 | NodeList sort_nodes = info.getElementsByTagName(SORT_ELEM);
|
---|
65 |
|
---|
66 | for (int i = 0; i < sort_nodes.getLength(); i++)
|
---|
67 | {
|
---|
68 | Element sort = (Element) sort_nodes.item(i);
|
---|
69 | String name = sort.getAttribute(GSXML.NAME_ATT);
|
---|
70 | Element node_extra = GSXML.getNamedElement(config_search, SORT_ELEM, GSXML.NAME_ATT, name);
|
---|
71 | if (node_extra == null)
|
---|
72 | {
|
---|
73 | logger.error("haven't found extra info for sort field named " + name);
|
---|
74 | continue;
|
---|
75 | }
|
---|
76 |
|
---|
77 | // get the display elements if any - displayName
|
---|
78 | NodeList display_names = node_extra.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
|
---|
79 | if (display_names != null)
|
---|
80 | {
|
---|
81 | for (int j = 0; j < display_names.getLength(); j++)
|
---|
82 | {
|
---|
83 | Element e = (Element) display_names.item(j);
|
---|
84 | sort.appendChild(owner.importNode(e, true));
|
---|
85 | }
|
---|
86 | }
|
---|
87 | } // for each sortfield
|
---|
88 | // Lucene/Solr doesn't do case folding or stemming or accent folding at the
|
---|
89 | // moment
|
---|
90 | does_case = false;
|
---|
91 | does_stem = false;
|
---|
92 | does_accent = false;
|
---|
93 |
|
---|
94 | return true;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /** add in the Lucene/Solr specific params to TextQuery */
|
---|
98 | protected void addCustomQueryParams(Element param_list, String lang)
|
---|
99 | {
|
---|
100 | super.addCustomQueryParams(param_list, lang);
|
---|
101 | /** Lucene's/Solr's rank (sort) param is based on sort fields, not ranked/not */
|
---|
102 | createParameter(RANK_PARAM, param_list, lang);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /** create a param and add to the list */
|
---|
106 | /** we override this to do a special rank param */
|
---|
107 | protected void createParameter(String name, Element param_list, String lang)
|
---|
108 | {
|
---|
109 | Element param = null;
|
---|
110 | if (name.equals(RANK_PARAM))
|
---|
111 | {
|
---|
112 | // get the fields
|
---|
113 | ArrayList<String> fields = new ArrayList<String>();
|
---|
114 | //fields.add(RANK_PARAM_RANK_VALUE);
|
---|
115 | ArrayList<String> field_names = new ArrayList<String>();
|
---|
116 | //field_names.add(getTextString("param.sortBy.rank", lang));
|
---|
117 | getSortData(fields, field_names, lang);
|
---|
118 |
|
---|
119 | param = GSXML.createParameterDescription2(this.doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, fields.get(0), fields, field_names);
|
---|
120 | }
|
---|
121 | if (param != null)
|
---|
122 | {
|
---|
123 | param_list.appendChild(param);
|
---|
124 | }
|
---|
125 | else
|
---|
126 | {
|
---|
127 | super.createParameter(name, param_list, lang);
|
---|
128 | }
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 | protected void getSortData(ArrayList<String> sort_ids, ArrayList<String> sort_names, String lang) {
|
---|
133 |
|
---|
134 | Element sort_list = (Element) GSXML.getChildByTagName(this.config_info, SORT_ELEM + GSXML.LIST_MODIFIER);
|
---|
135 | NodeList sorts = sort_list.getElementsByTagName(SORT_ELEM);
|
---|
136 | int len = sorts.getLength();
|
---|
137 | for (int i = 0; i < len; i++)
|
---|
138 | {
|
---|
139 | Element sort = (Element) sorts.item(i);
|
---|
140 | String shortname = sort.getAttribute(GSXML.SHORTNAME_ATT);
|
---|
141 | sort_ids.add(shortname);
|
---|
142 | String display_name = GSXML.getDisplayText(sort, GSXML.DISPLAY_TEXT_NAME, lang, "en");
|
---|
143 | if (display_name.equals(""))
|
---|
144 | {
|
---|
145 | display_name = sort.getAttribute(GSXML.NAME_ATT);
|
---|
146 | if (display_name.equals(""))
|
---|
147 | {
|
---|
148 | display_name = shortname;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | sort_names.add(display_name);
|
---|
152 |
|
---|
153 | }
|
---|
154 |
|
---|
155 | }
|
---|
156 | protected void getSortByIndexData(ArrayList<String> index_ids, ArrayList<String> index_names, String lang)
|
---|
157 | {
|
---|
158 | // the index info -
|
---|
159 | Element index_list = (Element) GSXML.getChildByTagName(this.config_info, INDEX_ELEM + GSXML.LIST_MODIFIER);
|
---|
160 | NodeList indexes = index_list.getElementsByTagName(INDEX_ELEM);
|
---|
161 | int len = indexes.getLength();
|
---|
162 | // now add even if there is only one
|
---|
163 | for (int i = 0; i < len; i++)
|
---|
164 | {
|
---|
165 | Element index = (Element) indexes.item(i);
|
---|
166 | String shortname = index.getAttribute(GSXML.SHORTNAME_ATT);
|
---|
167 | if (shortname.equals("") || shortname.equals("ZZ") || shortname.equals("TX"))
|
---|
168 | {
|
---|
169 | continue;
|
---|
170 | }
|
---|
171 | index_ids.add("by" + shortname);
|
---|
172 | String display_name = GSXML.getDisplayText(index, GSXML.DISPLAY_TEXT_NAME, lang, "en");
|
---|
173 | if (display_name.equals(""))
|
---|
174 | {
|
---|
175 | display_name = index.getAttribute(GSXML.NAME_ATT);
|
---|
176 | if (display_name.equals(""))
|
---|
177 | {
|
---|
178 | display_name = shortname;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | index_names.add(display_name);
|
---|
182 |
|
---|
183 | }
|
---|
184 |
|
---|
185 | }
|
---|
186 |
|
---|
187 | protected String addFieldInfo(String query, String field)
|
---|
188 | {
|
---|
189 | // currently, allfields (ZZ) is stored as a extra field for Lucene
|
---|
190 | if (field.equals(""))
|
---|
191 | { // || field.equals("ZZ")) {
|
---|
192 | return query;
|
---|
193 | }
|
---|
194 | return field + ":(" + query + ")";
|
---|
195 | }
|
---|
196 |
|
---|
197 | protected void addQueryElem(StringBuffer s, String q, String f, String c)
|
---|
198 | {
|
---|
199 |
|
---|
200 | String combine = "";
|
---|
201 | if (s.length() > 0)
|
---|
202 | {
|
---|
203 | combine = " " + c + " ";
|
---|
204 | }
|
---|
205 | s.append(combine + addFieldInfo(q, f));
|
---|
206 | }
|
---|
207 |
|
---|
208 | /** Lucene/Solr doesn't use these options at the moment */
|
---|
209 | protected String addStemOptions(String query, String stem, String casef, String accent)
|
---|
210 | {
|
---|
211 | return query;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Lucene/Solr does not use internal ids. It just uses hash ids. So we need
|
---|
216 | * to override these methods so no conversion is done.
|
---|
217 | */
|
---|
218 | /** convert indexer internal id to Greenstone oid */
|
---|
219 | protected String internalNum2OID(long docnum)
|
---|
220 | {
|
---|
221 | return Long.toString(docnum);
|
---|
222 | }
|
---|
223 |
|
---|
224 | protected String internalNum2OID(String docnum)
|
---|
225 | {
|
---|
226 | return docnum;
|
---|
227 |
|
---|
228 | }
|
---|
229 | }
|
---|