本文是一个使用lucene为文本创建并管理索引,根据索引检索文本的简单全文搜索例子。

Manven依赖

<dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-core</artifactId><version>6.0.0</version>
</dependency>
<dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-queryparser</artifactId><version>6.0.0</version>
</dependency>
<dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-analyzers-common</artifactId><version>6.0.0</version>
</dependency>
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.5</version>
</dependency>

索引管理

创建索引

public static void createIndex(FileManage file){
//索引保存路径String indexRootPath = getIndexRoot();File f = new File(indexRootPath);if(!f.exists()){f.mkdirs();}String indexPath = indexRootPath+File.separator+File.separator;Directory indexDir = null;IndexWriter indexWriter = null;try{indexDir = FSDirectory.open(new File(indexPath).toPath());Analyzer analyzer = new StandardAnalyzer();IndexWriterConfig config = new IndexWriterConfig(analyzer);indexWriter = new IndexWriter(indexDir, config);File sourceFile = new File(sourceRootPath);String content = org.apache.commons.io.FileUtils.readFileToString(sourceFile, "utf-8");Document doc = new Document();doc.add(new TextField("fileName", file.getPath(), Field.Store.YES));doc.add(new TextField("content", content, Field.Store.YES));indexWriter.addDocument(doc);indexWriter.flush();}catch(Exception e){log.error("create index error|",e);}finally{if(null != indexWriter){try {indexWriter.close();} catch (IOException e) {log.error("",e);}}}
}

删除索引

public static void deleteIndex(FileManage file){String indexRootPath = getIndexRoot();File f = new File(indexRootPath);if(!f.exists()){f.mkdirs();}String indexPath = indexRootPath+File.separator;Directory indexDir = null;IndexWriter indexWriter = null;try{indexDir = FSDirectory.open(new File(indexPath).toPath());Analyzer analyzer = new StandardAnalyzer();IndexWriterConfig config = new IndexWriterConfig(analyzer);indexWriter = new IndexWriter(indexDir, config);indexWriter.deleteDocuments(new Term("fileName", file.getFileName()));indexWriter.flush();}catch(Exception e){log.error("create index error|",e);}finally{if(null != indexWriter){try {indexWriter.close();} catch (IOException e) {log.error("",e);}}}
}

更新索引

public static void updateIndex(FileManage file){String indexRootPath = getIndexRoot();String sourceRootPath = getSourceRoot();File f = new File(indexRootPath);if(!f.exists()){f.mkdirs();}String indexPath = indexRootPath+File.separator;Directory indexDir = null;IndexWriter indexWriter = null;try{indexDir = FSDirectory.open(new File(indexPath).toPath());Analyzer analyzer = new StandardAnalyzer();IndexWriterConfig config = new IndexWriterConfig(analyzer);indexWriter = new IndexWriter(indexDir, config);File sourceFile = new File(file.getPath());String content = org.apache.commons.io.FileUtils.readFileToString(sourceFile, "utf-8");Document doc = new Document();doc.add(new TextField("fileName", file.getFileName(), Field.Store.YES));doc.add(new TextField("content", content, Field.Store.YES));indexWriter.updateDocument(new Term("fileName", file.getFileName()),doc);indexWriter.flush();}catch(Exception e){log.error("create index error|",e);}finally{if(null != indexWriter){try {indexWriter.close();} catch (IOException e) {log.error("",e);}}}
}

根据索引检索

public static List<SearchResult> search(List<FileManage> sourcefiles, String param){if(XaUtil.isEmpty(sourcefiles)){return Collections.EMPTY_LIST;}List<SearchResult> result = new ArrayList<SearchResult>();String indexRootPath = getIndexRoot();File f = new File(indexRootPath);if(!f.exists()){f.mkdirs();}Directory indexDir = null;DirectoryReader ireader = null;try{Analyzer analyzer = new StandardAnalyzer();IndexWriterConfig config = new IndexWriterConfig(analyzer);String[] fieldStr = new String[]{"fileName", "content"};BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST,BooleanClause.Occur.MUST};String indexPath = indexRootPath+File.separator;indexDir = FSDirectory.open(new File(indexPath).toPath());for(FileManage file : sourcefiles){ireader = DirectoryReader.open(indexDir);IndexSearcher isearcher = new IndexSearcher(ireader);String[] queryAry = new String[]{file.getUuidName(), param};Query query = MultiFieldQueryParser.parse(queryAry,fieldStr,flags,analyzer);ScoreDoc[] hits = isearcher.search(query,1000).scoreDocs;Document doc =null;for(ScoreDoc hit : hits){doc = isearcher.doc(hit.doc);SearchResult sr = new SearchResult();sr.setScore(hit.score);sr.setFileName(doc.get("fileName"));result.add(sr);}}}catch (Exception e){log.error("",e);}finally {if(null != indexDir){try {indexDir.close();} catch (IOException e) {log.error("",e);}}if(null != ireader){try {ireader.close();} catch (IOException e) {log.error("",e);}}return result;}
}

