lucene6.3 简单例子

来源:互联网 发布:格里芬数据 编辑:程序博客网 时间:2024/05/29 17:58

好久不折腾搜索了,睡前留意了下lucene版本,到了6.30顺便下载了,做了一个简单demo 测试比较坑的是需要jdk1.8了

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;


//注意jdk需要1.8 比较坑
public class IndexandSearch {


/**
* @param args
*/


private static Document createDocument(String title, String content) {
Document doc = new Document();
doc.add(new Field("content", content, TextField.TYPE_NOT_STORED));
doc.add(new Field("title", title, TextField.TYPE_STORED));
doc.add(new Field("author", "bobliu", TextField.TYPE_STORED));
return doc;
}


/**
* lucene简单实例 索引 查询 经济,分词器:标准分词器
*/
public static void testDemo() throws Exception{  
        Analyzer analyzer = new StandardAnalyzer();;  
        Directory idx = new RAMDirectory();  
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);  
        IndexWriter writer = new IndexWriter(idx, iwc);  
        writer.addDocument(createDocument("中央政治局研究2017年经济工作", "中共中央政治局12月9日召开会议,分析研究2017年经济工作,审议通过《关于加强国家安全工作的意见》。中共中央总书记习近平主持会议。"));  
        writer.addDocument(createDocument("中央政治局研究2017年经济工作new","test中共中央政治局12月9日召开会议,分析研究2017年经济工作,审议通过《关于加强国家安全工作的意见》。中共中央总书记习近平主持会议。1111111");  
        writer.commit();  
        writer.close();  
        IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(idx));  
        System.out.println("命中个数:"+searcher.search(new QueryParser("title", analyzer).parse("title:'经济'"), 10).totalHits);  
        
        TopDocs topdoc =  searcher.search(new QueryParser("title", analyzer).parse("title:'经济'"), 10);
        ScoreDoc[] hits=  topdoc.scoreDocs;  
        
        if(hits!=null && hits.length>0){  
           for(int i = 0; i < hits.length; i++){  
             Document hitDoc = searcher.doc(hits[i].doc);  
            System.out.println(hitDoc.get("content"));      

           System.out.println(hitDoc.get("author"));                     
           }             
        }
      
        
    }
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
testDemo();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

0 1
原创粉丝点击