最近在研究搜索殷勤的全文索引,刚刚有了点眉目,发现还是很实用的,这里放出入门秘籍,大家一起分享!

一,Lucene 简介

Lucene 是一个基于 Java 的全文信息检索工具包,它不是一个完整的搜索应用程序,而是为你的应用程序提供索引和搜索功能。Lucene 目前是 Apache Jakarta 家族中的一个开源项目。也是目前最为流行的基于 Java 开源全文检索工具包。
    目前已经有很多应用程序的搜索功能是基于 Lucene 的,比如 Eclipse 的帮助系统的搜索功能。Lucene 能够为文本类型的数据建立索引,所以你只要能把你要索引的数据格式转化的文本的,Lucene 就能对你的文档进行索引和搜索。比如你要对一些 HTML 文档,PDF 文档进行索引的话你就首先需要把 HTML 文档和 PDF 文档转化成文本格式的,然后将转化后的内容交给 Lucene 进行索引,然后把创建好的索引文件保存到磁盘或者内存中,最后根据用户输入的查询条件在索引文件上进行查询。不指定要索引的文档的格式也使 Lucene 能够几乎适用于所有的搜索应用程序。

我们先看看官方的架构图:

二,官网demo

1、首先到官网下载lucene-4.10.3.zip

官方网址:http://lucene.apache.org/

2、解压zip包,其中有一个demo有一个是lucene-xml-query-demo.war可以放到tomcat 安装目录的webapps中

3、将tomcat服务器开启输入localhost:8080/lucene-xml-query-demo将会出现界面但是点击查询会报java.lang.ClassNotFoundException:org.apache.lucene.xmlparser.webdemo.FormBasedXmlQueryDemo这个错误。这个原因是新版本中FormBasedXmlQueryDemo的路径

变了,这时你就需要到该项目的web.xml中将 <servlet-class> org.apache.lucene.xmlparser.webdemo.FormBasedXmlQueryDemo </servlet-class>

更改为 <servlet-class>org.apache.lucene.demo.xmlparser.FormBasedXmlQueryDemo</servlet-class>

然后把lucene-4.1.0解压包下 analysis\common\lucene-analyzers-common-4.10.2.jar和 sandbox\lucene-sandbox-4.10.2.jar

这两个文件拷贝到WEB-INF\lib文件夹下面,这时在点击查询就不会出现问题了输入java查询结果如下

三、简单实现

lucene基本工作原理简单可以理解为创建索引,而根据索引查询

下面是简单的例子

TxtFileInderxer作用是将D:/luceneData中所有的.txt文件建立索引并将所有的索引存放在D:/luceneIndex中

