0

我正在为我的一个搜索应用程序将 solr 从 5.4.3 迁移到 8.11,并成功升级到 7.7.3。但是对于进一步的升级,响应数据的顺序比以前发生了变化。在这里,我尝试将 FunctionScoreQuery 与 DoubleValuesSource 一起使用,因为 CustomScoreQuery 在 7.7.3 中已弃用并在 8 中删除。

下面是我的代码片段(现在我使用的是 solr 8.5.2 和 Lucene 8.5.2)

public class CustomQueryParser extends QParserPlugin {
 @Override
    public QParser createParser(final String qstr, final SolrParams localParams, final SolrParams params,
            final SolrQueryRequest req) {

        return new MyParser(qstr, localParams, params, req);
    }
private static class MyParser extends QParser {

        private Query innerQuery;
        private String queryString;

public MyParser(final String qstr, final SolrParams localParams, final SolrParams params,
                final SolrQueryRequest req) {

            super(qstr, localParams, params, req);

            if (qstr == null || qstr.trim().length() == 0) {
                this.queryString = DEFAULT_SEARCH_QUERY;
                setString(this.queryString);
            } else {
                this.queryString = qstr;
            }

            try {
                if (queryString.contains(":")) {
                    final QParser parser = getParser(queryString, "edismax", getReq());
                    this.innerQuery = parser.parse();
                } else {
                    final QParser parser = getParser(queryString, "dismax", getReq());
                    this.innerQuery = parser.parse();
                }
            } catch (final SyntaxError ex) {
                throw new RuntimeException("Error parsing query", ex);
            }
        }

@Override
        public Query parse() throws SyntaxError{
            final Query query = new MyCustomQuery(innerQuery);
            final CustomValuesSource customValuesSource = new CustomValuesSource(queryString,innerQuery);
           
            final FunctionScoreQuery fsq =  FunctionScoreQuery.boostByValue(query, customValuesSource.fromFloatField("score"));
            return fsq;
        }
}
}

public class MyCustomQuery extends Query {
   @Override
    public Weight createWeight(final IndexSearcher searcher, final ScoreMode scoreMode, final float boost) throws IOException {
        Weight weight;
       if(query == null){
            weight =  new ConstantScoreWeight(this, boost) {
                @Override
                public Scorer scorer(final LeafReaderContext context) throws IOException {
                    return new ConstantScoreScorer(this,score(),scoreMode, DocIdSetIterator.all(context.reader().maxDoc()));
                }
                @Override
                public boolean isCacheable(final LeafReaderContext leafReaderContext) {
                    return false;
                }
            };

        }else {
            weight = searcher.createWeight(query,scoreMode,boost);
        }
        return weight;
    }
}
   
    public class CustomValuesSource extends DoubleValuesSource {  
    
    @Override                                                                                                                            
    public DoubleValues getValues(final LeafReaderContext leafReaderContext,final DoubleValues doubleValues) throws IOException {        
        final DoubleValues  dv = new CustomDoubleValues(leafReaderContext);                                                                                            
        return dv;                                                                                                                       
    }   
    
     class CustomDoubleValues extends DoubleValues { 
    @Override                                                      
    public boolean advanceExact(final int doc) throws IOException {
     final Document document = leafReaderContext.reader().document(doc);
    final List<IndexableField> fields = document.getFields(); 
     for (final IndexableField field : fields) { 
    
    // total_score is being calculated with my own preferences
    document.add(new FloatDocValuesField("score",total_score)); 
    
    //can we include the **score** here? 
    this custom logic which includes score is not even calling. 
    }
                                                                   
    }  
    } 

我尝试了很长时间,但还没有找到一个可行的例子。任何人都可以帮助我并在这里救我。

谢谢你,沙马拉。

4

0 回答 0