前言

使用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读写索引的更多相关文章

  1. Lucene实战构建索引

    搭建lucene的步骤这里就不详细介绍了,无外乎就是下载相关jar包,在eclipse中新建java工程,引入相关的jar包即可 本文主要在没有剖析lucene的源码之前实战一下,通过实战来促进研究 ...

  2. lucene全文搜索之三:生成索引字段,创建索引文档(给索引字段加权)基于lucene5.5.3

    前言:上一章中我们已经实现了索引器的创建,但是我们没有索引文档,本章将会讲解如何生成字段.创建索引文档,给字段加权以及保存文档到索引器目录 luncene5.5.3集合jar包下载地址:http:// ...

  3. Lucene实战之初体验

    前言 最早做非结构化数据搜索时用的还是lucene.net,一直说在学习java的同时把lucene这块搞一搞,这拖了2年多了,终于开始搞这块了. 开发环境 idea2016.lucene6.0.jd ...

  4. Lucene实战(第2版)》

    <Lucene实战(第2版)>基于Apache的Lucene 3.0,从Lucene核心.Lucene应用.案例分析3个方面详细系统地介绍了Lucene,包括认识Lucene.建立索引.为 ...

  5. 3.2 Lucene实战:一个简单的小程序

    在讲解Lucene索引和检索的原理之前,我们先来实战Lucene:一个简单的小程序! 一.索引小程序 首先,new一个java project,名字叫做LuceneIndex. 然后,在project ...

  6. 深度学习实战篇-基于RNN的中文分词探索

    深度学习实战篇-基于RNN的中文分词探索 近年来,深度学习在人工智能的多个领域取得了显著成绩.微软使用的152层深度神经网络在ImageNet的比赛上斩获多项第一,同时在图像识别中超过了人类的识别水平 ...

  7. C++ MFC实现基于RFID读写器的上位机软件

    C++ MFC实现基于RFID读写器的上位机软件 该博客涉及的完整工程托管在https://github.com/Wsine/UpperMonitor,觉得好请给个Star (/▽\=) 运行和测试环 ...

  8. ASP.NET Core 实战:基于 Dapper 扩展你的数据访问方法

    一.前言 在非静态页面的项目开发中,必定会涉及到对于数据库的访问,最开始呢,我们使用 Ado.Net,通过编写 SQL 帮助类帮我们实现对于数据库的快速访问,后来,ORM(Object Relatio ...

  9. 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 ...

随机推荐

  1. windows 性能监视器

    转载地址:https://www.cnblogs.com/luo-mao/p/5872374.html

  2. git stash错误小记

    git出错小记 想要push代码,我们经常这样做. 1.查看状态 git status 2.隐藏本地编辑的新内容 git stash 3.拉远程的代码 git pull 这一步操作有的时候会报错,没有 ...

  3. python之高阶函数

    1.函数的参数能接收变量 def calc(x): return x*x n = 10 print(calc(n)) #输出为100 2.变量指向函数 def calc(x): return x*x ...

  4. Nginx的rewrite应用

    Rewrite主要的功能是实现URL重写,Nginx 的 Rewrite 规则采用 PCRE Perl 兼容正则表达式的语法进行规则匹配,如相使用 Nginx 的 Rewrite 功能,在编译 Ngi ...

  5. 位图法bitmap

    1.概念 1)所谓bitmap,就是用每一位(bit)来标记某个元素对应的value, 而key即是该元素,通常bitmap是一个int数组,用每一个int数的每一个bit来映射某个数据 2)由于采用 ...

  6. Xutils简

    //解析 private void myinitData() { RequestParams parms=new RequestParams("http://huixinguiyu.cn/A ...

  7. ELK学习

    [root@log-node1 ~]# cobbler repo add --name=logstash-2.3 --mirror=http://packages.elastic.co/logstas ...

  8. 【git 报错】Could not read from remote repository.Please make sure you have the correct access rights.

    我们在使用git clone 或其他命令的时候,有时候会遇到这类问题,如图: and the repository exists. fatal: Could not read from remote ...

  9. Linux 线程编程1.0

    在编译多线程程序的时候,需要连接libpthread文件: gcc pthread.c  -o  pthread  -lpthread: 所有线程一律平等,没有父子关系,线程属于进程. 创建线程用 p ...

  10. docker rmi命令-删除image

    rmi 删除image Usage: docker rmi IMAGE [IMAGE...]Remove one or more images -f,--force=falseForce remova ...