Elasticsearch filter by score

随声附和 提交于 2020-12-15 05:04:46

问题


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 search_item_no { get; set; }   
    public string search_group_one { get; set; }
    public string search_group_two { get; set; }
    public string search_description { get; set; }
    public string search_all_fields { get; set; } /* search_item_no + search_group_one + search_group_two + search_description and something else */
}

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

item_no # category # campaign # in_stock # search_item_no # search_group_one # search_group_two # search_description # search_all_fields
p-C-001 # cat1     # 1        # 1        # pc001          # aaaa@bbb@ccc     # kkkkk@llllllll   # red@metal@light    # pc001@aaaa@bbb@ccc@kkkkk@llllllll@red@metal@light
p-F-002 # cat1     # 1        # 1        # pf002          # ck001@www@zza    # yyy@rrrr@mmplp   # bold@plastic@ss    # pf002@ck001@www@zza@yyy@rrrr@mmplp@bold@plastic@ss
p-SW102 # cat2     # 0        # 1        # psw102         # psw102@777@ooo   # yyy@rrrr@mmplp   # bold@plastic@cc    # psw102@777@ooo@www@zza@yyy@rrrr@mmplp@bold@plastic@cc

I could not find similar issue. Either I'm doing someting wrong or I do not know how to search.

My index simply like this.

For example user searchs "c 001", I look at search_all_fields and find it in two documents. (WildCardQuery for all searching keywords "c" and "001") But the second one is useless. Because the first one matches with item_no. This is more valuable. The second one matches with group_one field. This is less valuable. I must filter it.

But on the other hand, someone searchs "yyy 102", then it only takes place in group_one and group two field. So I must not filter it.

Going twice elastic side for scoring is expensive. But How can I achive this?

QueryContainer queryContainsOr = new WildcardQuery() { Field = "score_all_fields", Value = "*yyy*" };
queryContainsOr |= new WildcardQuery() { Field = "score_all_fields", Value = "*102*" };
QueryContainer queryEqualsOr =  new TermQuery() { Field = "category", Value = "cat1" };
queryEqualsOr |=  new TermQuery() { Field = "category", Value = "cat2" };
QueryContainer queryEqualsAnd = new TermQuery() { Field = "campaign", Value = 1 };
queryEqualsAnd &= new TermQuery() { Field = "in_stock", Value = 1 };
        
        
QueryContainer mainQuery = queryContainsOr & 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))
                )
    ));
}

来源:https://stackoverflow.com/questions/63670605/elasticsearch-filter-by-score

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