4
votes

My ElasticSearch 2.x NEST query string search contains a wildcard:

Using NEST in C#:

var results = _client.Search<IEntity>(s => s
    .Index(Indices.AllIndices)
    .AllTypes()
    .Query(qs => qs
        .QueryString(qsq => qsq.Query("Micro*")))
    .From(pageNumber)
    .Size(pageSize));

Comes up with something like this:

$ curl -XGET 'http://localhost:9200/_all/_search?q=Micro*'

This code was derived from the ElasticSearch page on using Co-variants. The results are co-variant; they are of mixed type coming from multiple indices. The problem I am having is that all of the hits come back with a score of 1.

This is regardless of type or boosting. Can I boost by type or, alternatively, is there a way to reveal or "explain" the search result so I can order by score?

2

2 Answers

4
votes

Multi term queries like wildcard query are given a constant score equal to the boosting by default. You can change this behaviour using .Rewrite().

var results = client.Search<IEntity>(s => s
    .Index(Indices.AllIndices)
    .AllTypes()
    .Query(qs => qs
        .QueryString(qsq => qsq
            .Query("Micro*")
            .Rewrite(RewriteMultiTerm.ScoringBoolean)
        )
    )
    .From(pageNumber)
    .Size(pageSize)
);

With RewriteMultiTerm.ScoringBoolean, the rewrite method first translates each term into a should clause in a bool query and keeps the scores as computed by the query.

Note that this can be CPU intensive and there is a default limit of 1024 bool query clauses that can be easily hit for a large document corpus; running your query on the complete StackOverflow data set (questions, answers and users) for example, hits the clause limit for questions. You may want to analyze some text with an analyzer that uses an edgengram token filter.

0
votes

Wildcard searches will always return a score of 1. You can boost by a particular type. See this: How to boost index type in elasticsearch?