使用 Elastic/NEST 开始并包含搜索

Boosting Starts with and contains search using Elastic/NEST

我正在执行如下通配符搜索,我希望 starts with(her*) 结果的排名高于 contains(her) 搜索结果。有办法实现吗?

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())
            );

为此,我们使用 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 来实现它

这里有一些可以帮助你的东西:

            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())
            );