lucene

扶醉桌前 提交于 2019-12-02 03:04:54

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

 

  

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!