刚开始做全文检索也是找了很多资料但是网上的都不是很齐全luence是个很不多的工具

Lucene4.0的官网文档:http://lucene.apache.org/core/4_0_0/core/overview-summary.html

这个工具跟新很快很多风格不一样比如,以前版本的申请IndexWriter时,是这样的:

 IndexWriter indexWriter  =   new IndexWriter(indexDir,luceneAnalyzer, true );
但是4.0,我们需要配置一个conf,把配置内容放到这个对象中:
  IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer);
IndexWriter iwriter = new IndexWriter(directory, config); 这是其中最常用的五个文件:

  第一个,也是最重要的,Lucene-core-4.0.0.jar,其中包括了常用的文档,索引,搜索,存储等相关核心代码。


  第二个,Lucene-analyzers-common-4.0.0.jar,这里面包含了各种语言的词法分析器,用于对文件内容进行关键字切分,提取。


  第三个,Lucene-highlighter-4.0.0.jar,这个jar包主要用于搜索出的内容高亮显示。


  第四个和第五个,Lucene-queryparser-4.0.0.jar,提供了搜索相关的代码,用于各种搜索,比如模糊搜索,范围搜索,等等。


什么是全文检索
比如,我们一个文件夹中,或者一个磁盘中有很多的文件,记事本、world、Excel、pdf,我们想根据其中的关键词搜索包含的文件。
例如,我们输入Lucene,所有内容含有Lucene的文件就会被检查出来。这就是所谓的全文检索。 Lucene的使用主要体现在两个步骤:

  1 创建索引,通过IndexWriter对不同的文件进行索引的创建,并将其保存在索引相关文件存储的位置中。


  2 通过索引查寻关键字相关文档。

下面针对官网上面给出的一个例子,进行分析:
 1   Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
2
3 // Store the index in memory:
4 Directory directory = new RAMDirectory();
5 // To store an index on disk, use this instead:
6 //Directory directory = FSDirectory.open("/tmp/testindex");
7 IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer);
8 IndexWriter iwriter = new IndexWriter(directory, config);
9 Document doc = new Document();
10 String text = "This is the text to be indexed.";
11 doc.add(new Field("fieldname", text, TextField.TYPE_STORED));
12 iwriter.addDocument(doc);
13 iwriter.close();
14
15 // Now search the index:
16 DirectoryReader ireader = DirectoryReader.open(directory);
17 IndexSearcher isearcher = new IndexSearcher(ireader);
18 // Parse a simple query that searches for "text":
19 QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "fieldname", analyzer);
20 Query query = parser.parse("text");
21 ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
22 assertEquals(1, hits.length);
23 // Iterate through the results:
24 for (int i = 0; i < hits.length; i++) {
25 Document hitDoc = isearcher.doc(hits[i].doc);
26 assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
27 }
28 ireader.close();
29 directory.close();

  


索引的创建


  首先,我们需要定义一个词法分析器。


  比如一句话,“我爱我们的中国!”,如何对他拆分,扣掉停顿词“的”,提取关键字“我”“我们”“中国”等等。这就要借助的词法分析器Analyzer来实现。这里面使用的是标准的词法分析器,如果专门针对汉语,还可以搭配paoding,进行使用。


1 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);

  参数中的Version.LUCENE_CURRENT,代表使用当前的Lucene版本,本文环境中也可以写成Version.LUCENE_40。


  


  第二步,确定索引文件存储的位置,Lucene提供给我们两种方式:


  1 本地文件存储


Directory directory = FSDirectory.open("/tmp/testindex");

  2 内存存储


Directory directory = new RAMDirectory();

  可以根据自己的需要进行设定。


   


  第三步,创建IndexWriter,进行索引文件的写入。


IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer);
IndexWriter iwriter = new IndexWriter(directory, config);

  这里的IndexWriterConfig,据官方文档介绍,是对indexWriter的配置,其中包含了两个参数,第一个是目前的版本,第二个是词法分析器Analyzer。


  


  第四步,内容提取,进行索引的存储。


Document doc = new Document();
String text = "This is the text to be indexed.";
doc.add(new Field("fieldname", text, TextField.TYPE_STORED));
iwriter.addDocument(doc);
iwriter.close();

  第一行,申请了一个document对象,这个类似于数据库中的表中的一行。


  第二行,是我们即将索引的字符串。


  第三行,把字符串存储起来(因为设置了TextField.TYPE_STORED,如果不想存储,可以使用其他参数,详情参考官方文档),并存储“表明”为"fieldname".


  第四行,把doc对象加入到索引创建中。


  第五行,关闭IndexWriter,提交创建内容。


  


  这就是索引创建的过程。


关键字查询:


  第一步,打开存储位置


DirectoryReader ireader = DirectoryReader.open(directory);

  第二步,创建搜索器


IndexSearcher isearcher = new IndexSearcher(ireader);

  第三步,类似SQL,进行关键字查询


QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "fieldname", analyzer);
Query query = parser.parse("text");
ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
assertEquals(1, hits.length);
for (int i = 0; i < hits.length; i++) {
Document hitDoc = isearcher.doc(hits[i].doc);
assertEquals("This is the text to be indexed.",hitDoc.get("fieldname"));
}

  这里,我们创建了一个查询器,并设置其词法分析器,以及查询的“表名“为”fieldname“。查询结果会返回一个集合,类似SQL的ResultSet,我们可以提取其中存储的内容。


  关于各种不同的查询方式,可以参考官方手册,或者推荐的PPT


  第四步,关闭查询器等。


ireader.close();
directory.close();

最新文章

  1. webBrowser 加载网页
  2. Android自定义控件5--轮播图广告ViewPager基本实现
  3. VMware安装Elementary OS 后不能上网问题解决方法
  4. c++ 类型安全
  5. HTML5探索之datalist研究
  6. DB2 函数大全
  7. sql的join用法
  8. telnet 时代的 bbs
  9. (一)CSS3动画应用 - CSS3 实现 侧边栏展开收起
  10. bzoj:4762: 最小集合
  11. android TextView 垂直自动滚动字幕实现
  12. robotframework之用cmd去执行用例
  13. 如何看iOS崩溃日志
  14. Jmeter分布式部署测试-----远程连接多台电脑做压力性能测试
  15. 第三方登陆——QQ登陆详解
  16. Determine YARN and MapReduce Memory Configuration Settings
  17. 20150421 作业5 四则运算 测试与封装 5.1 5.2(doing)
  18. 使用 &lt;!DOCTYPE html&gt; 让 &lt;div&gt;&lt;img&gt;&lt;/div&gt;中的图片下面产生几个像素的空白间隔
  19. Ruby学习笔记7: 添加身份验证(adding Authentication)
  20. CF576E Painting Edges

热门文章

  1. 看完这篇 你就能完全操作git 远程分支的增、删、改、查了
  2. PHP 数组使用之道
  3. Win7解决无法在资源管理器中连接FTP问题
  4. RNNCell使用
  5. 2018/08/23 cstring中memset()函数的运用
  6. codeforces 407 div1 B题(Weird journey)
  7. 原生js获取复选框的值
  8. C51 原创电子琴 (蜂鸣器/计时器/中断/矩阵按键)
  9. SSM java.lang.NullPointerException
  10. BNUOJ 6719 Simpsons’ Hidden Talents