Source code: com/telefonicasoluciones/search/server/HLSearch.java
1 package com.telefonicasoluciones.search.server;
2
3 /**
4 * Realiza búsquedas sobre os índices.
5 * Creation date: (17/04/2001 9:57:14)
6 *
7 * @author: Ricardo Lorenzo
8 */
9 import org.apache.lucene.store.Directory;
10 import org.apache.lucene.store.FSDirectory;
11 import org.apache.lucene.analysis.Analyzer;
12 import org.apache.lucene.analysis.SimpleAnalyzer;
13 import org.apache.lucene.document.Field;
14 import org.apache.lucene.search.*;
15 import org.apache.lucene.queryParser.QueryParser;
16 import com.telefonicasoluciones.search.server.util.*;
17 import java.io.*;
18 import java.util.*;
19
20
21 public class HLSearch {
22 private HLHandler hls;
23 private Directory indexDirectory;
24 private int totalMatches;
25 private ArrayList result = new ArrayList();
26 /**
27 * Construye un objeto HLSearch, recibe un HLConfig.
28 */
29 public HLSearch(HLHandler newHls) throws IOException {
30 hls = newHls;
31 indexDirectory = FSDirectory.getDirectory(hls.getIndexDirectory(), false);
32 }
33 /**
34 * Retorna una Enumeración de resultados.
35 * Creation date: (14/11/2001 8:29:30)
36 * @return java.util.Vector
37 */
38 public java.util.ArrayList getResults() {
39 return result;
40 }
41 /**
42 * Retorna el total de resultados encontrados.
43 * Creation date: (19/11/2001 17:35:28)
44 * @return int
45 */
46 public int getTotalMatches() {
47 return totalMatches;
48 }
49 /**
50 * Busca dentro de los indices.
51 * Creation date: (19/11/2001 17:35:28)
52 * @param java.lang.String key
53 * @param java.lang.String match
54 */
55 public void search(String key, String match) throws IOException {
56 try {
57 Searcher searcher = new IndexSearcher(this.indexDirectory);
58 Analyzer analyzer = new HLAnalyzer();
59
60 Query query = QueryParser.parse(match, key, analyzer);
61 hls.writeSearchLog(" ["+match+"] "+new java.util.Date().toString());
62
63 Hits hits = searcher.search(query);
64 totalMatches = hits.length();
65 for (int i=0; i<this.totalMatches; i++) {
66 Hashtable fields = new Hashtable();
67 fields.put("score", new Float(hits.score(i)*100));
68 Enumeration listFields = hits.doc(i).fields();
69 while(listFields.hasMoreElements()) {
70 Field tempField = (Field) listFields.nextElement();
71 fields.put(tempField.name(),tempField.stringValue());
72 }
73 result.add(i,fields);
74 }
75 searcher.close();
76 } catch (Exception e) {
77 hls.writeErrorLog("Unknown error in search ["+e.getMessage()+"]");
78 }
79 }
80 /**
81 * Busca dentro de los indices.
82 * Creation date: (19/11/2001 17:35:28)
83 * @param java.lang.String[] key
84 * @param java.lang.String match
85 */
86 public void search(String[] key, String match) throws IOException {
87 try {
88 Searcher searcher = new IndexSearcher(this.indexDirectory);
89 Analyzer analyzer = new HLAnalyzer();
90
91 Query query = MultiFieldQueryParser.parse(match, key, analyzer);
92 hls.writeSearchLog(" ["+match+"] "+new java.util.Date().toString());
93
94 Hits hits = searcher.search(query);
95 totalMatches = hits.length();
96 for (int i=0; i<this.totalMatches; i++) {
97 Hashtable fields = new Hashtable();
98 fields.put("score", new Float(hits.score(i)*100));
99 Enumeration listFields = hits.doc(i).fields();
100 while(listFields.hasMoreElements()) {
101 Field tempField = (Field) listFields.nextElement();
102 fields.put(tempField.name(),tempField.stringValue());
103 }
104 result.add(i,fields);
105 }
106 searcher.close();
107 } catch (Exception e) {
108 hls.writeErrorLog("Unknown error in search ["+e.getMessage()+"]");
109 }
110 }
111 /**
112 * Busca dentro de los indices.
113 * Creation date: (19/11/2001 17:35:28)
114 * @param java.lang.String[] key
115 * @param java.lang.String match
116 */
117 public void search(HashMap terms) throws IOException {
118 try {
119 Searcher searcher = new IndexSearcher(this.indexDirectory);
120 Analyzer analyzer = new HLAnalyzer();
121
122 String match = new String();
123 Object keys[] = terms.keySet().toArray();
124 for (int i = keys.length; --i >= 0 ; ) {
125 match += " " + (String) keys[i] + ":" + (String) terms.get(keys[i]);
126 }
127 Query query = MultiFieldQueryParser.parse(terms, analyzer);
128 hls.writeSearchLog(" [" + match + " ] " + new java.util.Date().toString());
129
130 Hits hits = searcher.search(query);
131 totalMatches = hits.length();
132 for (int i=0; i<this.totalMatches; i++) {
133 Hashtable fields = new Hashtable();
134 fields.put("score", new Float(hits.score(i)*100));
135 Enumeration listFields = hits.doc(i).fields();
136 while(listFields.hasMoreElements()) {
137 Field tempField = (Field) listFields.nextElement();
138 fields.put(tempField.name(),tempField.stringValue());
139 }
140 result.add(i,fields);
141 }
142 searcher.close();
143 } catch (Exception e) {
144 hls.writeErrorLog("Unknown error in search ["+e.getMessage()+"]");
145 }
146 }
147 }