如何获取elasticsearch分数结果详细信息?

lokaqttq  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(537)

我有一个弹性索引,可以保存一些项目。结构如下。

public class items
{
    public string item_no { get; set; }
    public string category { get; set; }
    public int campaign { get; set; }
    public int in_stock { get; set; }
    // Next properties only include [a-z0-9]. Not any other characters
    public string score_item_no { get; set; }   
    public string score_group_one { get; set; }
    public string score_group_two { get; set; }
    public string score_description { get; set; }
    public string score_all_fields { get; set; } /* score_item_no + score_group_one + score_group_two + score_description and something else */
}

public class ClassForScore
{
        public int id { get; set; }
        public string item_no { get; set; }
}

我必须从结果中过滤无用的记录。我决定使用score选项并创建一个函数来计算平均分数。所以首先我调用ElasticSearchforScores,然后用minscore参数调用。我找不到任何过滤无用结果的方法对此有什么建议吗?这是第一个问题。
第二个:第一个score调用返回7条记录。每一张唱片都有不同的分数。例如,第一条记录有1100个分数。但我想知道这1100是从哪里来的?1000来自评分项目编号,100来自评分组1,或500来自评分组1匹配5个部分,其中500来自评分组2匹配5个部分,100来自评分描述匹配2个部分。有没有办法找到分数的细节?

QueryContainer queryContainsAnd = new WildcardQuery() { Field = "score_all_fields", Value = "*" + mykeyword + "*" };
    QueryContainer queryEqualsOr =  new TermQuery() { Field = "category", Value = *something1* };
    queryEqualsOr |=  new TermQuery() { Field = "category", Value = *something2* };
    QueryContainer queryEqualsAnd = new TermQuery() { Field = "campaign", Value = 1 };
    queryEqualsAnd &= new TermQuery() { Field = "in_stock", Value = 1 };

    QueryContainer mainQuery = queryContainsAnd & queryEqualsAnd & queryEqualsOr;

    Func<QueryContainerDescriptor<ClassForScore>, QueryContainer> fo = funcScoreParam(new ClassForScore(), filterItemNo, filterGroupOne, filterGroupTwo, filterDescription, mainQuery);
    ISearchResponse<ClassForScore> srcSkor = elasticClient.Search<ClassForScore>(s => s
        .RequestConfiguration(r => r.DisableDirectStreaming())
        .Query(fo)
        .Size(100)
    );
    IReadOnlyCollection<IHit<ClassForScore>> lstSkor = srcSkor.Hits;
    double? dblSkorAvg = 0;
    // Some calculation..
    //.....
    Func<QueryContainerDescriptor<items>, QueryContainer> fo2 = funcScoreParam(new ClassForScore(), filterItemNo, filterGroupOne, filterGroupTwo, filterDescription, mainQuery);
    ISearchResponse<items> srcResult = elasticClient.Search<items>(s => s
        .RequestConfiguration(r => r.DisableDirectStreaming())
        .From(0)
        .Size(100)
        .Sort(S => S.Descending(SortSpecialField.Score).Ascending(r => r.item_no))
        .MinScore(dblSkorAvg)
        .Query(fo2)
    );

    private Func<QueryContainerDescriptor<T>, QueryContainer> funcScoreParam<T>(T nesne, QueryContainer filterItemNo, QueryContainer filterGroupOne, QueryContainer filterGroupTwo, QueryContainer filterDescription, QueryContainer mainQuery) where T : class
    {
        return new Func<QueryContainerDescriptor<T>, QueryContainer>(q => q
            .FunctionScore(fsc => fsc
                .BoostMode(FunctionBoostMode.Sum)
                .ScoreMode(FunctionScoreMode.Sum)
                .Functions(fu => fu
                        .Weight(w => w
                            .Weight(1000)
                            .Filter(wf => wf
                            .Bool(bb => bb
                            .Must(filterItemNo))
                            ))
                        .Weight(w => w
                            .Weight(100)
                            .Filter(wf => wf
                            .Bool(bb => bb
                            .Must(filterGroupOne))
                            ))
                        .Weight(w => w
                            .Weight(100)
                            .Filter(wf => wf
                            .Bool(bb => bb
                            .Must(filterGroupTwo)) 
                            ))
                        .Weight(w => w
                            .Weight(50)
                            .Filter(wf => wf
                            .Bool(bb => bb
                            .Must(filterDescription))
                            ))
                    )
                    .Query(q2 => q2
                        .Bool(b => b
                        .Should(mainQuery))
                    )
        ));
    }
dldeef67

dldeef671#

你可以用 explain 参数返回有关每次命中分数计算的详细信息

ISearchResponse<items> srcResult = elasticClient.Search<items>(s => s
    .RequestConfiguration(r => r.DisableDirectStreaming())
    .From(0)
    .Size(100)
    .Sort(S => S.Descending(SortSpecialField.Score).Ascending(r => r.item_no))
    .MinScore(dblSkorAvg)
    .Query(fo2)
    .Explain() // <-- explain score computation for each hit
);

还有一个专用的explain api来理解如何计算特定文档的得分。

相关问题