搜索结果高亮

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);
        }

    }
}

 

转载于:https://www.cnblogs.com/loaderman/p/10059692.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用SearchView和RecyclerView来实现Android中的搜索功能,并且在搜索结果中高亮显示匹配的文本。下面是实现的简要步骤: 1. 首先,在你的布局文件中添加SearchView和RecyclerView。 2. 在Activity或Fragment中,找到SearchView并设置搜索监听器。在监听器的onQueryTextChange方法中,获取用户输入的搜索关键字,并更新RecyclerView的数据源。 3. 在RecyclerView的Adapter中,实现一个过滤方法来根据搜索关键字过滤数据源。这个过滤方法可以使用String类的contains方法来判断是否存在匹配的文本。 4. 在过滤方法中,你可以使用SpannableString类来对匹配的文本进行高亮处理。使用ForegroundColorSpan类来设置高亮文本的颜色。 这是一个简单的示例代码,帮助你更好地理解: ```java public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private List<String> dataList; private List<String> filteredDataList; private String searchText; // Adapter构造函数和其他方法... public void filter(String text) { searchText = text.toLowerCase(); filteredDataList.clear(); if (searchText.isEmpty()) { filteredDataList.addAll(dataList); } else { for (String item : dataList) { if (item.toLowerCase().contains(searchText)) { filteredDataList.add(item); } } } notifyDataSetChanged(); } // ViewHolder和其他方法的实现... @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { String item = filteredDataList.get(position); // 高亮处理 if (!searchText.isEmpty()) { int startPos = item.toLowerCase().indexOf(searchText); int endPos = startPos + searchText.length(); if (startPos != -1) { SpannableString spannableString = new SpannableString(item); ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.RED); spannableString.setSpan(colorSpan, startPos, endPos, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); holder.textView.setText(spannableString); } else { holder.textView.setText(item); } } else { holder.textView.setText(item); } } } ``` 这只是一个简单的示例,你可以根据实际需求进行修改和扩展。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值