首页 > Web开发 > 详细

lucene

时间:2019-10-23 18:50:23      阅读:90      评论:0      收藏:0      [点我收藏+]

1. 高亮

  

         HighlighterPtr highlighter;
        if (indices_data_[ctx->current_index_]->high_light_ == 1){
            SimpleHTMLFormatterPtr formatter = newLucene<SimpleHTMLFormatter>(indices_data_[ctx->current_index_]->pre_tag_, indices_data_[ctx->current_index_]->post_tag_);
            LOG(DEBUG) << "pre_tag:" << StringUtils::toUTF8(indices_data_[ctx->current_index_]->pre_tag_); 
            LOG(DEBUG) << "post_tag:" << StringUtils::toUTF8(indices_data_[ctx->current_index_]->post_tag_); 
            QueryPtr qquery = query;
            if (ctx->query_deformed_ == 1){
                string err; 
                qquery = QueryBuilder::BuildQueryEx(analyzer,  ctx->kw_r_, ctx->filters_r_, 0, indices_data_[ctx->current_index_]->min_should_, 
                    reader_filters_[ctx->current_index_], indices_exact_fields_[ctx->current_index_], err, ctx->current_index_, ctx, 1, indices_data_[ctx->current_index_]->search_rule_);
            }    
            if (qquery) {
            QueryScorerPtr scorer = newLucene<QueryScorer>(qquery);
            FragmenterPtr fragmenter = newLucene<SimpleSpanFragmenter>(scorer);
            highlighter = newLucene<Highlighter>(formatter,scorer);  
            }    
            //highlight->setTextFragmenter(fragmenter);  
        }  
                    if (highLight){
                        String value = fid->stringValue();
                        TokenStreamPtr tokenStream = analyzer->tokenStream(fid->name(), newLucene<StringReader>(value)); 
                        item.insert(sattrs[k].c_str(), StringUtils::toUTF8(highlighter->getBestFragment(tokenStream, value))); 
                        
                        /*
                        Collection<TextFragmentPtr> frag = highlighter->getBestTextFragments(tokenStream, value, false, 4);
                        Collection<String> fragTexts(Collection<String>::newInstance());
                        for (int32_t i = 0; i < frag.size(); ++i) {
                            LOG(DEBUG) << "frag:" << i;
                            if (frag[i]){
                                LOG(DEBUG) <<  "frag score:" << frag[i]->getScore();
                                LOG(DEBUG) <<  "frag text:" << StringUtils::toUTF8(frag[i]->toString());
                            }
                            if (frag[i] && frag[i]->getScore() > 0) {
                                fragTexts.add(frag[i]->toString());
                                LOG(DEBUG) << StringUtils::toUTF8(frag[i]->toString());
                            }
                        }
                        if (fragTexts.empty()) {
                            item.insert(sattrs[k].c_str(), StringUtils::toUTF8(value));
                            LOG(DEBUG) << "highlight failed";
                        }
                        else{
                            item.insert(sattrs[k].c_str(), StringUtils::toUTF8(fragTexts[0]));
                        }
                        */
                        
                    }
                    else{
                        item.insert(sattrs[k].c_str(), StringUtils::toUTF8(fid->stringValue()));
                    }

 

  

 

lucene

原文:https://www.cnblogs.com/kuipertan/p/11727880.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!