Elasticsearch function_score 打分源代码跟踪

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch function_score 打分源代码跟踪相关的知识,希望对你有一定的参考价值。

类注册器 IndicesModule

private void registerBuiltinQueryParsers() {
        registerQueryParser(MatchQueryParser.class);
        registerQueryParser(MultiMatchQueryParser.class);
        registerQueryParser(NestedQueryParser.class);
        registerQueryParser(HasChildQueryParser.class);
        registerQueryParser(HasParentQueryParser.class);
        registerQueryParser(DisMaxQueryParser.class);
        registerQueryParser(IdsQueryParser.class);
        registerQueryParser(MatchAllQueryParser.class);
        registerQueryParser(QueryStringQueryParser.class);
        registerQueryParser(BoostingQueryParser.class);
        registerQueryParser(BoolQueryParser.class);
        registerQueryParser(TermQueryParser.class);
        registerQueryParser(TermsQueryParser.class);
        registerQueryParser(FuzzyQueryParser.class);
        registerQueryParser(RegexpQueryParser.class);
        registerQueryParser(RangeQueryParser.class);
        registerQueryParser(PrefixQueryParser.class);
        registerQueryParser(WildcardQueryParser.class);
        registerQueryParser(FilteredQueryParser.class);
        registerQueryParser(ConstantScoreQueryParser.class);
        registerQueryParser(SpanTermQueryParser.class);
        registerQueryParser(SpanNotQueryParser.class);
        registerQueryParser(SpanWithinQueryParser.class);
        registerQueryParser(SpanContainingQueryParser.class);
        registerQueryParser(FieldMaskingSpanQueryParser.class);
        registerQueryParser(SpanFirstQueryParser.class);
        registerQueryParser(SpanNearQueryParser.class);
        registerQueryParser(SpanOrQueryParser.class);
        registerQueryParser(MoreLikeThisQueryParser.class);
        registerQueryParser(WrapperQueryParser.class);
        registerQueryParser(IndicesQueryParser.class);
        registerQueryParser(CommonTermsQueryParser.class);
        registerQueryParser(SpanMultiTermQueryParser.class);

        registerQueryParser(FunctionScoreQueryParser.class);
        //注意此处

        if (ShapesAvailability.JTS_AVAILABLE) {
            registerQueryParser(GeoShapeQueryParser.class);
        }
    }

调用分析器 FunctionScoreQueryParser

@Override
    public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
         ...
        // handle cases where only one score function and no filter was
        // provided. In this case we create a FunctionScoreQuery.
        if (filterFunctions.size() == 0 || filterFunctions.size() == 1 && (filterFunctions.get(0).filter == null || Queries.isConstantMatchAllQuery(filterFunctions.get(0).filter))) {
            ScoreFunction function = filterFunctions.size() == 0 ? null : filterFunctions.get(0).function;
            FunctionScoreQuery theQuery = new FunctionScoreQuery(query, function, minScore);
            if (combineFunction != null) {
                theQuery.setCombineFunction(combineFunction);
            }
            theQuery.setBoost(boost);
            theQuery.setMaxBoost(maxBoost);
            return theQuery;
            // in all other cases we create a FiltersFunctionScoreQuery.
        } else {
            FiltersFunctionScoreQuery functionScoreQuery = new FiltersFunctionScoreQuery(query, scoreMode,
                    filterFunctions.toArray(new FiltersFunctionScoreQuery.FilterFunction[filterFunctions.size()]), maxBoost, minScore);
            if (combineFunction != null) {
                functionScoreQuery.setCombineFunction(combineFunction);
            }
            functionScoreQuery.setBoost(boost);
            return functionScoreQuery;
        }
    }

其中 FunctionScoreQuery theQuery = new FunctionScoreQuery(query, function, minScore); 会调用 类FunctionScoreQuery

技术分享图片

调用下面代码

技术分享图片

接着调用 ScriptScoreFunction类的
技术分享图片

接着调用NativeScriptEngineService 类的

技术分享图片

这里会自定义的打分插件进行编译 缓存 并保存实例, 自定义插件样例如下

技术分享图片

CombineScript 会处理一下参数和重写run方法。

而上面的截图里的
double result = leafScript.runAsDouble(); 会调用此run方法,将最终的得分返回给上层调用。

以上是关于Elasticsearch function_score 打分源代码跟踪的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch 教程

Elasticsearch 基本概念

Elasticsearch 学习笔记 Elasticsearch及Elasticsearch head安装配置

Elasticsearch - Elasticsearch 8.X;Elasticsearch 8.X集群

Elasticsearch 安装

ElasticSearch知识概括