(完)

使用lucene实现简单的全文检索相关推荐

  1. lucene php,用PHP调用Lucene包来实现全文检索_php

    由于工作需要,需要使用php实现对网站内大量数量进行全文检索, 而且目前最流行的全文检索的搜索引擎库就是Lucene了, 它是Apache Jakarta的一个子项目,并且提供了简单实用的API, 用 ...

  2. java lucence使用案例_2、Lucene 最简单的使用(小例子)

    在了解了Lucene以后,我打算亲手来做一个Lucene的小例子,这个例子只是Lucene最简单的应用:使用Lucene实现标准的英文搜索: 1.下载Lucene 下载Lucene,到Lucene的官 ...

  3. php lucene索引,用PHP调用Lucene包来实现全文检索_PHP教程

    由于工作需要,需要使用PHP实现对网站内大量数量进行全文检索, 而且目前最流行的全文检索的搜索引擎库就是Lucene了, 它是Apache Jakarta的一个子项目,并且提供了简单实用的API, 用 ...

  4. Lucene 原理分析之-全文检索的一般过程-笔记

    2019独角兽企业重金招聘Python工程师标准>>> 描述了全文检索的一般过程 全文检索大体分两个过程,索引创建 (Indexing) 和搜索索引 (Search) . 索引创建: ...

  5. 使用Lucene开发简单的站内新闻搜索引擎(环境的搭建)

    全文检索的概念 全文检索是计算机程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位置.当用户查询时根据建立的索引查找,类似于通过字典的检索字表查字的过程. Lucen ...

  6. 使用Lucene开发简单的站内新闻搜索引擎(索引的搜索)

    上一篇以及创建好索引,搜索引擎当然少不了搜索.这里为了方便,所以就不把数据保存到数据库,使用Lucene的搜索方法.开始贴代码. 简单的搜索代码 public List<Article> ...

  7. lucene的简单使用

    什么是Lucene? Lucene是一个免费开源的高性能.可伸缩的全文检索引擎. Lucene能做些什么? 为文章建立全文索引,提供高效的查询服务. 使用Lucene建立索引,并查询索引 import ...

  8. Lucene的简单应用

    2019独角兽企业重金招聘Python工程师标准>>> 照着baidu写着Lucene的例子,突然发现new PaodingAnalyzer()不是Lucene包下的类,一查才知道他 ...

  9. springboot ElasticSearch 简单的全文检索高亮

    前阵子和张三丰聊天提到了es.这次正好有机会学习并使用 首先引入依赖 <dependency><groupId>org.springframework.boot</gro ...

最新文章

  1. 刚刚,特斯拉宣布终止私有化,马斯克最终没能说服大股东
  2. ADC0832双通道实现AD电压转换
  3. GridView 实现LinkButton下载文件/附件
  4. 00-02.PHP 网站假设 之 学习PHP语法 [James建站]
  5. 第七章 scikit-learn与机器学习实战
  6. eclipse-indigo 中 user libraries 自动部署 WEB-INF/lib 配置
  7. 计算机控制实验室装置,自控/计控原理实验箱
  8. php网页象棋源码,JS小游戏之象棋暗棋源码详解_javascript技巧
  9. 土建中级工程师考试用书电子版_湖南土建中级职称教材1.pdf
  10. c语言入门手机自学软件,C语言入门学习
  11. 小米6X 线刷兼救砖_解账户锁_纯净刷机包_教程
  12. 微软热招,一起定义未来的搜索引擎!
  13. 全球打工人的抗争!谷歌员工希望更公平,国内只求告别996
  14. 甘恒通:腾讯信鸽海量移动推送服务构建
  15. web前端设计师需要常去的15个设计博客资源站
  16. 嵌入式Linux开发教程:Linux常见命令(上篇)
  17. My97 DatePicker切换日期类型
  18. 【C语言小游戏】——老鼠走迷宫
  19. 百度SEO站群Pofo英文版主题-创意组合和博客主题(WordPress响应式)
  20. Python对齐显示||tabulate函数||wcwidth模块||知道这些就够了

热门文章

  1. 【原】PSD图标素材的全自动切图方法,适用于IOS、安卓、web前端等领域
  2. 想要把日志在两个blog同步太过麻烦了
  3. python编写win 本地reader程序_使用Python、win32api和Acrobat Reader 9打印PDF
  4. android恢复联系人,如何从Android手机恢复联系人[最佳方式]
  5. 请写出至少五个块级元素_Java 面试题(五)
  6. android sensor源码,阅读android有关sensor的源码总结 - JerryMo06的专栏 - CSDN博客
  7. 网络拓扑图画图工具_一款功能强大的免费在线作图工具
  8. 佳能fax_l150如何打印_佳能faxl150说明书下载
  9. 甜品果汁饮品拍摄设计海报,美如蓬莱仙境!
  10. UI设计素材|切换动效,打破界面沉闷感