本文主要是介绍Lucene多字段查询高亮显示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 在百度搜索的时候,查询的关键词会高亮显示
- 在搜索一个关键词的时候,有可能这个关键词在title和content中,搜索的时候要把结果全部显示出来
实例说明
package com.bart.lucene.mutilseacher;import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
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.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.Scorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.junit.Test;
import com.bart.lucene.entity.Article;
import com.bart.lucene.util.LuceneUtils;/*** Article多条件搜索加高亮显示* @author hp*/
public class ArticleDao {/*** 高亮显示* 查询索引库中所有的记录* @throws Exception */@Testpublic void findAll() throws Exception{String keyword = "显卡";List<Article>articleList = new ArrayList<Article>(); IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.getDirectory());//默认之搜索一个字段,单条件搜索//QueryParser queryParser = new QueryParser(LuceneUtils.getVersion(),"content",LuceneUtils.getAnalyzer());//搜索多个字段,多条件搜索QueryParser queryParser2 = new MultiFieldQueryParser(LuceneUtils.getVersion(),new String[]{"title","content"},LuceneUtils.getAnalyzer());//Query query = queryParser.parse(keyword);Query query = queryParser2.parse(keyword);TopDocs topDocs = indexSearcher.search(query,100);//以下代码对查询结果进行高亮显示// 1.格式化对象,设置前缀和后缀Formatter formatter = new SimpleHTMLFormatter("<font color='red'>","</font>");// 2.关键词对象Scorer scorer = new QueryScorer(query);// 3. 高亮对象Highlighter highlighter = new Highlighter(formatter, scorer);for(int i=0;i<topDocs.scoreDocs.length;i++){ScoreDoc scoreDoc = topDocs.scoreDocs[i];int no = scoreDoc.doc;//关键词没有高亮Document document = indexSearcher.doc(no);// 1. 关键词添加高亮String titleHighLight = highlighter.getBestFragment(LuceneUtils.getAnalyzer(),"title",document.get("title"));String contentHighLight = highlighter.getBestFragment(LuceneUtils.getAnalyzer(),"content",document.get("content"));// 2. 将高亮的结果再次封装到document对象中document.getField("title").setValue(titleHighLight);document.getField("content").setValue(contentHighLight);Article article = LuceneUtils.document2JavaBean(document,Article.class);articleList.add(article);}//遍历for(Article a : articleList){System.out.println(a);}}/*** 增加document对象到对象索引库中* @throws Exception*/@Testpublic void testCreate() throws Exception{Article article = new Article(1,"显卡","显卡是电脑的核心部件",10);Document document = LuceneUtils.javaBean2Document(article);IndexWriter indexWriter = new IndexWriter(LuceneUtils.getDirectory(), LuceneUtils.getAnalyzer(), LuceneUtils.getMaxFieldLength());indexWriter.addDocument(document);//设置当插入三条记录的时候合并到同一条记录,默认是10个缓存就合并索引的cfs文件indexWriter.setMergeFactor(3);
// //合并记录,不管插入多少条
// indexWriter.optimize();indexWriter.close();}}
输出结果:
Article [id=1, title=显卡, content=显卡是电脑的核心部件, count=10]
这篇关于Lucene多字段查询高亮显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!