Lucene实战之基于StandardAnalyzer读写索引
前言
使用lucene创建索引时如果指定了解析器,则需要读写都使用这个解析器,目前我发现也就是在处理中文这块比较麻烦,像你在使用solr时如果配置了ik分词,则需要把index清空重新创建才能继续搜索。
本篇引用lucene-6.4.0和4.x的几个关键类会有不同的地方。
创建索引
public void index(){
Directory dir=null;
Analyzer analyzer=null;
IndexWriterConfig config=null;
IndexWriter indexWriter=null;
try{
/**
* SimpleFSDirectory 不能很好支持多线程操作
* **/
dir =new SimpleFSDirectory(Paths.get(INDEX_URL));
analyzer=new StandardAnalyzer();
config =new IndexWriterConfig(analyzer);
/**
* IndexWriter(Directory d,IndexWriterConfig config)
* **/
indexWriter =new IndexWriter(dir,config);
indexWriter.deleteAll();
List<UploadBook> books =bookDao.listAllBooks();
Document document=null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(UploadBook book:books){
document=new Document();
document.add(new Field("id",book.getId().toString(), TextField.TYPE_STORED));
document.add(new Field("ip",book.getIp(), TextField.TYPE_STORED));
document.add(new Field("title",book.getOriginFileName(), TextField.TYPE_STORED));
document.add(new Field("content", PdfReader.read(INDEX_PDF+book.getNewFileName()),TextField.TYPE_STORED));
document.add(new Field("createtime",formatter.format(book.getCreateTime()), TextField.TYPE_STORED));
indexWriter.addDocument(document);
}
indexWriter.commit();
System.out.println("======索引创建完成,公创建"+books.size()+"条索引========");
}catch (IOException ex){
ex.printStackTrace();
}
catch(Exception ex){
ex.printStackTrace();
}finally {
if(indexWriter !=null){
try{
indexWriter.close();
}catch (IOException ex){
System.out.println("======indexWriter close exception========");
}
}
}
}
读取索引
public static List<Book> search2(String kw){
Directory dir=null;
Analyzer analyzer=null;
List<Book> list = new ArrayList<Book>();
try{
dir= FSDirectory.open(Paths.get("e:\\soso\\index"));
analyzer=new StandardAnalyzer();
DirectoryReader reader =DirectoryReader.open(dir);
IndexSearcher searcher=new IndexSearcher(reader);
QueryParser parser=new QueryParser("content",analyzer);
Query query =parser.parse(kw);
ScoreDoc[] docs=searcher.search(query,100).scoreDocs;
for (int i = 0; i < docs.length; i++) {
Document firstHit = searcher.doc(docs[i].doc);
Book book=new Book();
book.setId(Integer.parseInt(firstHit.getField("id").stringValue()));
book.setIp(firstHit.getField("ip").stringValue());
String title=firstHit.getField("title").stringValue();
title=title.substring(0,title.lastIndexOf("."));
book.setTitle(title);
String content=firstHit.getField("content").stringValue();
if(content.length()>=500){
content=content.substring(0,500)+"......";
}
book.setContent(content);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-mm");
Date date =format.parse(firstHit.getField("createtime").stringValue());
book.setCreateTime(format.format(date));
list.add(book);
}
}catch(Exception ex){
}finally {
try{
dir.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
return list;
}
Lucene实战之基于StandardAnalyzer读写索引的更多相关文章
- Lucene实战构建索引
搭建lucene的步骤这里就不详细介绍了,无外乎就是下载相关jar包,在eclipse中新建java工程,引入相关的jar包即可 本文主要在没有剖析lucene的源码之前实战一下,通过实战来促进研究 ...
- lucene全文搜索之三:生成索引字段,创建索引文档(给索引字段加权)基于lucene5.5.3
前言:上一章中我们已经实现了索引器的创建,但是我们没有索引文档,本章将会讲解如何生成字段.创建索引文档,给字段加权以及保存文档到索引器目录 luncene5.5.3集合jar包下载地址:http:// ...
- Lucene实战之初体验
前言 最早做非结构化数据搜索时用的还是lucene.net,一直说在学习java的同时把lucene这块搞一搞,这拖了2年多了,终于开始搞这块了. 开发环境 idea2016.lucene6.0.jd ...
- Lucene实战(第2版)》
<Lucene实战(第2版)>基于Apache的Lucene 3.0,从Lucene核心.Lucene应用.案例分析3个方面详细系统地介绍了Lucene,包括认识Lucene.建立索引.为 ...
- 3.2 Lucene实战:一个简单的小程序
在讲解Lucene索引和检索的原理之前,我们先来实战Lucene:一个简单的小程序! 一.索引小程序 首先,new一个java project,名字叫做LuceneIndex. 然后,在project ...
- 深度学习实战篇-基于RNN的中文分词探索
深度学习实战篇-基于RNN的中文分词探索 近年来,深度学习在人工智能的多个领域取得了显著成绩.微软使用的152层深度神经网络在ImageNet的比赛上斩获多项第一,同时在图像识别中超过了人类的识别水平 ...
- C++ MFC实现基于RFID读写器的上位机软件
C++ MFC实现基于RFID读写器的上位机软件 该博客涉及的完整工程托管在https://github.com/Wsine/UpperMonitor,觉得好请给个Star (/▽\=) 运行和测试环 ...
- ASP.NET Core 实战:基于 Dapper 扩展你的数据访问方法
一.前言 在非静态页面的项目开发中,必定会涉及到对于数据库的访问,最开始呢,我们使用 Ado.Net,通过编写 SQL 帮助类帮我们实现对于数据库的快速访问,后来,ORM(Object Relatio ...
- R语言实战实现基于用户的简单的推荐系统(数量较少)
R语言实战实现基于用户的简单的推荐系统(数量较少) a<-c(1,1,1,1,2,2,2,2,3,3,3,4,4,4,5,5,5,5,6,6,7,7) b<-c(1,2,3,4,2,3,4 ...
随机推荐
- 从服务器角度分析RPG游戏——NPC的AI
最近主程有些忙,甩给我一些服务器的代码,零零散散总结了一些要素. java程序架构也是层层分析,先罗列出需要做的工作,然后从主干到细节依次实现.就这点而言,程序和绘画有很多类似的地方. 关于怪物AI类 ...
- P3379 【模板】最近公共祖先(LCA)(树链剖分)版
#include <bits/stdc++.h> #define read read() #define up(i,l,r) for(register int i = (l);i < ...
- sleep() 和 wait() 区别
sleep()不释放同步锁,wait()释放同步锁 sleep()的作用是让线程休眠指定的时间,时间到后自动恢复线程执行.运行的主动权是由线程决定的. wait()可以用notify()直接唤起,运行 ...
- 把router-link标签渲染成指定的标签
<router-link>标签默认渲染成 <a>标签,可以通过tag属性把router-link渲染成指定的标签,如: <router-link to="/&q ...
- 第一个Python小爬虫
这个爬虫是参考http://python.jobbole.com/81353/这篇文章写的 这篇文章可能年代过于久远,所以有些代码会报错,然后我自己稍微修改了一下,增加了一个getContentAll ...
- Linux连不上校园网怎么办?
原本,在我们学校我只要连上WiFi打开浏览器, 它就会自动重定向到校园网登录的界面.但是今天浏览器并没有自己打开登录页面,一直在加载.于是我想直接登录路由器,在地址栏输入192.168.0.1,结果它 ...
- re、词云
正则: re.S使点也能匹配到\n:re.I不区分规则中的大小写:re.X忽略空格及#后的注释:re.M把^和$由文首文末变为各行的首尾. Egの删除各行行尾的alex,alex不区分大小写: ...
- spring boot开发笔记——mybatis
概述 mybatis框架的优点,就不用多说了,今天这边干货主要讲mybatis的逆向工程,以及springboot的集成技巧,和分页的使用 因为在日常的开发中,当碰到特殊需求之类会手动写一下s ...
- php安装扩展的几种方法
转自:http://doc3.workerman.net/appendices/install-extension.html 安装扩展 注意 与Apache+PHP或者Nginx+PHP的运行模式不同 ...
- 4.json解析
格式 {"name":"zhangsan", "age":18, "books":[{"name": ...