1 | /*
|
---|
2 | * GS2MGSearch.java
|
---|
3 | * Copyright (C) 2002 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 | package org.greenstone.gsdl3.service;
|
---|
19 |
|
---|
20 |
|
---|
21 | // Greenstone classes
|
---|
22 | import org.greenstone.mg.*;
|
---|
23 | import org.greenstone.gsdl3.util.*;
|
---|
24 |
|
---|
25 | // XML classes
|
---|
26 | import org.w3c.dom.Element;
|
---|
27 | import org.w3c.dom.Node;
|
---|
28 | import org.w3c.dom.NodeList;
|
---|
29 |
|
---|
30 | // General Java classes
|
---|
31 | import java.io.File;
|
---|
32 | import java.util.HashMap;
|
---|
33 | import java.util.Iterator;
|
---|
34 | import java.util.Map;
|
---|
35 | import java.util.Set;
|
---|
36 | import java.util.Vector;
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | *
|
---|
41 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
42 | * @author <a href="mailto:[email protected]">Michael Dewsnip</a>
|
---|
43 | * @version $Revision: 5188 $
|
---|
44 | */
|
---|
45 |
|
---|
46 | public class GS2MGSearch
|
---|
47 | extends GS2Search {
|
---|
48 |
|
---|
49 |
|
---|
50 | private MGWrapper mg_src = null;
|
---|
51 |
|
---|
52 |
|
---|
53 | /** constructor */
|
---|
54 | public GS2MGSearch()
|
---|
55 | {
|
---|
56 | this.mg_src = new MGWrapper();
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | /** this creates all teh params and appends them to param_list.
|
---|
61 | * if display=true it creates the text strings version
|
---|
62 | * otherwise it creates the description version
|
---|
63 | */
|
---|
64 | protected boolean createTextQueryParamList(Element param_list,
|
---|
65 | String lang)
|
---|
66 | {
|
---|
67 | // the order they are specified here is the order they appear on
|
---|
68 | // the query form
|
---|
69 | createParameter(INDEX_PARAM, param_list, lang);
|
---|
70 | createParameter(CASE_PARAM, param_list, lang);
|
---|
71 | createParameter(STEM_PARAM, param_list, lang);
|
---|
72 | createParameter(MATCH_PARAM, param_list, lang);
|
---|
73 | createParameter(MAXDOCS_PARAM, param_list, lang);
|
---|
74 | createParameter(QUERY_PARAM, param_list, lang);
|
---|
75 |
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | /** Process a text query */
|
---|
81 | protected Element processTextQuery(Element request)
|
---|
82 | {
|
---|
83 | // Create a new (empty) result message
|
---|
84 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
85 | result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE);
|
---|
86 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
87 |
|
---|
88 | // Get the parameters of the request
|
---|
89 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
90 | if (param_list == null) {
|
---|
91 | System.err.println("Error: TextQuery request had no paramList.");
|
---|
92 | return result; // Return the empty result
|
---|
93 | }
|
---|
94 |
|
---|
95 | // Process the request parameters
|
---|
96 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
97 |
|
---|
98 | // Make sure a query has been specified
|
---|
99 | String query = (String) params.get(QUERY_PARAM);
|
---|
100 | if (query == null || query.equals("")) {
|
---|
101 | return result; // Return the empty result
|
---|
102 | }
|
---|
103 |
|
---|
104 | // If an index hasn't been specified, use the default
|
---|
105 | String index = (String) params.get(INDEX_PARAM);
|
---|
106 | if (index == null) {
|
---|
107 | index = this.default_index;
|
---|
108 | }
|
---|
109 |
|
---|
110 | // The location of the MG index and text files
|
---|
111 | String basedir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) +
|
---|
112 | File.separatorChar; // Needed for MG
|
---|
113 | String textdir = GSFile.collectionTextPath(this.cluster_name);
|
---|
114 | String indexpath = GSFile.collectionIndexPath(this.cluster_name, index);
|
---|
115 | this.mg_src.setIndex(indexpath);
|
---|
116 |
|
---|
117 | // set the mg query parameters to the values the user has specified
|
---|
118 | setStandardQueryParams(params);
|
---|
119 | this.mg_src.runQuery(basedir, textdir, query);
|
---|
120 | MGQueryResult mqr = this.mg_src.getQueryResult();
|
---|
121 | long totalDocs = mqr.getTotalDocs();
|
---|
122 |
|
---|
123 | // Get the docnums out, and convert to HASH ids
|
---|
124 | Vector docs = mqr.getDocs();
|
---|
125 | if (docs.size() == 0) {
|
---|
126 | System.err.println("GS2MGSearch: Warning: No results found...\n");
|
---|
127 | }
|
---|
128 |
|
---|
129 | // Create a metadata list to store information about the query results
|
---|
130 | Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
131 | result.appendChild(metadata_list);
|
---|
132 |
|
---|
133 | // Add a metadata element specifying the number of matching documents
|
---|
134 | // because teh total number is just the number returned, use numDocsReturned, not numDocsMatched
|
---|
135 | GSXML.addMetadata(this.doc, metadata_list, "numDocsReturned", ""+totalDocs);
|
---|
136 | // Create a document list to store the matching documents, and add them
|
---|
137 | Element document_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
138 | result.appendChild(document_list);
|
---|
139 | for (int d = 0; d < docs.size(); d++) {
|
---|
140 | long docnum = ((MGDocInfo) docs.elementAt(d)).num_;
|
---|
141 | String doc_id = this.gdbm_src.docnum2Oid(docnum);
|
---|
142 | Element doc_node = createDocumentNodeElement(doc_id);
|
---|
143 | document_list.appendChild(doc_node);
|
---|
144 | }
|
---|
145 |
|
---|
146 | // Create a term list to store the term information, and add it
|
---|
147 | Element term_list = this.doc.createElement(GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
|
---|
148 | result.appendChild(term_list);
|
---|
149 | Vector terms = mqr.getTerms();
|
---|
150 | for (int t = 0; t < terms.size(); t++) {
|
---|
151 | MGTermInfo term_info = (MGTermInfo) terms.get(t);
|
---|
152 |
|
---|
153 | String term = term_info.term_;
|
---|
154 | int stem_method = term_info.stem_method_;
|
---|
155 | Vector equiv_terms = term_info.equiv_terms_;
|
---|
156 |
|
---|
157 | Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
|
---|
158 | term_elem.setAttribute(GSXML.NAME_ATT, term);
|
---|
159 | term_elem.setAttribute(STEM_ATT, "" + stem_method);
|
---|
160 |
|
---|
161 | Element equiv_term_list = this.doc.createElement(EQUIV_TERM_ELEM+GSXML.LIST_MODIFIER);
|
---|
162 | term_elem.appendChild(equiv_term_list);
|
---|
163 |
|
---|
164 | long total_term_freq = 0;
|
---|
165 | for (int et = 0; et < equiv_terms.size(); et++) {
|
---|
166 | MGEquivTermInfo equiv_term_info = (MGEquivTermInfo) equiv_terms.get(et);
|
---|
167 |
|
---|
168 | Element equiv_term_elem = this.doc.createElement(GSXML.TERM_ELEM);
|
---|
169 | equiv_term_elem.setAttribute(GSXML.NAME_ATT, equiv_term_info.term_);
|
---|
170 | equiv_term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + equiv_term_info.match_docs_);
|
---|
171 | equiv_term_elem.setAttribute(FREQ_ATT, "" + equiv_term_info.term_freq_);
|
---|
172 | equiv_term_list.appendChild(equiv_term_elem);
|
---|
173 |
|
---|
174 | total_term_freq += equiv_term_info.term_freq_;
|
---|
175 | }
|
---|
176 |
|
---|
177 | term_elem.setAttribute(FREQ_ATT, "" + total_term_freq);
|
---|
178 | term_list.appendChild(term_elem);
|
---|
179 | }
|
---|
180 |
|
---|
181 | return result;
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | // should probably use a list rather than map
|
---|
186 | protected boolean setStandardQueryParams(HashMap params)
|
---|
187 | {
|
---|
188 | // set the default ones
|
---|
189 | this.mg_src.setReturnTerms(true);
|
---|
190 | Set entries = params.entrySet();
|
---|
191 | Iterator i = entries.iterator();
|
---|
192 | while (i.hasNext()) {
|
---|
193 | Map.Entry m = (Map.Entry)i.next();
|
---|
194 | String name = (String)m.getKey();
|
---|
195 | String value = (String)m.getValue();
|
---|
196 |
|
---|
197 | if (name.equals(CASE_PARAM)) {
|
---|
198 | boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
|
---|
199 | this.mg_src.setCase(val);
|
---|
200 | }
|
---|
201 | else if (name.equals(STEM_PARAM)) {
|
---|
202 | boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
|
---|
203 | this.mg_src.setStem(val);
|
---|
204 | }
|
---|
205 | else if (name.equals(MATCH_PARAM)) {
|
---|
206 | int mode = (value.equals(MATCH_PARAM_ALL) ? 1 : 0);
|
---|
207 | this.mg_src.setMatchMode(mode);
|
---|
208 | }
|
---|
209 | else if (name.equals(MAXDOCS_PARAM)) {
|
---|
210 | int docs = Integer.parseInt(value);
|
---|
211 | this.mg_src.setMaxDocs(docs);
|
---|
212 | } // ignore any others
|
---|
213 | }
|
---|
214 | return true;
|
---|
215 | }
|
---|
216 | }
|
---|