简体   繁体   中英

Boosting Starts with and contains search using Elastic/NEST

I am doing a wildcard search as below and I want to have the starts with(her*) results to have more ranking than the contains( her ) search results. Is there a way to achieve this?

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

Here is something that could help you :

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

We use custom score scripts for this purpose. In JSON it looks like this:

"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 }"

We don't use NEST for this, but I've found a pull-requst for implementation this in NEST

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM