source: trunk/indexers/lucene-gs/src/org/greenstone/LuceneWrapper/GS2LuceneQuery.java@ 12256

Last change on this file since 12256 was 12256, checked in by mdewsnip, 18 years ago

Now returns query terms and their frequencies.

  • Property svn:keywords set to Author Date Id Revision
File size: 2.5 KB
Line 
1/**
2 *
3 * @author [email protected]
4 * @author [email protected]
5 * @version
6 */
7
8import java.io.BufferedReader;
9import java.io.InputStreamReader;
10import java.util.HashSet;
11import java.util.Iterator;
12
13import org.apache.lucene.analysis.Analyzer;
14import org.apache.lucene.analysis.standard.StandardAnalyzer;
15import org.apache.lucene.document.Document;
16import org.apache.lucene.index.IndexReader;
17import org.apache.lucene.index.Term;
18import org.apache.lucene.queryParser.QueryParser;
19import org.apache.lucene.search.Hits;
20import org.apache.lucene.search.IndexSearcher;
21import org.apache.lucene.search.Query;
22import org.apache.lucene.search.Searcher;
23
24
25public class GS2LuceneQuery
26{
27 public static void main (String args[])
28 {
29 if (args.length == 0) {
30 System.out.println("Usage: GS2LuceneQuery <index directory>");
31 return;
32 }
33
34 try {
35 Searcher searcher = new IndexSearcher(args[0]);
36 Analyzer analyzer = new StandardAnalyzer();
37 IndexReader reader = ((IndexSearcher) searcher).getIndexReader();
38
39 BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
40 while (true) {
41 // Read the query from STDIN
42 String query_string = in.readLine();
43 if (query_string == null || query_string.length() == -1) {
44 break;
45 }
46 System.err.println("**** query = " + query_string);
47
48 // Parse the query and rewrite it into individual terms (eg. for wildcard searches)
49 QueryParser query_parser = new QueryParser("TX", analyzer);
50 Query query = query_parser.parse(query_string);
51 query = query.rewrite(reader);
52
53 // Perform the query
54 Hits hits = searcher.search(query);
55 System.out.println("<ResultSet>");
56 System.out.println(" <QueryString>" + query.toString("TX")+"</QueryString>");
57
58 // Return the list of expanded query terms and their frequencies
59 HashSet terms = new HashSet();
60 query.extractTerms(terms);
61 Iterator iter = terms.iterator();
62 while (iter.hasNext()) {
63 Term term = (Term) iter.next();
64 System.out.println(" <Term freq=\"" + reader.docFreq(term) + "\">" + term.text() + "</Term>");
65 }
66
67 // Return the matching documents
68 System.out.println(" <MatchingDocsInfo num=\"" + hits.length() + "\"/>");
69 for (int i = 0; i < hits.length(); i++) {
70 Document doc = hits.doc(i);
71 String node_id = doc.get("nodeID");
72 System.out.println(" <Match id=\"" + node_id + "\"/>");
73 }
74
75 System.out.println("</ResultSet>");
76 }
77
78 searcher.close();
79 }
80 catch (Exception exception) {
81 exception.printStackTrace();
82 }
83 }
84}
Note: See TracBrowser for help on using the repository browser.