博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用lucene实现在一个(或者多个)字段中查找多个关键字
阅读量:6995 次
发布时间:2019-06-27

本文共 2899 字,大约阅读时间需要 9 分钟。

  最近跟着师兄们做个项目,我的任务就是负责做个“全文检索”的小模块。用到了Lucene的索引,下面的是其中的用Lucene实现在索引的一个字段(比如文章内容字段)进行查找多个关键字的实例代码。

  1.Lucene说明

  Lucene是非常优秀的成熟的开源的免费的纯java语言的全文索引检索工具包。

  Lucene的的强项在“建立索引”和”搜索“,而不是实现具体的”分词“。Lucene支持对生成索引的进行”增,删,改,查“操作,这比自己建立的索引有了很大的进步。

  可以使用专门的分词程序进行分词,在分词的结果上用Lucene建立索引。

  2.用Lucene实现在一个或者多个字段中的检索

  主要是函数:MultiFieldQueryParser.parse(String[] query,String[] field,Occur[] occ,Analyzer analyzer);

      1)query:要查找的字符串数组

      2)field:要查找的字符串数组对应的字段(当然有可以相同的)

      3)occ:表示对应字段的限制。有三种:Occur.MUST(结果“与”), Occur.MUST_NOT(结果“差”),Occur.SHOULD(结果“或”)

      4)analyzer:对查询数据的分析器,最好与建立索引时用的分析器一致

  3.代码示例

  下面这个程序可以实现在一个字段“contents”中查找多个关键字。稍加修改也可以在多个字段查找多个关键字。

import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.queryParser.MultiFieldQueryParser;import org.apache.lucene.search.BooleanClause.Occur;import org.apache.lucene.search.Hits;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;public class MultiPhraseQuerySearcher {    private static String indexPath = "E:\\Lucene\\index";    // 索引保存目录        public static void createIndex(){    // 建立索引       IndexWriter writer;       try {            writer = new IndexWriter(indexPath,new StandardAnalyzer(),true);                       Field fieldB1 = new Field("contents","今晚的辩题很道地:在我们这些人当中?",Field.Store.YES,Field.Index.TOKENIZED);            Field fieldB2 = new Field("contents","我们为电影《今朝》是一部不错的影片。",Field.Store.YES,Field.Index.TOKENIZED);            Field fieldB3 = new Field("contents","我们到底是啥意思呢?",Field.Store.YES,Field.Index.TOKENIZED);            Document doc1 = new Document();            Document doc2 = new Document();            Document doc3 = new Document();            doc1.add(fieldB1);            doc2.add(fieldB2);            doc3.add(fieldB3);                       writer.addDocument(doc1);            writer.addDocument(doc2);            writer.addDocument(doc3);            writer.close();       }        catch (Exception e) {           e.printStackTrace();       }     }        public static void main(String[] args) {    //contests字段上查找含有"我们","今晚"这两个字段的Doument       Query query;       IndexSearcher searcher;        try {            //生成索引            createIndex();            searcher = new IndexSearcher(indexPath);            //要查找的字符串数组            String [] stringQuery={"我们","今晚"};            //待查找字符串对应的字段            String[] fields={"contents","contents"};            //Occur.MUST表示对应字段必须有查询值, Occur.MUST_NOT 表示对应字段必须没有查询值            Occur[] occ={Occur.MUST,Occur.MUST};                        query=MultiFieldQueryParser.parse(stringQuery,fields,occ,new StandardAnalyzer());            Hits hits = searcher.search(query);            for(int i=0;i

 

    

  

转载于:https://www.cnblogs.com/xudong-bupt/archive/2013/05/08/3065297.html

你可能感兴趣的文章
十、装饰(Decorator)模式 --结构模式(Structural Pattern)
查看>>
WWDC 2013 Session笔记 - UIKit Dynamics入门
查看>>
5月7日——采用第三方页面内容,但是顶部title使用自己的
查看>>
RGBa颜色 css3的Alpha通道支持
查看>>
SSE图像算法优化系列十八:三次卷积插值的进一步SSE优化。
查看>>
unity SystemInfo类 获得电量battery
查看>>
[好文要转]【关于block使用的5点注意事项】
查看>>
Windows如何安装自定义服务
查看>>
095、如何创建Swarm集群?(Swarm02)
查看>>
结对开发地铁
查看>>
附加题
查看>>
this kernel requires an x86-64 cpu,but only detected an i686 cpu
查看>>
extjs4学习-02-导入相关文件
查看>>
python generator iterator和iterable object
查看>>
求二维数组中最大子数组的和
查看>>
SaltStack
查看>>
1. 构建您的第一个应用
查看>>
适配器模式(Adapter)
查看>>
FastStone Capture无法录制系统声音解决方法(win10)
查看>>
wamp 软件 rewrite伪静态支持
查看>>