source: trunk/gsdl3/web/sites/localsite/collect/gberg/java/Search.java@ 5956

Last change on this file since 5956 was 5956, checked in by kjdon, 20 years ago

new files for making a simple lucene collection from xml documents

  • Property svn:keywords set to Author Date Id Revision
File size: 2.1 KB
Line 
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
14import java.io.IOException;
15import java.io.BufferedReader;
16import java.io.InputStreamReader;
17
18import org.apache.lucene.analysis.Analyzer;
19import org.apache.lucene.analysis.standard.StandardAnalyzer;
20import org.apache.lucene.document.Document;
21import org.apache.lucene.search.Searcher;
22import org.apache.lucene.search.IndexSearcher;
23import org.apache.lucene.search.Query;
24import org.apache.lucene.search.Hits;
25import org.apache.lucene.queryParser.QueryParser;
26import org.apache.lucene.search.TermQuery;
27import org.apache.lucene.index.Term;
28
29public 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}
Note: See TracBrowser for help on using the repository browser.