Lucene实战之初体验
前言
最早做非结构化数据搜索时用的还是lucene.net,一直说在学习java的同时把lucene这块搞一搞,这拖了2年多了,终于开始搞这块了。
开发环境
idea2016、lucene6.0、jdk1.8
使用lucene准备条件
1、pom.xml

2、测试数据。 我从博客园首页拿了几条数据,直接写了个伪Dao返回一个List<Blog>
public List<Blog> listBlogs(){
List<Blog> list = new ArrayList<Blog>();
Blog b1=new Blog(6538488,"风过无痕-唐","你真的了解volatile吗,关于volatile的那些事","http://www.cnblogs.com/tangyanbo/p/6538488.html");
Blog b2=new Blog(6541312,"布鲁克石","TypeScript设计模式之职责链、状态","http://www.cnblogs.com/brookshi/p/6541312.html");
Blog b7=new Blog(6547215,"嵘么么","轻松理解JavaScript闭包","http://www.cnblogs.com/rongmm/p/6547215.html");
Blog b3=new Blog(6541220,"一线码农","使用Task的一些知识优化了一下同事的多线程协作取消的一串代码","http://www.cnblogs.com/huangxincheng/p/6541220.html");
Blog b4=new Blog(6516387,"xybaby","并发与同步、信号量与管程、生产者消费者问题","http://www.cnblogs.com/xybaby/p/6516387.html");
Blog b5=new Blog(6532713,"王福朋","深入理解 JavaScript 异步系列(4)—— Generator","http://www.cnblogs.com/wangfupeng1988/p/6532713.html");
Blog b6=new Blog(6517788,"欢醉","入坑系列之HAProxy负载均衡","http://www.cnblogs.com/zhangs1986/p/6517788.html");
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
list.add(b5);
list.add(b6);
list.add(b7);
return list;
}
IndexSearcher搜索
1、搜索体验,基于servlet开发,直接接受路径参数key

