`
aaron_ch
  • 浏览: 172894 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Lucene practice

    博客分类:
  • Java
阅读更多

1.simply practice the lucene api for search document and data, post code first

lucene version :2.2.0

set up the index of files
java 代码
  1. package com.aaron.lucene;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileReader;   
  5. import java.io.Reader;   
  6. import java.util.Date;   
  7. import org.apache.lucene.analysis.Analyzer;   
  8. import org.apache.lucene.analysis.standard.StandardAnalyzer;   
  9. import org.apache.lucene.document.Document;   
  10. import org.apache.lucene.document.Field;   
  11. import org.apache.lucene.index.IndexWriter;   
  12.   
  13. /**  
  14.  * This class demonstrate the process of creating index with Lucene for text  
  15.  * files  
  16.  */  
  17. public class TxtFileIndexer {   
  18.     public static void main(String[] args) throws Exception {   
  19.         // indexDir is the directory that hosts Lucene's index files   
  20.         File indexDir = new File("C:/bin/lucence/Index");   
  21.         // dataDir is the directory that hosts the text files that to be indexed   
  22.         File dataDir = new File("C:/bin/lucence/Data");   
  23.         Analyzer luceneAnalyzer = new StandardAnalyzer();   
  24.         File[] dataFiles = dataDir.listFiles();   
  25.         IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,   
  26.                 true);   
  27.         long startTime = new Date().getTime();   
  28.         for (int i = 0; i < dataFiles.length; i++) {   
  29.             if (dataFiles[i].isFile()   
  30.                     && dataFiles[i].getName().endsWith(".txt")) {   
  31.                 System.out.println("Indexing file "  
  32.                         + dataFiles[i].getCanonicalPath());   
  33.                 Document document = new Document();   
  34.                 Reader txtReader = new FileReader(dataFiles[i]);   
  35.   
  36.                 document.add(new Field("path", dataFiles[i].getPath(),   
  37.                         Field.Store.YES, Field.Index.UN_TOKENIZED));   
  38.                 document.add(new Field("contents", txtReader));   
  39.                 indexWriter.addDocument(document);   
  40.             }   
  41.         }   
  42.         indexWriter.optimize();   
  43.         indexWriter.close();   
  44.         long endTime = new Date().getTime();   
  45.   
  46.         System.out.println("It takes " + (endTime - startTime)   
  47.                 + " milliseconds to create index for the files in directory "  
  48.                 + dataDir.getPath());   
  49.     }   
  50. }   
Search key word
java 代码
  1. package com.aaron.lucene;   
  2.   
  3. import java.io.File;   
  4. import org.apache.lucene.document.Document;   
  5. import org.apache.lucene.index.Term;   
  6. import org.apache.lucene.search.Hits;   
  7. import org.apache.lucene.search.IndexSearcher;   
  8. import org.apache.lucene.search.TermQuery;   
  9. import org.apache.lucene.store.FSDirectory;   
  10.   
  11. /**  
  12.  * This class is used to demonstrate the   
  13.  * process of searching on an existing   
  14.  * Lucene index  
  15.  *  
  16.  */  
  17. public class TextFileSearcher {   
  18.     public static void main(String[] args) throws Exception {   
  19.         String queryStr = "lucene";   
  20.         //This is the directory that hosts the Lucene index   
  21.         File indexDir = new File("C:/bin/lucence/Index");   
  22.         FSDirectory directory = FSDirectory.getDirectory(indexDir);   
  23.         IndexSearcher searcher = new IndexSearcher(directory);   
  24.         if (!indexDir.exists()) {   
  25.             System.out.println("The Lucene index is not exist");   
  26.             return;   
  27.         }   
  28.         Term term = new Term("contents", queryStr.toLowerCase());   
  29.         TermQuery luceneQuery = new TermQuery(term);   
  30.         Hits hits = searcher.search(luceneQuery);   
  31.         for (int i = 0; i < hits.length(); i++) {   
  32.             Document document = hits.doc(i);   
  33.             System.out.println("File: " + document.get("path"));   
  34.         }   
  35.     }   
  36. }   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics