|
3 | 3 | import java.io.IOException;
|
4 | 4 | import java.text.ParseException;
|
5 | 5 |
|
6 |
| -import org.apache.lucene.analysis.standard.StandardAnalyzer; |
7 |
| -import org.apache.lucene.document.Document; |
8 |
| -import org.apache.lucene.document.Field; |
9 |
| -import org.apache.lucene.document.StringField; |
10 |
| -import org.apache.lucene.document.TextField; |
11 |
| -import org.apache.lucene.index.DirectoryReader; |
12 |
| -import org.apache.lucene.index.IndexReader; |
13 |
| -import org.apache.lucene.index.IndexWriter; |
14 |
| -import org.apache.lucene.index.IndexWriterConfig; |
15 |
| -import org.apache.lucene.queryparser.classic.QueryParser; |
16 |
| -import org.apache.lucene.search.IndexSearcher; |
17 |
| -import org.apache.lucene.search.Query; |
18 |
| -import org.apache.lucene.search.ScoreDoc; |
19 |
| -import org.apache.lucene.search.TopScoreDocCollector; |
20 |
| -import org.apache.lucene.store.Directory; |
21 |
| -import org.apache.lucene.store.RAMDirectory; |
22 |
| -import org.apache.lucene.util.Version; |
23 |
| - |
24 | 6 | public class HelloLucene {
|
25 | 7 |
|
26 | 8 | public static void main(String[] args) throws IOException, ParseException {
|
27 | 9 |
|
28 | 10 | // 对于这个简单的例子,我们将从一些字符串中创建一个内存索引。
|
29 | 11 | // 0. Specify the analyzer for tokenizing text.
|
30 | 12 | // The same analyzer should be used for indexing and searching
|
31 |
| - StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); |
32 |
| - |
33 |
| - // 1. create the index |
34 |
| - Directory index = new RAMDirectory(); |
35 |
| - |
36 |
| - IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer); |
37 |
| - |
38 |
| - IndexWriter w = new IndexWriter(index, config); |
39 |
| - addDoc(w, "Lucene in Action", "193398817"); |
40 |
| - addDoc(w, "Lucene for Dummies", "55320055Z"); |
41 |
| - addDoc(w, "Managing Gigabytes", "55063554A"); |
42 |
| - addDoc(w, "The Art of Computer Science", "9900333X"); |
43 |
| - w.close(); |
44 |
| - |
45 |
| - // 2. query |
46 |
| - String querystr = args.length > 0 ? args[0] : "lucene"; |
47 |
| - |
48 |
| - // the "title" arg specifies the default field to use |
49 |
| - // when no field is explicitly specified in the query. |
50 |
| - Query q = null; |
51 |
| - try { |
52 |
| - q = new QueryParser(Version.LUCENE_40, "title", analyzer).parse(querystr); |
53 |
| - } catch (org.apache.lucene.queryparser.classic.ParseException e) { |
54 |
| - e.printStackTrace(); |
55 |
| - } |
56 |
| - |
57 |
| - // 3. search |
58 |
| - int hitsPerPage = 10; |
59 |
| - IndexReader reader = DirectoryReader.open(index); |
60 |
| - IndexSearcher searcher = new IndexSearcher(reader); |
61 |
| - TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true); |
62 |
| - searcher.search(q, collector); |
63 |
| - ScoreDoc[] hits = collector.topDocs().scoreDocs; |
64 |
| - |
65 |
| - // 4. display results |
66 |
| - System.out.println("Found " + hits.length + " hits."); |
67 |
| - for (int i = 0; i < hits.length; ++i) { |
68 |
| - int docId = hits[i].doc; |
69 |
| - Document d = searcher.doc(docId); |
70 |
| - System.out.println((i + 1) + ". " + d.get("isbn") + "\t" + d.get("title")); |
71 |
| - } |
72 |
| - |
73 |
| - // reader can only be closed when there |
74 |
| - // is no need to access the documents any more. |
75 |
| - reader.close(); |
76 |
| - } |
77 |
| - |
78 |
| - private static void addDoc(IndexWriter w, String title, String isbn) throws IOException { |
79 |
| - Document doc = new Document(); |
80 |
| - doc.add(new TextField("title", title, Field.Store.YES)); |
81 |
| - |
82 |
| - // use a string field for isbn because we don't want it tokenized |
83 |
| - doc.add(new StringField("isbn", isbn, Field.Store.YES)); |
84 |
| - w.addDocument(doc); |
| 13 | +// StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_7_1_0); |
| 14 | +// |
| 15 | +// // 1. create the index |
| 16 | +// Directory index = new RAMDirectory(); |
| 17 | +// |
| 18 | +// IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer); |
| 19 | +// |
| 20 | +// IndexWriter w = new IndexWriter(index, config); |
| 21 | +// addDoc(w, "Lucene in Action", "193398817"); |
| 22 | +// addDoc(w, "Lucene for Dummies", "55320055Z"); |
| 23 | +// addDoc(w, "Managing Gigabytes", "55063554A"); |
| 24 | +// addDoc(w, "The Art of Computer Science", "9900333X"); |
| 25 | +// w.close(); |
| 26 | +// |
| 27 | +// // 2. query |
| 28 | +// String querystr = args.length > 0 ? args[0] : "lucene"; |
| 29 | +// |
| 30 | +// // the "title" arg specifies the default field to use |
| 31 | +// // when no field is explicitly specified in the query. |
| 32 | +// Query q = null; |
| 33 | +// try { |
| 34 | +// q = new QueryParser(Version.LUCENE_40, "title", analyzer).parse(querystr); |
| 35 | +// } catch (org.apache.lucene.queryparser.classic.ParseException e) { |
| 36 | +// e.printStackTrace(); |
| 37 | +// } |
| 38 | +// |
| 39 | +// // 3. search |
| 40 | +// int hitsPerPage = 10; |
| 41 | +// IndexReader reader = DirectoryReader.open(index); |
| 42 | +// IndexSearcher searcher = new IndexSearcher(reader); |
| 43 | +// TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true); |
| 44 | +// searcher.search(q, collector); |
| 45 | +// ScoreDoc[] hits = collector.topDocs().scoreDocs; |
| 46 | +// |
| 47 | +// // 4. display results |
| 48 | +// System.out.println("Found " + hits.length + " hits."); |
| 49 | +// for (int i = 0; i < hits.length; ++i) { |
| 50 | +// int docId = hits[i].doc; |
| 51 | +// Document d = searcher.doc(docId); |
| 52 | +// System.out.println((i + 1) + ". " + d.get("isbn") + "\t" + d.get("title")); |
| 53 | +// } |
| 54 | +// |
| 55 | +// // reader can only be closed when there |
| 56 | +// // is no need to access the documents any more. |
| 57 | +// reader.close(); |
| 58 | +// } |
| 59 | +// |
| 60 | +// private static void addDoc(IndexWriter w, String title, String isbn) throws IOException { |
| 61 | +// Document doc = new Document(); |
| 62 | +// doc.add(new TextField("title", title, Field.Store.YES)); |
| 63 | +// |
| 64 | +// // use a string field for isbn because we don't want it tokenized |
| 65 | +// doc.add(new StringField("isbn", isbn, Field.Store.YES)); |
| 66 | +// w.addDocument(doc); |
| 67 | +// } |
85 | 68 | }
|
86 |
| - |
87 | 69 | }
|
0 commit comments