1 | /*
|
---|
2 | * search.java
|
---|
3 | *
|
---|
4 | * Simple command line program to search the lucene index.
|
---|
5 | * Run like: java -classpath $CLASSPATH:<path-to-gberg>/java Search <index directory>
|
---|
6 | * where the index directory is the index/idx folder.
|
---|
7 | *
|
---|
8 | * Copyright 2003 The New Zealand Digital Library Project
|
---|
9 | *
|
---|
10 | * A component of the Greenstone digital library software
|
---|
11 | * from the New Zealand Digital Library Project at the
|
---|
12 | * University of Waikato, New Zealand.
|
---|
13 | *
|
---|
14 | * This program is free software; you can redistribute it and/or modify
|
---|
15 | * it under the terms of the GNU General Public License as published by
|
---|
16 | * the Free Software Foundation; either version 2 of the License, or
|
---|
17 | * (at your option) any later version.
|
---|
18 | *
|
---|
19 | * This program is distributed in the hope that it will be useful,
|
---|
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
22 | * GNU General Public License for more details.
|
---|
23 | *
|
---|
24 | * You should have received a copy of the GNU General Public License
|
---|
25 | * along with this program; if not, write to the Free Software
|
---|
26 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
27 | *
|
---|
28 | *********************************************************************/
|
---|
29 |
|
---|
30 |
|
---|
31 | import java.io.IOException;
|
---|
32 | import java.io.BufferedReader;
|
---|
33 | import java.io.InputStreamReader;
|
---|
34 | import java.io.File;
|
---|
35 |
|
---|
36 | //import org.apache.lucene.analysis.Analyzer;
|
---|
37 | //import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
---|
38 | import org.apache.lucene.document.Document;
|
---|
39 | import org.apache.lucene.search.Searcher;
|
---|
40 | import org.apache.lucene.search.IndexSearcher;
|
---|
41 | import org.apache.lucene.search.Query;
|
---|
42 | //import org.apache.lucene.search.Hits;
|
---|
43 | import org.apache.lucene.search.TopDocs;
|
---|
44 | import org.apache.lucene.queryParser.QueryParser;
|
---|
45 | import org.apache.lucene.search.TermQuery;
|
---|
46 | import org.apache.lucene.index.Term;
|
---|
47 | import org.apache.lucene.store.Directory;
|
---|
48 | import org.apache.lucene.store.FSDirectory;
|
---|
49 | import org.apache.lucene.index.DirectoryReader;
|
---|
50 | import org.apache.lucene.index.IndexReader;
|
---|
51 | import org.apache.lucene.util.Version;
|
---|
52 |
|
---|
53 | public class Search {
|
---|
54 |
|
---|
55 | public static void main (String args[]) {
|
---|
56 |
|
---|
57 | if (args.length == 0) {
|
---|
58 | System.out.println("Usage: Search <index directory>");
|
---|
59 | return;
|
---|
60 | }
|
---|
61 | try {
|
---|
62 |
|
---|
63 | Directory indexdir_dir = FSDirectory.open(new File(args[0]));
|
---|
64 | IndexReader reader = DirectoryReader.open(indexdir_dir);
|
---|
65 | IndexSearcher searcher = new IndexSearcher(reader);
|
---|
66 |
|
---|
67 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
|
---|
68 | System.out.println("Type .q to quit");
|
---|
69 | while (true) {
|
---|
70 | System.out.print("Query: ");
|
---|
71 | String line = in.readLine();
|
---|
72 | line.trim();
|
---|
73 | if (line.equals(".q"))
|
---|
74 | break;
|
---|
75 |
|
---|
76 | Term term = new Term("content",line);
|
---|
77 |
|
---|
78 | Query query = new TermQuery(term);
|
---|
79 | System.out.println("Searching for: " + query.toString("content"));
|
---|
80 | final int HITS_PER_PAGE=10;
|
---|
81 |
|
---|
82 | TopDocs hits = searcher.search(query, Integer.MAX_VALUE);
|
---|
83 | System.out.println(hits.totalHits + " total matching documents");
|
---|
84 | for (int start = 0; start < hits.totalHits; start += HITS_PER_PAGE) {
|
---|
85 |
|
---|
86 | int end = Math.min(hits.totalHits, start + HITS_PER_PAGE);
|
---|
87 | for (int i = start; i < end; i++) {
|
---|
88 | int docnum = hits.scoreDocs[i].doc;
|
---|
89 | Document doc = reader.document(docnum);
|
---|
90 | String node_id = doc.get("nodeID");
|
---|
91 | System.out.println(i + ". ID: "+node_id);
|
---|
92 | }
|
---|
93 | if (hits.totalHits > end) {
|
---|
94 | System.out.print("more (y/n) ? ");
|
---|
95 | line = in.readLine();
|
---|
96 | if (line.length() == 0 || line.charAt(0) == 'n')
|
---|
97 | break;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | }
|
---|
102 |
|
---|
103 | reader.close();
|
---|
104 | }
|
---|
105 | catch (Exception e) {
|
---|
106 | System.out.println(" caught a " + e.getClass() +
|
---|
107 | "\n with message: " + e.getMessage());
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|