lucene4.0入门实例


lucene4.0入门实例

 

1:以前用3.5的时候,到现在也差不多忘了,重新看了下文档,写个简单的例子

 

lucene4.0中有很多新的东西,其中Field类主要不能new Field()要通过其子类去实现比如new StringField()等,对分词等参数也有部分变化。

 

创建索引的代码如下:

 

 

Java代码  收藏代码
  1. package com.search.lucene;  
  2.   
  3. import java.io.File;  
  4.   
  5. import org.apache.lucene.analysis.Analyzer;  
  6. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
  7. import org.apache.lucene.document.Document;  
  8. import org.apache.lucene.document.Field.Store;  
  9. import org.apache.lucene.document.StringField;  
  10. import org.apache.lucene.document.TextField;  
  11. import org.apache.lucene.index.IndexWriter;  
  12. import org.apache.lucene.index.IndexWriterConfig;  
  13. import org.apache.lucene.store.Directory;  
  14. import org.apache.lucene.store.FSDirectory;  
  15. import org.apache.lucene.util.Version;  
  16. import org.junit.Before;  
  17. import org.junit.Test;  
  18.   
  19. public class IndexFile {  
  20.   
  21.     protected String[] ids={"1""2"};  
  22.   
  23.     protected String[] content={"Amsterdam has lost of add  cancals""i love  add this girl"};  
  24.   
  25.     protected String[] city={"Amsterdam""Venice"};  
  26.   
  27.     private Directory dir;  
  28.   
  29.     /** 
  30.      * 初始添加文档 
  31.      * @throws Exception 
  32.      */  
  33.     @Test  
  34.     public void init() throws Exception {  
  35.         String pathFile="D://lucene/index";  
  36.         dir=FSDirectory.open(new File(pathFile));  
  37.         IndexWriter writer=getWriter();  
  38.         for(int i=0; i < ids.length; i++) {  
  39.             Document doc=new Document();  
  40.             doc.add(new StringField("id", ids[i], Store.YES));  
  41.             doc.add(new TextField("content", content[i], Store.YES));  
  42.             doc.add(new StringField("city", city[i], Store.YES));  
  43.             writer.addDocument(doc);  
  44.         }  
  45.         System.out.println("init ok?");  
  46.         writer.close();  
  47.     }  
  48.   
  49.     /** 
  50.      * 获得IndexWriter对象 
  51.      * @return 
  52.      * @throws Exception 
  53.      */  
  54.     public IndexWriter getWriter() throws Exception {  
  55.         Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_40);  
  56.         IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_40, analyzer);  
  57.         return new IndexWriter(dir, iwc);  
  58.     }  
  59.   
  60. }  

 说明下:lucene4.0中有核心包和其他包:我导入


 

 

Java代码  收藏代码
  1. package com.search.lucene;  
  2.   
  3. import java.io.File;  
  4.   
  5. import org.apache.lucene.document.Document;  
  6. import org.apache.lucene.index.DirectoryReader;  
  7. import org.apache.lucene.index.IndexReader;  
  8. import org.apache.lucene.index.Term;  
  9. import org.apache.lucene.search.IndexSearcher;  
  10. import org.apache.lucene.search.ScoreDoc;  
  11. import org.apache.lucene.search.TermQuery;  
  12. import org.apache.lucene.search.TopDocs;  
  13. import org.apache.lucene.store.Directory;  
  14. import org.apache.lucene.store.FSDirectory;  
  15. import org.junit.Test;  
  16.   
  17. public class IndexSearch {  
  18.   
  19.     /** 
  20.      * 查询 
  21.      * @throws Exception 
  22.      */  
  23.     @Test  
  24.     public void search() throws Exception {  
  25.         String filePath="D://lucene/index";  
  26.         Directory dir=FSDirectory.open(new File(filePath));  
  27.         IndexReader reader=DirectoryReader.open(dir);  
  28.         IndexSearcher searcher=new IndexSearcher(reader);  
  29.         Term term=new Term("content""add");  
  30.         TermQuery query=new TermQuery(term);  
  31.         TopDocs topdocs=searcher.search(query, 5);  
  32.         ScoreDoc[] scoreDocs=topdocs.scoreDocs;  
  33.         System.out.println("查询结果总数---" + topdocs.totalHits+"最大的评分--"+topdocs.getMaxScore());  
  34.         for(int i=0; i < scoreDocs.length; i++) {  
  35.             int doc = scoreDocs[i].doc;  
  36.             Document document = searcher.doc(doc);  
  37.             System.out.println("content===="+document.get("content"));  
  38.             System.out.println("id--" + scoreDocs[i].doc + "---scors--" + scoreDocs[i].score+"---index--"+scoreDocs[i].shardIndex);  
  39.         }  
  40.         reader.close();  
  41.     }  
  42. }  

相关内容

    暂无相关文章