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支持外网访问,因此我在本地做了一些简单测试(可能有些业余),之后 ...
随机推荐
- Flex 各种校验
Flex 各种校验: 参考:http://blog.csdn.net/jerry_bj/article/details/5650469 参考正则表达式:http://www.cnblogs.com/f ...
- HTML5常用标签分类
1.行级元素标签:a.span.sup.sub.em.b.big.i.strong 2.块元素标签:div.p.h1~h6.ul.ol.li.table.form.article.footer.hea ...
- SQL SERVER 判断是否存在并删除某个数据库、表、视图、触发器、储存过程、函数
-- SQL SERVER 判断是否存在某个触发器.储存过程 -- 判断储存过程,如果存在则删除IF (EXISTS(SELECT * FROM sysobjects WHERE name='proc ...
- 编写JQuery插件-4
封装对象方法的插件 jQuery.fn.extend() 的两种写法 以添加一个点击按钮为例: 方法一: (function ($) { $.fn.mask = function(options){ ...
- iOS网络层设计感想
App的开发无外乎从网络端获取数据显示在屏幕上,数据做些缓存或者持久化,所以网络层极为重要.原来只是把AFNetwork二次封装了一下,使得调用变得很简单,并没有深层次的考虑一些问题. 前言 参考: ...
- 使用python制作ArcGIS插件(2)代码编写
使用python制作ArcGIS插件(2)代码编写 by 李远祥 上一章节已经介绍了如何去搭建AddIn的界面,接下来要实现具体的功能,则到了具体的编程环节.由于使用的是python语言进行编程,则开 ...
- 【前端】:css
前言: 关于前端的第二篇博客,会写关于css的,内容比较基础.写完这篇博客,会做一个类似美乐乐家具的界面.good luck to me~ 一.css-引用样式 标签上设置style属性: <b ...
- Java内部类基本使用
链接到外部类 创建内部类时,那个类的对象同时拥有封装对象(封装内部类的外部类)的一个链接,所以内部类可以访问外部类的成员. 内部类拥有对外部类所有元素的访问权限. 看如下代码,内部类SSe ...
- Python单元测试——深入理解unittest
单元测试的重要性就不多说了,可恶的是python中有太多的单元测试框架和工具,什么unittest, testtools, subunit, coverage, testrepository, nos ...
- block之--- 基本使用
block的类型:对象 官方文档描述如下 “Blocks are Objective-C objects, which means they can be added to collections l ...