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();
}
}
}
- lucene6.3 简单例子
- lucene6 IntPoint
- jQuery简单例子-3
- lucene3.3的简单例子
- spring security 3 简单例子
- 简单例子
- 简单例子
- 简单例子
- Lucene6.1学习案例
- Lucene6的使用
- 初识Lucene6.6.0
- Lucene6.0基础教学
- lucene6.6.0学习心得
- Flex builder 3 简单例子1
- Spring AOP—3、简单的例子
- JavaScript介绍及其简单例子(3)
- Lucene6.1查询所有数据
- lucene6.2.1测试用例
- 嵌入式软件开发经典面试题
- This network connection does not exist
- 移动端之路-阿里Weex框架快速体验与环境搭建
- 正则表达式匹配
- java延时队列使用
- lucene6.3 简单例子
- request和response的中文乱码问题及解决方案
- centos7搭建分布式——系统环境
- mac 终端配置iterm2 + oh-my-zsh + tmux + vim
- 前言
- okGo的相关应用
- stm32 nvic配置
- 表达式求值
- JSP = HTML技术 + Java技术 + JSP特有技术