您的位置:首页 > 其它

Lucene 学习(三):在一个(或者多个)字段中查找多个关键字

2016-07-05 21:31 381 查看
上一篇中,实现了中文分词的操作,在实际试用中,发现众多搜索引擎的搜索框中,我们在不同的key之间使用空格来表示“或”的语义,并且也许我们的关键字在title或者content中,那么现在我们就需要实现“在一个(或者多个)字段中查找多个关键字”的需求。以下便来看看如何实现。

下面代码基于上一篇的代码修改(红色标注地方为重点关注点):

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class IKAnalyzerDemo2 {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//建立索引
String text1 = "IK Analyzer是一个结合词典分词和文法分词的中文分词开源工具包。它使用了全新的正向迭代最细粒度切" +
"分算法。";
String title1="这是标题1";
String text2 = "中文分词工具包可以和lucene是一起使用的";
String title2="这是标题2";
String text3 = "中文分词,你妹";
String title3="这是标题3";
String fieldName = "contents";
Analyzer analyzer = new IKAnalyzer();
RAMDirectory directory = new RAMDirectory();
IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LATEST, analyzer);
IndexWriter indexWriter = new IndexWriter(directory, writerConfig);
Document document1 = new Document();
document1.add(new StringField("ID", "1", Field.Store.YES));
document1.add(new TextField("title", title1, Field.Store.YES));
document1.add(new TextField(fieldName, text1, Field.Store.YES));
indexWriter.addDocument(document1);

Document document2 = new Document();
document2.add(new StringField("ID", "2", Field.Store.YES));
document1.add(new TextField("title", title2, Field.Store.YES));
document2.add(new TextField(fieldName, text2, Field.Store.YES));
indexWriter.addDocument(document2);

Document document3 = new Document();
document3.add(new StringField("ID", "3", Field.Store.YES));
document1.add(new TextField("title", title3, Field.Store.YES));
document3.add(new TextField(fieldName, text3, Field.Store.YES));
indexWriter.addDocument(document3);
indexWriter.close();

//搜索
DirectoryReader indexReader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(indexReader);
//要查找的字符串数组
String [] stringQuery={"可以","标题1"};
//待查找字符串对应的字段
String[] fields={"content","title"};
//Occur.MUST表示对应字段必须有查询值, Occur.MUST_NOT 表示对应字段必须没有查询值,Occur.SHOULD表示对应字段应该存在查询值(但不是必须)
Occur[] occ={Occur.SHOULD,Occur.SHOULD};

try {
Query query = MultiFieldQueryParser.parse(stringQuery, fields, occ, analyzer);
TopDocs topDocs = searcher.search(query, 5);
System.out.println("命中数:"+topDocs.totalHits);
ScoreDoc[] docs = topDocs.scoreDocs;
for(ScoreDoc doc : docs){
Document d = searcher.doc(doc.doc);
System.out.println("内容:"+d.get(fieldName));
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(indexReader != null){
try{
indexReader.close();
}catch (IOException e) {
e.printStackTrace();
}
}

if(directory != null){
try{
directory.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}

}

}
这里特殊地方为:Occur数组,通过这里,便可以实现比较复杂的判断,这里我们暂时都使用Occur.SHOULD(表示关键词有没有都可以),以方便测试,关于此值的用法,后续再继续研究讲解
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: