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