lucene5.3.1+IKAnalyer 构建简单搜索引擎
2015-11-30 21:25
369 查看
项目应用场景
最近需要做一个简单的信息展示系统,信息和普通新闻差不多,主要有标题和内容,信息需要能通过关键词检索到,考虑到信息比较简单,检索也很简单,主要是通过标题和内容搜索,不想用Solr搭建搜索引擎,想用的Lucene写个简的搜索,能构增加索引、删除索引,通过关键字搜索信息就可以了。项目依赖包
Lucene使用最新版本5.3.1 Maven配置如下<dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>5.3.1</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>5.3.1</version> </dependency>
分词器选择
由于项目对搜索精度没有太高要求,所以选择常用的IK分词器就可以了,选用的版本为IKAnalyzer2012_V5.jar ,百度可以很好搜索文章准备
在C:/MyIndex/doc 目录下准备了两篇测试新闻,如下图所示创建索引
public static void indexFile() throws IOException { Directory dir = FSDirectory.open(Paths.get("C:/MyIndex")); Analyzer analyzer = new IKAnalyzer(); IndexWriterConfig iwc = new IndexWriterConfig(analyzer); IndexWriter writer = new IndexWriter(dir, iwc); File path=new File("C:\\MyIndex\\doc"); File [] file= path.listFiles(); for(File f:file){ String tile=f.getName(); String content=""; BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(f),"utf-8")); String line=""; while (null!=(line=br.readLine())) { content+=line; } Document doc = new Document(); doc.add(new TextField("name", tile, Field.Store.YES)); doc.add(new TextField("content", content,Field.Store.YES)); writer.addDocument(doc); System.out.println("add " + tile); } writer.close(); }
测试搜索
public static void search() throws IOException, ParseException { Analyzer analyzer = new IKAnalyzer(); IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get("C:/MyIndex"))); IndexSearcher searcher = new IndexSearcher(reader); QueryParser parser = new QueryParser("name", analyzer); String queries = "奥巴马"; Query query = parser.parse(queries); System.out.println("Searching for: " + query.toString(queries)); TopDocs results = searcher.search(query, 10); System.out.println("Total match:" + results.totalHits); ScoreDoc[] hits = results.scoreDocs; int count = 1; for (ScoreDoc hit : hits) { Document doc1 = searcher.doc(hit.doc); String res = doc1.get("name"); System.err.println(count + " " + res + ", " + hit.score); count++; } }
运行结果
add ***会见奥巴马: 中美两国要牢牢把握构建新型大国关系正确方向.txt add 最高法紧急下令暂缓运毒7.5公斤农民死刑执行.txt Searching for: name:奥 name:巴马 Total match:1 1 ***会见奥巴马: 中美两国要牢牢把握构建新型大国关系正确方向.txt, 0.30935922
原文链接
相关文章推荐
- 关于源码编译每次提示有错误 要make update-api
- JDBC(数据库)
- openfire源码分析---8
- android的EditText重新获取焦点问题
- PMI(Pointwise Mutual Information)
- Android 中 Handler 内存泄露解决办法实例
- JAVAWEB之自定义TAG开发
- 解决win10鼠标晃动问题
- 登陆拦截 filter() , 字符编码拦截器
- Hibernate查询_HQL_EJBQL_QBC_QBE
- Qt从零开始制作串口调试助手-(第十二章、串口数据发送)-Creator_Ly
- 如何让label.text 中的某些字变成不同的颜色
- Jquery取值
- 九宫格解锁可用修改版
- Linux系统与Windows的基本区别
- Jquery选择器
- 1063. Set Similarity
- C++ 表达式和运算符
- 胖虎谈ImageLoader框架(三)
- Neper软件介绍及安装视频教程