|
| 1 | +package com.xingyun; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.text.ParseException; |
| 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 | +public class HelloLucene { |
| 25 | + |
| 26 | + public static void main(String[] args) throws IOException, ParseException { |
| 27 | + |
| 28 | + // 对于这个简单的例子,我们将从一些字符串中创建一个内存索引。 |
| 29 | + // 0. Specify the analyzer for tokenizing text. |
| 30 | + // 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); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments