Author: Not specified Language: java
Description: Not specified Timestamp: 2018-04-14 11:29:14 +0000
View raw paste Reply
  1. import java.io.IOException;
  2. import java.nio.file.Paths;
  3.  
  4. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  5. import org.apache.lucene.document.Document;
  6. import org.apache.lucene.document.Field;
  7. import org.apache.lucene.document.StringField;
  8. import org.apache.lucene.document.TextField;
  9. import org.apache.lucene.index.DirectoryReader;
  10. import org.apache.lucene.index.IndexReader;
  11. import org.apache.lucene.index.IndexWriter;
  12. import org.apache.lucene.index.IndexWriterConfig;
  13. import org.apache.lucene.queryparser.classic.QueryParser;
  14. import org.apache.lucene.search.*;
  15. import org.apache.lucene.store.*;
  16.  
  17. public class HelloLucene {
  18.  
  19.     public static void main(String[] args) throws IOException {
  20.         // 0. Specify the analyzer for tokenizing text.
  21.         //    The same analyzer should be used for indexing and searching
  22.         StandardAnalyzer analyzer = new StandardAnalyzer();
  23.  
  24.         // 1. create the index
  25.         Directory index = FSDirectory.open(Paths.get("./index.lucene"));
  26.  
  27.         IndexWriterConfig config = new IndexWriterConfig(analyzer);
  28.  
  29.         IndexWriter w = new IndexWriter(index, config);
  30.         addDoc(w, "Lucene in Action", "193398817");
  31.         addDoc(w, "Lucene for Dummies (Lucene)", "55320055Z");
  32.         addDoc(w, "Managing Gigabytes", "55063554A");
  33.         addDoc(w, "The Art of Computer Science", "9900333X");
  34.         w.close();
  35.  
  36.         // 2. query
  37.         String querystr = args.length > 0 ? args[0] : "lucene";
  38.  
  39.         // the "title" arg specifies the default field to use
  40.         // when no field is explicitly specified in the query.
  41.         Query q = null;
  42.         try {
  43.             q = new QueryParser("title", analyzer).parse(querystr);
  44.         } catch (org.apache.lucene.queryparser.classic.ParseException e) {
  45.             e.printStackTrace();
  46.         }
  47.  
  48.         // 3. search
  49.         int hitsPerPage = 10;
  50.         IndexReader reader = DirectoryReader.open(index);
  51.         IndexSearcher searcher = new IndexSearcher(reader);
  52.  
  53.         ScoreDoc[] hits = searcher.search(q, 100).scoreDocs;
  54.        
  55.         // 4. display results
  56.         System.out.println("Found " + hits.length + " hits.");
  57.         for (int i = 0; i < hits.length; ++i) {
  58.             int docId = hits[i].doc;
  59.             float score = hits[i].score;
  60.             Document d = searcher.doc(docId);
  61.             System.out.println((i + 1) + ". " + d.get("isbn") + "\t" + d.get("title") + "\t" + score);
  62.         }
  63.  
  64.         // reader can only be closed when there
  65.         // is no need to access the documents any more.
  66.         reader.close();
  67.     }
  68.  
  69.     private static void addDoc(IndexWriter w, String title, String isbn) throws IOException {
  70.         Document doc = new Document();
  71.         doc.add(new TextField("title", title, Field.Store.YES));
  72.  
  73.         // use a string field for isbn because we don't want it tokenized
  74.         doc.add(new StringField("isbn", isbn, Field.Store.YES));
  75.         w.addDocument(doc);
  76.     }
  77. }
View raw paste Reply