前言

使用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. 【APP测试(Android)】--交叉事件

  2. AX_DbSynchronize

    static void KTL_DBSynchronize(Args _args)  {      Dictionary dict;      int idx, lastIdx, totalTable ...

  3. 1.6Eigen中系数运算Reductions, visitors and broadcasting

    Eigen::Matrix2d mat; mat<<,, ,; cout<<"矩阵所有系数之和:"<<mat.sum();//1+2+3+4=1 ...

  4. 我的第一个python爬虫

    我的第一个爬虫,哈哈,纯面向过程 实现目标: 1.抓取本地conf文件,其中的URL地址,然后抓取视频名称以及对应的下载URL 2.抓取URL会单独写在本地路径下,以便复制粘贴下载 废话补多少,代码实 ...

  5. OpenCV从2到3的过渡

    与版本2.4相比,OpenCV 3.0引入了许多新算法和功能.有些模块已被重写,有些已经重组.尽管2.4中的大多数算法仍然存在,但接口可能不同.本节描述了一般性的最显着变化,过渡操作的所有细节和示例都 ...

  6. hdu 4027 Can you answer these queries?[线段树]

    题目 题意: 输入一个 :n  .(1<=n<<100000) 输入n个数    (num<2^63) 输入一个m :代表m个操作    (1<=m<<100 ...

  7. MyBatis 源码分析 - 内置数据源

    1.简介 本篇文章将向大家介绍 MyBatis 内置数据源的实现逻辑.搞懂这些数据源的实现,可使大家对数据源有更深入的认识.同时在配置这些数据源时,也会更清楚每种属性的意义和用途.因此,如果大家想知其 ...

  8. 【接口时序】8、DDR3驱动原理与FPGA实现(一、DDR的基本原理)

    一. 软件平台与硬件平台 软件平台: 1.操作系统:Windows-8.1 2.开发套件:无 3.仿真工具:无 硬件平台: 1. FPGA型号:无 2. DDR3型号:无 二. 存储器的分类 存储器一 ...

  9. 【接口时序】4、SPI总线的原理与Verilog实现

    一. 软件平台与硬件平台 软件平台: 1.操作系统:Windows-8.1 2.开发套件:ISE14.7 3.仿真工具:ModelSim-10.4-SE 硬件平台: 1. FPGA型号:Xilinx公 ...

  10. redis复习

    一起学习...