• 搜索结果高亮


    package loaderman.highlighter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    import loaderman.entity.Article;
    import loaderman.util.LuceneUtil;
    import org.apache.lucene.document.Document;
    
    import org.apache.lucene.index.IndexWriter;
    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.*;
    import org.junit.Test;
    
    /**
     * 搜索结果中关键字高亮
     * @author AdminTC
     */
    public class ArticleDao {
        /**
         * 增加document对象索引库中
         */
        @Test
        public void add() throws Exception{
            Article article = new Article(1,"学习","学习使人快乐...",10);
            Document document = LuceneUtil.javabean2document(article);
            IndexWriter indexWriter = new IndexWriter(LuceneUtil.getDirectory()    ,LuceneUtil.getAnalyzer(),LuceneUtil.getMaxFieldLength());
            indexWriter.addDocument(document);
            indexWriter.close();
        }
    
    
    
    
    
    
        @Test
        public void findAll() throws Exception{
            String keywords = "培训";
            List<Article> articleList = new ArrayList<Article>();
    
            QueryParser queryParser = new QueryParser(LuceneUtil.getVersion(),"content",LuceneUtil.getAnalyzer());
            Query query = queryParser.parse(keywords);
            IndexSearcher indexSearcher = new IndexSearcher(LuceneUtil.getDirectory());
            TopDocs topDocs = indexSearcher.search(query,100);
    
    
            //以下代码对内容中含有关键字的字符串高亮显示
    
            //格式对象
            Formatter formatter = new SimpleHTMLFormatter("<font color='red'>","</font>");
            //关键字对象
            Scorer scorer = new QueryScorer(query);
            //高亮对象
            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);
    
                //关键字高亮
                String titleHighlighter = highlighter.getBestFragment(LuceneUtil.getAnalyzer(),"title",document.get("title"));
                String contentHighlighter = highlighter.getBestFragment(LuceneUtil.getAnalyzer(),"content",document.get("content"));
    
                //将高亮后的结果再次封装到document对象中
                document.getField("title").setValue(titleHighlighter);
                document.getField("content").setValue(contentHighlighter);
    
                Article article = (Article) LuceneUtil.document2javabean(document,Article.class);
                articleList.add(article);
            }
            for(Article a : articleList){
                System.out.println(a);
            }
    
        }
    }
  • 相关阅读:
    PHP javascript cookie
    angular.js初探
    熟悉陌生框架或代码, 产品设计小结
    question2answer论坛框架分析及web开发思考
    一个很好用的系统管理的命令lsof(转载)
    Linux nc命令用法收集
    Linux IO实时监控iostat命令详解(转载)
    AIX中查找端口号和进程
    bash之局部变量与子shell(转载)
    Linux下产生随机密码10方法
  • 原文地址:https://www.cnblogs.com/loaderman/p/10059692.html
Copyright © 2020-2023  润新知