source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS3MGSearch.java@ 8439

Last change on this file since 8439 was 8439, checked in by kjdon, 20 years ago

when creating doc nodes, now we choose hierarchy or simple based on whether there is a section structure in the database. we used to always change doc ids that matched the whole structure to be the first section id, eg 20041102:1 to 20041102:1-1. this is now only done for hierarchical docs, not for simple docs.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
1/*
2 * GS3MGSearch.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 */
18package org.greenstone.gsdl3.service;
19
20
21// Greenstone classes
22import org.greenstone.mg.*;
23import org.greenstone.gsdl3.util.*;
24
25// XML classes
26import org.w3c.dom.Element;
27import org.w3c.dom.Node;
28import org.w3c.dom.NodeList;
29
30// General Java classes
31import java.io.File;
32import java.util.HashMap;
33import java.util.Iterator;
34import java.util.Map;
35import java.util.Set;
36import 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: 8439 $
44 */
45
46public class GS3MGSearch
47 extends GS3Search {
48
49
50 private MGWrapper mg_src = null;
51
52 /** constructor */
53 public GS3MGSearch()
54 {
55 this.mg_src = new MGWrapper();
56 }
57
58
59 /** this creates all teh params and appends them to param_list.
60 * if display=true it creates the text strings version
61 * otherwise it creates the description version
62 */
63 protected boolean createTextQueryParamList(Element param_list,
64 String lang)
65 {
66 // the order they are specified here is the order they appear on
67 // the query form
68 createParameter(INDEX_PARAM, param_list, lang);
69 createParameter(CASE_PARAM, param_list, lang);
70 createParameter(STEM_PARAM, param_list, lang);
71 createParameter(MATCH_PARAM, param_list, lang);
72 createParameter(MAXDOCS_PARAM, param_list, lang);
73 createParameter(QUERY_PARAM, param_list, lang);
74
75 return true;
76 }
77
78
79 /** Process a text query */
80 protected Element processTextQuery(Element request)
81 {
82 // Create a new (empty) result message
83 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
84 result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE);
85 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
86
87 // Get the parameters of the request
88 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
89 if (param_list == null) {
90 System.err.println("Error: TextQuery request had no paramList.");
91 return result; // Return the empty result
92 }
93
94 // Process the request parameters
95 HashMap params = GSXML.extractParams(param_list, false);
96
97 // Make sure a query has been specified
98 String query = (String) params.get(QUERY_PARAM);
99 if (query == null || query.equals("")) {
100 return result; // Return the empty result
101 }
102
103 // If an index hasn't been specified, use the default
104 String index = (String) params.get(INDEX_PARAM);
105 if (index == null) {
106 index = this.default_index;
107 }
108
109 // The location of the MG index and text files
110 String basedir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar; // Needed for MG
111 String textdir = GSFile.collectionTextPath("index");
112 String indexpath = GSFile.collectionIndexPath("index", index);
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("GS3MGSearch: 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 mg_id = index+"."+docnum;
142 String mg_id = this.base_index_prefix+"."+docnum;
143 String doc_id = database.MGNum2OID(mg_id);
144 // hack !!!
145 if (doc_id.endsWith("-All")) {
146 // get rid of the All bit
147 doc_id = doc_id.substring(0,doc_id.length()-4);
148 //doc_id=doc_id.replaceAll("All","1");
149 }
150
151 Element doc_node = createDocumentNodeElement(doc_id);
152 document_list.appendChild(doc_node);
153 }
154
155 // Create a term list to store the term information, and add it
156 Element term_list = this.doc.createElement(GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
157 result.appendChild(term_list);
158 Vector terms = mqr.getTerms();
159 for (int t = 0; t < terms.size(); t++) {
160 MGTermInfo term_info = (MGTermInfo) terms.get(t);
161
162 String term = term_info.term_;
163 int stem_method = term_info.stem_method_;
164 Vector equiv_terms = term_info.equiv_terms_;
165
166 Element term_elem = this.doc.createElement(GSXML.TERM_ELEM);
167 term_elem.setAttribute(GSXML.NAME_ATT, term);
168 term_elem.setAttribute(STEM_ATT, "" + stem_method);
169
170 Element equiv_term_list = this.doc.createElement(EQUIV_TERM_ELEM+GSXML.LIST_MODIFIER);
171 term_elem.appendChild(equiv_term_list);
172
173 long total_term_freq = 0;
174 for (int et = 0; et < equiv_terms.size(); et++) {
175 MGEquivTermInfo equiv_term_info = (MGEquivTermInfo) equiv_terms.get(et);
176
177 Element equiv_term_elem = this.doc.createElement(GSXML.TERM_ELEM);
178 equiv_term_elem.setAttribute(GSXML.NAME_ATT, equiv_term_info.term_);
179 equiv_term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + equiv_term_info.match_docs_);
180 equiv_term_elem.setAttribute(FREQ_ATT, "" + equiv_term_info.term_freq_);
181 equiv_term_list.appendChild(equiv_term_elem);
182
183 total_term_freq += equiv_term_info.term_freq_;
184 }
185
186 term_elem.setAttribute(FREQ_ATT, "" + total_term_freq);
187 term_list.appendChild(term_elem);
188 }
189
190 return result;
191 }
192
193
194 // should probably use a list rather than map
195 protected boolean setStandardQueryParams(HashMap params)
196 {
197 // set the default ones
198 this.mg_src.setReturnTerms(true);
199 Set entries = params.entrySet();
200 Iterator i = entries.iterator();
201 while (i.hasNext()) {
202 Map.Entry m = (Map.Entry)i.next();
203 String name = (String)m.getKey();
204 String value = (String)m.getValue();
205
206 if (name.equals(CASE_PARAM)) {
207 boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
208 this.mg_src.setCase(val);
209 }
210 else if (name.equals(STEM_PARAM)) {
211 boolean val = (value.equals(BOOLEAN_PARAM_ON) ? true : false);
212 this.mg_src.setStem(val);
213 }
214 else if (name.equals(MATCH_PARAM)) {
215 int mode = (value.equals(MATCH_PARAM_ALL) ? 1 : 0);
216 this.mg_src.setMatchMode(mode);
217 }
218 else if (name.equals(MAXDOCS_PARAM)) {
219 int docs = Integer.parseInt(value);
220 this.mg_src.setMaxDocs(docs);
221 } // ignore any others
222 }
223 return true;
224 }
225}
Note: See TracBrowser for help on using the repository browser.