1 | /*
|
---|
2 | * search.java
|
---|
3 | *
|
---|
4 | * Created on 25 February 2003, 02:25
|
---|
5 | */
|
---|
6 |
|
---|
7 |
|
---|
8 | /**
|
---|
9 | *
|
---|
10 | * @author [email protected]
|
---|
11 | * @version
|
---|
12 | */
|
---|
13 |
|
---|
14 | import java.io.IOException;
|
---|
15 | import java.io.BufferedReader;
|
---|
16 | import java.io.InputStreamReader;
|
---|
17 |
|
---|
18 | import org.apache.lucene.analysis.Analyzer;
|
---|
19 | import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
---|
20 | import org.apache.lucene.document.Document;
|
---|
21 | import org.apache.lucene.search.Searcher;
|
---|
22 | import org.apache.lucene.search.IndexSearcher;
|
---|
23 | import org.apache.lucene.search.Query;
|
---|
24 | import org.apache.lucene.search.Hits;
|
---|
25 | import org.apache.lucene.queryParser.QueryParser;
|
---|
26 | import org.apache.lucene.search.TermQuery;
|
---|
27 | import org.apache.lucene.index.Term;
|
---|
28 |
|
---|
29 | public class Search {
|
---|
30 |
|
---|
31 | public static void main (String args[]) {
|
---|
32 |
|
---|
33 | if (args.length == 0) {
|
---|
34 | System.out.println("Usage: Search <index directory>");
|
---|
35 | return;
|
---|
36 | }
|
---|
37 | try {
|
---|
38 | Searcher searcher = new IndexSearcher(args[0]);
|
---|
39 | Analyzer analyzer = new StandardAnalyzer();
|
---|
40 |
|
---|
41 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
|
---|
42 | while (true) {
|
---|
43 | System.out.print("Query: ");
|
---|
44 | String line = in.readLine();
|
---|
45 |
|
---|
46 | if (line.length() == -1)
|
---|
47 | break;
|
---|
48 |
|
---|
49 | Term term = new Term("content",line);
|
---|
50 |
|
---|
51 | Query query = new TermQuery(term);
|
---|
52 | System.out.println("Searching for: " + query.toString("content"));
|
---|
53 |
|
---|
54 | Hits hits = searcher.search(query);
|
---|
55 | System.out.println(hits.length() + " total matching documents");
|
---|
56 |
|
---|
57 | final int HITS_PER_PAGE=10;
|
---|
58 |
|
---|
59 | for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
|
---|
60 | int end = Math.min(hits.length(), start + HITS_PER_PAGE);
|
---|
61 | for (int i = start; i < end; i++) {
|
---|
62 | Document doc = hits.doc(i);
|
---|
63 | String node_id= doc.get("nodeID");
|
---|
64 | System.out.println(i + ". ID: "+node_id);
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (hits.length() > end) {
|
---|
68 | System.out.print("more (y/n) ? ");
|
---|
69 | line = in.readLine();
|
---|
70 | if (line.length() == 0 || line.charAt(0) == 'n')
|
---|
71 | break;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | searcher.close();
|
---|
78 | }
|
---|
79 | catch (Exception e) {
|
---|
80 | System.out.println(" caught a " + e.getClass() +
|
---|
81 | "\n with message: " + e.getMessage());
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|