- Timestamp:
- 2007-01-11T14:55:04+13:00 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS2LuceneSearch.java
r13270 r13576 25 25 import org.w3c.dom.Element; 26 26 import org.w3c.dom.NodeList; 27 27 import org.w3c.dom.Document; 28 28 // java classes 29 29 import java.util.ArrayList; 30 30 import java.util.HashMap; 31 import java.io.File; 32 import java.util.Iterator; 33 import java.util.Set; 34 import java.util.Map; 35 import java.util.Vector; 31 36 32 37 // Logging 33 38 import org.apache.log4j.Logger; 39 40 import org.nzdl.gsdl.LuceneWrap.GS2LuceneQuery; 41 import org.nzdl.gsdl.LuceneWrap.LuceneQueryResult; 34 42 35 43 public class GS2LuceneSearch 36 44 extends AbstractGS2FieldSearch 37 45 { 38 39 // TODO: lucene query object 40 46 protected static final String RANK_PARAM_RANK_VALUE = "rank"; 47 41 48 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.GS2LuceneSearch.class.getName()); 42 49 50 private GS2LuceneQuery lucene_src=null; 51 43 52 public GS2LuceneSearch() 44 53 { 45 // TODO: create new query object 46 54 this.lucene_src = new GS2LuceneQuery(); 47 55 // Lucene uses double operators, not single 48 56 AND_OPERATOR = "&&"; 49 57 OR_OPERATOR = "||"; 50 58 51 } 52 59 does_paging = true; 60 does_chunking = true; 61 } 62 53 63 public void cleanUp() { 54 64 super.cleanUp(); 55 // TODO: clean up query object56 } 57 58 65 this.lucene_src.cleanUp(); 66 } 67 68 /** configure this service */ 59 69 public boolean configure(Element info, Element extra_info) 60 70 { … … 68 78 does_stem = false; 69 79 does_accent = false; 70 71 // TODO: configure the query object based on info from config file72 80 73 81 return true; 74 82 } 75 83 76 84 /** add in the lucene specific params to TextQuery */ 77 85 protected void addCustomQueryParams(Element param_list, String lang) … … 80 88 /** lucenes rank param is based on fields, not ranked/not */ 81 89 createParameter(RANK_PARAM, param_list, lang); 82 } 83 90 createParameter(FIELD_ATT, param_list, lang); 91 } 92 84 93 /** create a param and add to the list */ 85 94 /** we override this to do a special rank param */ … … 90 99 // get the fields 91 100 ArrayList fields = new ArrayList(); 92 fields.add( "rank");101 fields.add(RANK_PARAM_RANK_VALUE); 93 102 ArrayList field_names = new ArrayList(); 94 103 field_names.add(getTextString("param.sortBy.rank", lang)); … … 103 112 } 104 113 } 105 114 106 115 protected void getSortByIndexData(ArrayList index_ids, ArrayList index_names, String lang){ 107 116 // the field list - read from config file … … 116 125 } 117 126 // TODO change field so that name is the id, and full metadata name is somthing else 127 118 128 if (shortname.equals("")) { 119 129 shortname = name; 120 130 } 121 if (shortname.equals("ZZ") ) {122 // ZZ is a fake index 131 if (shortname.equals("ZZ") || shortname.equals("TX")) { 132 // ZZ is a fake index, and we don't sort by TX 123 133 continue; 124 134 } … … 132 142 133 143 /** methods to handle actually doing the query */ 144 134 145 /** do any initialisation of the query object */ 135 146 protected boolean setUpQueryer(HashMap params) { 136 // TODO 147 String indexdir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separatorChar + "index"+File.separatorChar; 148 149 String index = "didx"; 150 int maxdocs = 100; 151 int hits_per_page = 20; 152 int start_page = 1; 153 // set up the query params 154 Set entries = params.entrySet(); 155 Iterator i = entries.iterator(); 156 while (i.hasNext()) { 157 Map.Entry m = (Map.Entry)i.next(); 158 String name = (String)m.getKey(); 159 String value = (String)m.getValue(); 160 161 if (name.equals(MAXDOCS_PARAM)&& !value.equals("")) { 162 maxdocs = Integer.parseInt(value); 163 } else if (name.equals(HITS_PER_PAGE_PARAM)) { 164 hits_per_page = Integer.parseInt(value); 165 } else if (name.equals(START_PAGE_PARAM)) { 166 start_page = Integer.parseInt(value); 167 168 } else if (name.equals(MATCH_PARAM)) { 169 if (value.equals(MATCH_PARAM_ALL)) { 170 this.lucene_src.setDefaultConjunctionOperator("AND"); 171 } else{ 172 this.lucene_src.setDefaultConjunctionOperator("OR"); 173 } 174 } else if (name.equals(RANK_PARAM)) { 175 if (value.equals(RANK_PARAM_RANK_VALUE)) { 176 value = null; 177 } 178 this.lucene_src.setSortField(value); 179 } else if (name.equals(LEVEL_PARAM)) { 180 if (value.toUpperCase().equals("SEC")){ 181 index = "sidx"; 182 } 183 else { 184 index = "didx"; 185 } 186 } // ignore any others 187 } 188 // set up start and end results if necessary 189 int start_results = 1; 190 if (start_page != 1) { 191 start_results = ((start_page-1) * hits_per_page) + 1; 192 } 193 int end_results = hits_per_page * start_page; 194 this.lucene_src.setStartResults(start_results); 195 this.lucene_src.setEndResults(end_results); 196 197 this.lucene_src.setIndexDir(indexdir+index); 198 this.lucene_src.initialise(); 137 199 return true; 138 200 } 139 201 /** do the query */ 140 202 protected Object runQuery(String query) { 141 // TODO 203 try { 204 LuceneQueryResult lqr=this.lucene_src.runQuery(query); 205 return lqr; 206 } catch (Exception e) { 207 logger.error ("exception happened in run query: ", e); 208 } 209 142 210 return null; 143 211 } 144 212 /** get the total number of docs that match */ 145 213 protected long numDocsMatched(Object query_result) { 146 // TODO 147 return 0; 214 return ((LuceneQueryResult)query_result).getTotalDocs(); 215 148 216 } 149 217 /** get the list of doc ids */ 150 218 protected String [] getDocIDs(Object query_result) { 151 // TODO 152 return null; 219 Vector docs = ((LuceneQueryResult)query_result).getDocs(); 220 String [] doc_nums = new String [docs.size()]; 221 for (int d = 0; d < docs.size(); d++) { 222 String doc_num = Long.toString(((LuceneQueryResult.DocInfo) docs.elementAt(d)).num_); 223 doc_nums[d] = doc_num; 224 } 225 return doc_nums; 153 226 } 154 227 /** get the list of doc ranks */ 155 228 protected String [] getDocRanks(Object query_result) { 156 // TODO 157 return null; 229 Vector docs = ((LuceneQueryResult)query_result).getDocs(); 230 String [] doc_ranks = new String [docs.size()]; 231 for (int d = 0; d < docs.size(); d++) { 232 doc_ranks[d] = Float.toString(((LuceneQueryResult.DocInfo) docs.elementAt(d)).rank_); 233 } 234 return doc_ranks; 158 235 } 159 236 /** add in term info if available */ 160 237 protected boolean addTermInfo(Element term_list, HashMap params, 161 238 Object query_result) { 162 // TODO 163 return true; 164 } 165 239 String query_level = (String)params.get(LEVEL_PARAM); // the current query level 240 241 Vector terms = ((LuceneQueryResult)query_result).getTerms(); 242 for (int t = 0; t < terms.size(); t++) { 243 LuceneQueryResult.TermInfo term_info = (LuceneQueryResult.TermInfo) terms.get(t); 244 245 Element term_elem = this.doc.createElement(GSXML.TERM_ELEM); 246 term_elem.setAttribute(GSXML.NAME_ATT, term_info.term_); 247 term_elem.setAttribute(FREQ_ATT, "" + term_info.term_freq_); 248 term_elem.setAttribute(NUM_DOCS_MATCH_ATT, "" + term_info.match_docs_); 249 term_elem.setAttribute(FIELD_ATT, term_info.field_); 250 term_list.appendChild(term_elem); 251 } 252 return true; 253 } 254 166 255 protected String addFieldInfo(String query, String field) { 167 256 if (field.equals("") || field.equals("ZZ")) { … … 170 259 return field+":("+query+")"; 171 260 } 172 261 173 262 protected void addQueryElem(StringBuffer s, String q, String f, String c) { 174 263 175 264 String combine=""; 176 265 if (s.length()>0) { … … 179 268 s.append(combine + addFieldInfo(q,f)); 180 269 } 181 270 182 271 /** Lucene doesn't use these options at the moment */ 183 272 protected String addStemOptions(String query, String stem,
Note:
See TracChangeset
for help on using the changeset viewer.