2019独角兽企业重金招聘Python工程师标准>>>

照着baidu写着Lucene的例子,突然发现new PaodingAnalyzer()不是Lucene包下的类,一查才知道他是paoding解牛的一个用于Lucene的工具,提供中文支持的,需要加入jar包:paoding-analysis.jar。其中要把该jar包中的paoding-dic-home.properties文件中的paoding.dic.home写成paoding.dic.home=F:\\jar\\Paoding\\dic形式(别忘了把注解去了)。dic为解压的dic文件夹。

创建索引例子代码如下:

package lucene;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;public class IndexWriterTest {public static final String indexDir = "src//core//scratch//";public static final String dataDir = "src//lucene//index//";public static void main(String[] args) throws FileNotFoundException,IOException,CorruptIndexException,LockObtainFailedException {File indexFile = new File(indexDir);Analyzer luceneAnalyzer = new StandardAnalyzer();IndexWriter indexWriter = new IndexWriter(dataDir, luceneAnalyzer, true);for(File file : indexFile.listFiles()){Reader reader = new FileReader(file);Document document = new Document();document.add(new Field("path",file.getCanonicalPath(), Field.Store.YES,Field.Index.UN_TOKENIZED));document.add(new Field("contents", reader));indexWriter.addDocument(document);}indexWriter.optimize();indexWriter.close();System.out.println("complete...");}
}

查询代码如下:

package lucene;import java.io.IOException;import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.FSDirectory;public class IndexSearcherTest {public static final String queryString = new String("m");@SuppressWarnings("deprecation")public static void main(String[] args) {try {FSDirectory directory = FSDirectory.getDirectory(IndexWriterTest.dataDir, false);IndexSearcher searcher = new IndexSearcher(directory);Term term = new Term("contents",queryString.toLowerCase());TermQuery luceneQuery = new TermQuery(term);Hits hits = searcher.search(luceneQuery);for(int i = 0; i < hits.length(); i++){Document document = hits.doc(i);System.out.println("File: " + document.get("path"));} } catch (IOException e) {e.printStackTrace();}System.out.println("complete...");}}

转载于:https://my.oschina.net/sourcecoding/blog/51943

Lucene的简单应用相关推荐

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

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

  2. 使用lucene实现简单的全文检索

    本文是一个使用lucene为文本创建并管理索引,根据索引检索文本的简单全文搜索例子. Manven依赖 <dependency><groupId>org.apache.luce ...

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

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

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

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

  5. lucene的简单使用

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

  6. lucene 简单示例

    lucene的简单实例<一>http://www.javaeye.com/topic/39597 lucene的简单实例<二> http://www.javaeye.com/p ...

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

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

  8. 《深入理解Elasticsearch(原书第2版)》——第2章 查询DSL进阶 2.1 Apache Lucene默认评分公式解释...

    本节书摘来自华章计算机<深入理解Elasticsearch(原书第2版)>一书中的第2章,第2.1节,作者 [美]拉斐尔·酷奇(Rafal Ku)马雷克·罗戈任斯基(Marek Rogoz ...

  9. ElasticSearch,Sphinx,Lucene,Solr,Xapian。哪种适合哪种用途? [关闭]

    我目前正在寻找其他搜索方法,而不是拥有一个巨大的SQL查询. 我最近看到了弹性搜索并玩弄了嗖 (搜索引擎的Python实现). 你能说出你的选择理由吗? #1楼 我的sphinx.conf sourc ...

最新文章

  1. 让 Spring Boot 启动更快一点
  2. 在Eclipse中的Android项目里实现代码复用
  3. python3官方最新下载-Python
  4. python零碎知识(8):UserDict 类
  5. ThinkPhp5使用bootstrap样式分页
  6. Journey Of Code组组员贡献率
  7. 活跃了 40 年的 AWK 现在怎么样了?
  8. 使用 jsfl 发布flash IDE 插件
  9. Linux基础-固化命令的方式grep
  10. 《WF编程》系列之16 - 工作流与外部世界:生存周期事件 3.2 工作流与外部世界
  11. (详解)IDEA中使用git教程
  12. `node2vec` `TSNE` 待解决问题
  13. 集丰照明|如何深度解读 LED 标准?
  14. android立方体旋转动画,如何画出一个旋转的立方体
  15. 二层交换机、三层交换机和路由器的区别
  16. docker的常用命令(镜像、容器常用操作)
  17. python3网络爬虫开发实战6.3爬取微博处理since_id
  18. Vue官网下载Vue.js和Vue.min.js
  19. 文献检索与阅读技巧:如何高效的阅读和学习论文文献
  20. ps可以去视频水印h吗

热门文章

  1. 西门子s7-400闭环控制模块_西门子6ES7312-1AE14-0AB0
  2. Python中相见恨晚的技巧(记得收藏)
  3. 学Python,这些内置数据类型总结(数字类型)你可否知道
  4. nurbs建模能用MATLAB么,NURBS(matlab生成nurbs曲线图像)
  5. 鸿蒙系统手机mate40,鸿蒙系统来了!华为Mate40首批,2年前手机将被淘汰
  6. python爬图片_网络爬虫经验:反爬和反反爬
  7. Python底层实现KNN
  8. 在 Linux 中创建和管理归档文件教程在 Linux 中创建和管理归档文件教程
  9. 修复mysql编码错乱的数据_关于MySQL数据库编码修复相关问题
  10. C语言易错题--求最大公约数与最小公倍数之和(辗转相除法)