2、先写入索引
public class IndexWriterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String index_path="d:\\indexDir";
Directory dir=null;
Analyzer analyzer=null;
IndexWriterConfig config=null;
IndexWriter indexWriter=null;
try {
dir =new SimpleFSDirectory(Paths.get(index_path));
analyzer=new StandardAnalyzer();
config =new IndexWriterConfig(analyzer);
indexWriter =new IndexWriter(dir,config);
BlogDao blogDao=new BlogDao();
List<Blog> list = blogDao.listBlogs();
for (Blog blog:list){
Document document=new Document();
document.add(new Field("id",blog.getId().toString(),TextField.TYPE_STORED));
document.add(new Field("author",blog.getAuthor(),TextField.TYPE_STORED));
document.add(new Field("title",blog.getTitle(),TextField.TYPE_STORED));
document.add(new Field("url",blog.getUrl(),TextField.TYPE_STORED));
indexWriter.addDocument(document);
}
indexWriter.commit();
System.out.println("====索引创建完成====");
}catch (Exception ex){
ex.printStackTrace();
}finally {
if(indexWriter !=null){
indexWriter.close();
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
2、查询索引数据
public class IndexReaderServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Directory dir =null;
DirectoryReader reader =null;
IndexSearcher searcher=null;
String index_path="d:\\indexDir";
try{
//dir =new RAMDirectory();
dir =new SimpleFSDirectory(Paths.get(index_path));
reader = DirectoryReader.open(dir);
searcher=new IndexSearcher(reader);
String key = req.getParameter("key");
Query query = new TermQuery(new Term("title",key));
TopDocs rs=searcher.search(query,100);
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
PrintWriter out = resp.getWriter();
StringBuilder strHtml =new StringBuilder();
for (int i = 0; i < rs.scoreDocs.length; i++) {
Document firstHit = searcher.doc(rs.scoreDocs[i].doc);
strHtml.append("title:"+firstHit.getField("title").stringValue()+"<br>");
strHtml.append("author:"+firstHit.getField("author").stringValue()+"<br>");
strHtml.append("<a href='"+firstHit.getField("url").stringValue()+"'>"+ firstHit.getField("url").stringValue()+"</a><br>");
strHtml.append("====================================================<br>");
}
out.write(strHtml.toString());
}catch (Exception ex){
ex.printStackTrace();
}finally {
reader.close();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
总结
读写索引用到的几个关键类:IndexWriter、IndexSearcher、Directory、DirectoryReader、Document、TermQuery、Field
Lucene实战之初体验的更多相关文章
- Flume 实战(1) -- 初体验
前言: Flume-ng是数据收集/聚合/传输的组件, Flume-ng抛弃了Flume OG原本繁重的zookeeper和Master, Collector, 其整体的架构更加的简洁和明了. 其基础 ...
- Scala 深入浅出实战经典 第66讲:Scala并发编程实战初体验
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- Python+Flask+Gunicorn 项目实战(一) 从零开始,写一个Markdown解析器 —— 初体验
(一)前言 在开始学习之前,你需要确保你对Python, JavaScript, HTML, Markdown语法有非常基础的了解.项目的源码你可以在 https://github.com/zhu-y ...
- 【全面解禁!真正的Expression Blend实战开发技巧】第七章 MVVM初体验-在DataGrid行末添加按钮
原文:[全面解禁!真正的Expression Blend实战开发技巧]第七章 MVVM初体验-在DataGrid行末添加按钮 博客更新较慢,先向各位读者说声抱歉.这一节讲解的依然是开发中经常遇到的一种 ...
- dubbo实战之一:准备和初体验
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- Dubbo基础一之实战初体验
本以为写这个小作文没什么难度的,可是好像并不是.前段时间重心放在驾考科目二,就想着小作文科二考过了再写也不是事,因为都实战过了.今天想着写却发现脑袋里啥都想不起来了,得翻项目和笔记回忆一下.所以还是那 ...
- .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验
不知不觉,“.NET平台开源项目速览“系列文章已经15篇了,每一篇都非常受欢迎,可能技术水平不高,但足够入门了.虽然工作很忙,但还是会抽空把自己知道的,已经平时遇到的好的开源项目分享出来.今天就给大家 ...
- python--爬虫入门(七)urllib库初体验以及中文编码问题的探讨
python系列均基于python3.4环境 ---------@_@? --------------------------------------------------------------- ...
- 【阿里云产品公测】消息队列服务MQS java SDK 机器人应用初体验
[阿里云产品公测]消息队列服务MQS java SDK 机器人应用初体验 作者:阿里云用户啊里新人 初体验 之 测评环境 由于MQS支持外网访问,因此我在本地做了一些简单测试(可能有些业余),之后 ...
随机推荐
- PHP那些最好的轮子
PHP那些最好的轮子 Databse 数据库ORM Doctrine 2 License : MIT Source Code Allo点评:Doctrine是功能最全最完善的PHP ORM,社区一直很 ...
- SQL索引--基础理论
1.索引的定义 索引是数据库表中一列或多列的值进行的一种排序,用于快速找出在某一列中特定的值. 2.索引的原理 如果不使用索引,则通常的查询数据中,需要对表中数据做一一对应的比较,直到找出所有相关的行 ...
- HDU4474
Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- 从svn上回滚版本
转载地址:http://blog.csdn.net/happyqyt/article/details/7107039 提交SVN后想回滚到旧版本. 选择TortoiseSVN→Repo-browser ...
- C# vs TypeScript - 高级类型
总目录 从C#到TypeScript - 类型 从C#到TypeScript - 高级类型 从C#到TypeScript - 变量 从C#到TypeScript - 接口 从C#到TypeScript ...
- 如何用webbrowser获取ajax动态生成的网页的源码?
1.步骤一:修改IE内核的版本(这个方法厉害了) public Form1() { InitializeComponent();int BrowserVer, RegVal; // get the i ...
- CI Weekly #13 | 用更 Geek 的方式配置你的 CI 工作流
flow.ci 的重大更新来了--支持通过 .yml 文件配置工作流(测试阶段),具体的使用方法可参考文档:同时 flow.ci 也开放了社区>> club.flow.ci,使用的任何问题 ...
- MongoDB与Redis的比较
MongoDB和Redis都是NoSQL,采用结构型数据存储.二者在使用场景中,存在一定的区别,这也主要由于二者在内存映射的处理过程,持久化的处理方法不同. MongoDB建议集群部署,更多的考虑到集 ...
- Python自然语言处理学习笔记之信息提取步骤&分块(chunking)
一.信息提取模型 信息提取的步骤共分为五步,原始数据为未经处理的字符串, 第一步:分句,用nltk.sent_tokenize(text)实现,得到一个list of strings 第二步:分词,[ ...
- 【前端】:css
前言: 关于前端的第二篇博客,会写关于css的,内容比较基础.写完这篇博客,会做一个类似美乐乐家具的界面.good luck to me~ 一.css-引用样式 标签上设置style属性: <b ...