package org.com.test;import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.Date;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.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;public class TxtFileIndexer {public static void main(String[] args) throws Exception {// indexDir is the directory that hosts Lucene's index filesFile indexDir = new File("D:\\luceneIndex");// dataDir is the directory that hosts the text files that to be indexedFile dataDir = new File("D:\\luceneData");// Analyzer luceneAnalyzer = new// StandardAnalyzer(Version.LUCENE_4_10_2);// 对文档进行分词Analyzer luceneAnalyzer = new StandardAnalyzer();File[] dataFiles = dataDir.listFiles();IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3, luceneAnalyzer);// 创建索引IndexWriter indexWriter = new IndexWriter(FSDirectory.open(indexDir),indexWriterConfig);// IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,// true);long startTime = new Date().getTime();for (int i = 0; i < dataFiles.length; i++) {if (dataFiles[i].isFile()&& dataFiles[i].getName().endsWith(".txt")) {System.out.println("Indexing file "+ dataFiles[i].getCanonicalPath());// 封装document对象Document document = new Document();Reader txtReader = new FileReader(dataFiles[i]);document.add(new TextField("path", dataFiles[i].getCanonicalPath(), Store.YES));// document.add(Field.Text("contents", txtReader));document.add(new TextField("contents", txtReader));indexWriter.addDocument(document);}}indexWriter.commit();// indexWriter.optimize();indexWriter.close();long endTime = new Date().getTime();System.out.println("It takes " + (endTime - startTime)+ " milliseconds to create index for the files in directory "+ dataDir.getPath());}}

TxtFileSearcher作用是从D:/luceneIndex中读取索引并查询.txt文件中含有lucene的文件

package org.com.test;import java.io.File;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;public class TxtFileSearcher {public static void main(String[] args) throws Exception {String queryStr = "java";// This is the directory that hosts the Lucene indexFile indexDir = new File("D:\\luceneIndex");Directory directory = FSDirectory.open(indexDir);// FSDirectory fsDirectory = FSDirectory.open(indexDir);IndexReader indexReader = IndexReader.open(directory);// FSDirectory directory = FSDirectory.getDirectory(indexDir,false);// IndexReader indexReader = IndexReader.open(fsDirectory);IndexSearcher indexSearcher = new IndexSearcher(indexReader);// IndexSearcher searcher = new IndexSearcher(indexReader);if (!indexDir.exists()) {System.out.println("The Lucene index is not exist");return;}Term term = new Term("contents", queryStr.toLowerCase());TermQuery luceneQuery = new TermQuery(term);// Hits hits = searcher.search(luceneQuery);TopDocs topDocs = indexSearcher.search(luceneQuery, 1000);ScoreDoc[] scoreDocs = topDocs.scoreDocs;if (scoreDocs == null || scoreDocs.length == 0) {System.out.println("The  Lucene index is not exist");}for (int i = 0; i < scoreDocs.length; i++) {Document document = indexSearcher.doc(scoreDocs[i].doc);System.out.println("File: " + document.get("path"));}indexReader.close();}
}

执行结果如下:

以"D:\\luceneData"为数据源建立索引:

按“java”为关键字搜索结果:

总结:

在我们的逐渐成熟,走向大牛的技术生涯中,一直有一个现象,我们愁眉不展研究了N个日日夜夜的结果,某牛分分钟就找到的一个jar包实现了我们的功能,而且比较完善,每每遇到这个现象,我就会想起《荀子·劝学》中的名句:

吾尝终日而思矣,不如须臾之所学也;吾尝跂而望矣,不如登高之博见也。登高而招,臂非加长也,而见者远;顺风而呼,声非加疾也,而闻者彰。假舆马者,非利足也,而致千里;假舟楫者,非能水也,而绝江河。君子生非异也,善假于物也。

千年前的古人,已经教育我们,作为君子,应该学习站在巨人的肩膀上,人类发明文字,就是为了后人可以继承,有了继承,我们以几千年的文明塑造了无与伦比的发展速度,且这个速度一直被刷新!程序,何尝不是这样!!!

lucene4.10.3入门相关推荐

  1. 教程 | 10分钟入门简笔画(哆啦A梦篇)

    多啦A梦主题曲 - 腾讯视频 从小就喜欢小叮当这个形象,不知道为什么后来改成了多啦A梦. 多啦A梦之歌,时常萦绕在耳旁: 心中有许多愿望,能够实现有多棒,只有哆啦a梦可以带着我实现梦想 可爱圆圆胖脸庞 ...

  2. 大神的日语学习方法,10天入门,2个月达到N3水平,7个月突破N1

    标题:大神的日语学习方法,10天入门,2个月达到N3水平,7个月突破N1,我先收藏起来,我晕,颠覆了我学习语言的思维,按照这个办法可以去攻克其他语种了         一.前言  很多同学问我学习日语 ...

  3. 10分钟入门 ANSA API

      扫描下方二维码关注我的微信公众号 - CAE软件二次开发Lab阅读全文! 文章目录 10分钟入门 ANSA API Script Editor (脚本编辑器) Modules(模块) 在ANSA中 ...

  4. 微信小程序怎么在wxml中插入多个图片_白云工商带你10分钟入门微信小程序开发...

    10分钟入门微信小程序开发 程序开发难吗? No!来白云工商带你10分钟入门微信小程序开发! 英语不好能学好程序开发吗? 当然能,程序开发常见的也就那几个单词,只要热爱,非常简单! 欣赏 首先,来欣赏 ...

  5. 教程 | 10分钟入门简笔画(彩色小插画)

    你好,色彩. BY:铃铛子 马克笔的笔触感觉: BY:铃铛子 由于大家写字的时候,为了笔锋,会起笔停顿一下,落笔再停顿一下,但是运行马克笔的时候不要这样,起笔确定了就画线,落笔除非必要,否则不停顿. ...

  6. 【强烈推荐】网络安全10本入门必看书籍

    前言 对于初学者来说,了解网络安全的入门知识是非常重要的.以下是我推荐的10本入门网络安全必看的书籍 1.<黑客攻防技术宝典> 作者:余洪涛,出版社:清华大学出版社 这本书是网络安全初学者 ...

  7. 教程 | 10分钟入门简笔画12(创意小插画)

    我的一家 很多宝宝说想学创作,今天跟大家分享简笔画中的联想小插画. 所谓联想小插画,就是想到哪里画到哪里.首先确定一个主题,抓住主要元素,然后用装饰性字体.元素.花边来丰富画面.我尝试创作了一张My ...

  8. 教程 | 10分钟入门禅绕画 3

    禅绕装饰画是一种意识流装饰画,也是一种有趣随性的涂鸦,笔触可以天马星空随意走动. 禅绕画的构图技巧:重复.对称.均衡.重叠.勾线.肌理等.所有技巧的组合可以使得画面节奏和谐.疏密有度.节奏韵律恰如其分 ...

  9. 【推荐】网络安全10本入门必看书籍

    前言 对于初学者来说,了解网络安全的入门知识是非常重要的.以下是我推荐的10本入门网络安全必看的书籍 1.<黑客攻防技术宝典> 作者:余洪涛,出版社:清华大学出版社 这本书是网络安全初学者 ...

最新文章

  1. 【完结】如何学习AutoML在模型优化中的应用,这12篇文章可以作为一个参考
  2. spring boot整合JPA实现多条件查询并分页
  3. linux遍历文件目录 链表形式
  4. python操作数据库 封装类
  5. HANA report creation implementation go through
  6. 对check list理解
  7. 请问重定向与请求转发有什么区别?
  8. InDesign 软件教程,如何自定义工作区?
  9. 计算机科技想象作文600字,科技想象作文600字四篇
  10. 关于代付和分账系统的区别
  11. php怎么接入微支付宝支付,php 微信公众号接入支付宝支付
  12. 服务器存档修改器,太吾绘卷存档修改器v2.6
  13. top邮箱怎么登录,解决方案
  14. 大数据开发常用命令大全 大全
  15. 远程连接云服务器中的mysql数据库_云服务器远程连接mysql数据库
  16. 纪录 vue 滚动条失效
  17. 【计算机网络】计算机网络
  18. vb.net如何打包exe安装文件
  19. java学习笔记2(datawhale教程):运算符和表达式、流程控制、数组
  20. 二、Nacos下载和安装

热门文章

  1. 苹果禁用UDID的后续
  2. android8.1 屏蔽通知提示
  3. 【转】游戏buff设计参见
  4. 微信小程序中 input空间输入文字如何清空
  5. javscript执行顺序(笔记)
  6. [译]游戏开发者的试炼
  7. 阿里云asp主机 后台登录一直提示验证码错误_Lazada官方最全开店指南首发!云开店之入驻篇,快速入驻只需四步...
  8. 职场7个建议 轻松抛弃同龄人
  9. 持久内存(PMem)科普
  10. 【JavaWeb学习】—特殊字符(二)