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 org.greenstone.gsdl3.util.*;
|
---|
23 |
|
---|
24 | // XML classes
|
---|
25 | import org.w3c.dom.Element;
|
---|
26 | import org.w3c.dom.NodeList;
|
---|
27 |
|
---|
28 | // java classes
|
---|
29 | import java.util.ArrayList;
|
---|
30 | import java.util.HashMap;
|
---|
31 |
|
---|
32 | // Logging
|
---|
33 | import org.apache.log4j.Logger;
|
---|
34 |
|
---|
35 | public class GS2LuceneSearch
|
---|
36 | extends AbstractGS2FieldSearch
|
---|
37 | {
|
---|
38 |
|
---|
39 | // TODO: lucene query object
|
---|
40 |
|
---|
41 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2LuceneSearch.class.getName());
|
---|
42 |
|
---|
43 | public GS2LuceneSearch()
|
---|
44 | {
|
---|
45 | // TODO: create new query object
|
---|
46 |
|
---|
47 | // Lucene uses double operators, not single
|
---|
48 | AND_OPERATOR = "&&";
|
---|
49 | OR_OPERATOR = "||";
|
---|
50 |
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void cleanUp() {
|
---|
54 | super.cleanUp();
|
---|
55 | // TODO: clean up query object
|
---|
56 | }
|
---|
57 |
|
---|
58 | /** configure this service */
|
---|
59 | public boolean configure(Element info, Element extra_info)
|
---|
60 | {
|
---|
61 | if (!super.configure(info, extra_info)){
|
---|
62 | return false;
|
---|
63 | }
|
---|
64 |
|
---|
65 | // Lucene doesn't do case folding or stemming or accent folding at the
|
---|
66 | // moment
|
---|
67 | does_case = false;
|
---|
68 | does_stem = false;
|
---|
69 | does_accent = false;
|
---|
70 |
|
---|
71 | // TODO: configure the query object based on info from config file
|
---|
72 |
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** add in the lucene specific params to TextQuery */
|
---|
77 | protected void addCustomQueryParams(Element param_list, String lang)
|
---|
78 | {
|
---|
79 | super.addCustomQueryParams(param_list, lang);
|
---|
80 | /** lucenes rank param is based on fields, not ranked/not */
|
---|
81 | createParameter(RANK_PARAM, param_list, lang);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** create a param and add to the list */
|
---|
85 | /** we override this to do a special rank param */
|
---|
86 | protected void createParameter(String name, Element param_list, String lang)
|
---|
87 | {
|
---|
88 | Element param = null;
|
---|
89 | if (name.equals(RANK_PARAM)) {
|
---|
90 | // get the fields
|
---|
91 | ArrayList fields = new ArrayList();
|
---|
92 | fields.add("rank");
|
---|
93 | ArrayList field_names = new ArrayList();
|
---|
94 | field_names.add(getTextString("param.sortBy.rank", lang));
|
---|
95 | getSortByIndexData(fields, field_names, lang);
|
---|
96 |
|
---|
97 | param = GSXML.createParameterDescription2(this.doc, name, getTextString("param."+name, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, (String)fields.get(0), fields, field_names );
|
---|
98 | }
|
---|
99 | if (param != null) {
|
---|
100 | param_list.appendChild(param);
|
---|
101 | } else {
|
---|
102 | super.createParameter(name, param_list, lang);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | protected void getSortByIndexData(ArrayList index_ids, ArrayList index_names, String lang){
|
---|
107 | // the field list - read from config file
|
---|
108 | Element field_list = (Element)GSXML.getChildByTagName(this.config_info, GSXML.FIELD_ELEM+GSXML.LIST_MODIFIER);
|
---|
109 | NodeList fields = field_list.getElementsByTagName(GSXML.FIELD_ELEM);
|
---|
110 | for (int i=0; i< fields.getLength();i++) {
|
---|
111 | String shortname = ((Element)fields.item(i)).getAttribute(GSXML.SHORTNAME_ATT);
|
---|
112 | String name = ((Element)fields.item(i)).getAttribute(GSXML.NAME_ATT);
|
---|
113 | if (name.equals("")) {
|
---|
114 | // no name, ignore
|
---|
115 | continue;
|
---|
116 | }
|
---|
117 | // TODO change field so that name is the id, and full metadata name is somthing else
|
---|
118 | if (shortname.equals("")) {
|
---|
119 | shortname = name;
|
---|
120 | }
|
---|
121 | if (shortname.equals("ZZ")) {
|
---|
122 | // ZZ is a fake index
|
---|
123 | continue;
|
---|
124 | }
|
---|
125 | index_ids.add("by"+shortname);
|
---|
126 | // should these be changed to a text element based on lang?
|
---|
127 | // or is the name of a metadata element eg dc:Title its
|
---|
128 | // name in all langs
|
---|
129 | index_names.add(name);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | /** methods to handle actually doing the query */
|
---|
134 | /** do any initialisation of the query object */
|
---|
135 | protected boolean setUpQueryer(HashMap params) {
|
---|
136 | // TODO
|
---|
137 | return true;
|
---|
138 | }
|
---|
139 | /** do the query */
|
---|
140 | protected Object runQuery(String query) {
|
---|
141 | // TODO
|
---|
142 | return null;
|
---|
143 | }
|
---|
144 | /** get the total number of docs that match */
|
---|
145 | protected long numDocsMatched(Object query_result) {
|
---|
146 | // TODO
|
---|
147 | return 0;
|
---|
148 | }
|
---|
149 | /** get the list of doc ids */
|
---|
150 | protected String [] getDocIDs(Object query_result) {
|
---|
151 | // TODO
|
---|
152 | return null;
|
---|
153 | }
|
---|
154 | /** get the list of doc ranks */
|
---|
155 | protected String [] getDocRanks(Object query_result) {
|
---|
156 | // TODO
|
---|
157 | return null;
|
---|
158 | }
|
---|
159 | /** add in term info if available */
|
---|
160 | protected boolean addTermInfo(Element term_list, HashMap params,
|
---|
161 | Object query_result) {
|
---|
162 | // TODO
|
---|
163 | return true;
|
---|
164 | }
|
---|
165 |
|
---|
166 | protected String addFieldInfo(String query, String field) {
|
---|
167 | if (field.equals("") || field.equals("ZZ")) {
|
---|
168 | return query;
|
---|
169 | }
|
---|
170 | return field+":("+query+")";
|
---|
171 | }
|
---|
172 |
|
---|
173 | protected void addQueryElem(StringBuffer s, String q, String f, String c) {
|
---|
174 |
|
---|
175 | String combine="";
|
---|
176 | if (s.length()>0) {
|
---|
177 | combine = " "+c+" ";
|
---|
178 | }
|
---|
179 | s.append(combine + addFieldInfo(q,f));
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** Lucene doesn't use these options at the moment */
|
---|
183 | protected String addStemOptions(String query, String stem,
|
---|
184 | String casef, String accent) {
|
---|
185 | return query;
|
---|
186 | }
|
---|
187 | }
|
---|