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

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

Now uses the t variable to control whether a "some" or "all" search is done with Lucene. Many thanks to John Thompson and DL Consulting Ltd.

  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1/**
2 *
3 * @author [email protected]
4 * @author [email protected]
5 * @version
6 */
7
8package org.nzdl.gsdl.LuceneWrap;
9
10
11import java.io.BufferedReader;
12import java.io.InputStreamReader;
13import java.util.HashSet;
14import java.util.Iterator;
15
16import org.apache.lucene.analysis.Analyzer;
17import org.apache.lucene.analysis.standard.StandardAnalyzer;
18import org.apache.lucene.document.Document;
19import org.apache.lucene.index.IndexReader;
20import org.apache.lucene.index.Term;
21import org.apache.lucene.queryParser.QueryParser;
22import org.apache.lucene.search.Hits;
23import org.apache.lucene.search.IndexSearcher;
24import org.apache.lucene.search.Query;
25import org.apache.lucene.search.Searcher;
26import org.apache.lucene.search.Sort;
27
28
29public class GS2LuceneQuery
30{
31 public static void main (String args[])
32 {
33 if (args.length == 0) {
34 System.out.println("Usage: GS2LuceneQuery <index directory> (<sort field>)");
35 return;
36 }
37
38 try {
39 Searcher searcher = new IndexSearcher(args[0]);
40 Sort sorter = new Sort();
41 // New code to allow the default conjunction operator to be
42 // definable
43 String default_conjuction_operator = "OR";
44 for (int i = 1; i < args.length; i++)
45 {
46 if (args[i].equals("-sort"))
47 {
48 i++;
49 sorter = new Sort(args[i]);
50 }
51 if (args[i].equals("-dco"))
52 {
53 i++;
54 default_conjuction_operator = args[i];
55 }
56 }
57
58 Analyzer analyzer = new StandardAnalyzer();
59 IndexReader reader = ((IndexSearcher) searcher).getIndexReader();
60
61 BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
62 while (true) {
63 // Read the query from STDIN
64 String query_string = in.readLine();
65 if (query_string == null || query_string.length() == -1) {
66 break;
67 }
68 System.err.println("**** query = " + query_string);
69
70 // Parse the query and rewrite it into individual terms (eg. for wildcard searches)
71 QueryParser query_parser = new QueryParser("TX", analyzer);
72 // Set the default conjuction operator
73 System.err.println("**** DCO = " + default_conjuction_operator);
74 if (default_conjuction_operator.equals("AND"))
75 {
76 query_parser.setDefaultOperator(query_parser.AND_OPERATOR);
77 }
78 // Otherwise its OR
79
80 Query query = query_parser.parse(query_string);
81 query = query.rewrite(reader);
82
83 // Perform the query
84 Hits hits = searcher.search(query, sorter);
85 System.out.println("<ResultSet>");
86 System.out.println(" <QueryString>" + query_string + "</QueryString>");
87
88 // Return the list of expanded query terms and their frequencies
89 HashSet terms = new HashSet();
90 query.extractTerms(terms);
91 System.out.println(" <QueryTermsInfo num=\"" + terms.size() + "\"/>");
92 Iterator iter = terms.iterator();
93 while (iter.hasNext()) {
94 Term term = (Term) iter.next();
95 //System.out.println(" <Term value=\"" + term.text() + "\" freq=\"" + reader.docFreq(term) + "\"/>");
96 System.out.println(" <Term value=\"" + term.text() + "\" freq=\"" + reader.docFreq(term) + "\" field=\"" + term.field() + "\"/>");
97 }
98
99 // Return the matching documents
100 System.out.println(" <MatchingDocsInfo num=\"" + hits.length() + "\"/>");
101 for (int i = 0; i < hits.length(); i++) {
102 Document doc = hits.doc(i);
103 String node_id = doc.get("nodeID");
104 System.out.println(" <Match id=\"" + node_id + "\"/>");
105 }
106
107 System.out.println("</ResultSet>");
108 }
109
110 searcher.close();
111 }
112 catch (Exception exception) {
113 exception.printStackTrace();
114 }
115 }
116}
Note: See TracBrowser for help on using the repository browser.