Boosting以Elastic / NEST开头并包含搜索

时间:2015-08-19 01:35:31

标签: c# nest elasticsearch

我正在进行下面的通配符搜索,我想让(她的*)结果的开头比包含(她的)搜索结果的排名更多。有没有办法实现这个目标?

elasticClient.Search<ClientSearchContract>(p => 
                p.Query(q => q
                    .Bool(bq => bq
                        .Should(mq => mq.QueryString(qs =>
                    qs.Query(her* *her*))
                        )
                    )
                )
                .Sort(x => x.OnField("_score").Descending())
            );

2 个答案:

答案 0 :(得分:0)

为此,我们使用custom score脚本。在JSON中,它看起来像这样:

"query": 
{
    "custom_score": 
    { 
        "query": *query* ,
        "params" : 
        { 
            "streetMultiply": " + ((!searchQuery.HasCityValue()) ? 100 : 0) + @",
            "addressMultiply": " + ((!searchQuery.HasStreetValue()) ? 100 : 0) + @"
        }
        ,"script": "if (doc['streetId'].value == 0) { _score * streetMultiply } else { if (doc['addressId'].value == 0) { _score * addressMultiply } else { _score } }"
    } 
}, "size": 100 }"

我们没有使用NEST,但我在NEST找到了pull-requst来实现这个

答案 1 :(得分:0)

这可以帮到你:

            var result = elasticClient.Search<ClientSearchContract>(q => q
            .Index("index")
            .Type("type")
            .Query(qq =>
            {
                QueryContainer startsQuery = null;
                QueryContainer containQuery = null;
                {
                    startsQuery |= qq.QueryString(qs => qs.OnFields(f => f.Field).Query("her*").Boost(2));
                    containQuery |= qq.QueryString(qs => qs.OnFields(f => f.Field).Query("*her*").Boost(1.2));
                }

                return startsQuery || containQuery;
            })
            .Take(10)
            .Sort(x => x.OnField("_score").Descending())
            );