Search options in Elasticsearch percolate function

194 Views Asked by At

My question is how can I use search options like multimatch, slop and fuzziness in a percolate function using NEST (c#)?

I want to implement a percolate function that returns exactly the opposite result of the following search function:

public List<string> search(string query){
.......
.......
var searchResponse = client.Search<Document>(s => s 
                .AllTypes()
                   .From(0)
                   .Take(10)
                .Query(q => q               // define query
                    .MultiMatch(mp => mp            // of type MultiMatch
                    .Query(input.Trim())
                    .Fields(f => f          // define fields to search against
                    .Fields(f3 => f3.doc_text))
                    .Slop(2)
                    .Operator(Operator.And)
                    .Fuzziness(Fuzziness.Auto))));
}

The following is the percolate function I currently use but don't know how to include multimatch, slop and fuzziness options. I could not find detail about this in its documentation.

var searchResponseDoc = client.Search<PercolatedQuery>(s => s
               .Query(q => q
               .Percolate(f => f
               .Field(p => p.Query)
               .DocumentType<Document>() //I have a class called Document
               .Document(myDocument))) // myDocument is an object of type Document

Thanks.

1

There are 1 best solutions below

1
On

In the first you are doing a multi_match query which has the options you require. In the latter you do a percolator query.

If you want to do both you can use binary and bitwise operators e.g

.Query(q => 
    !q.MultiMatch(mp => mp
        .Query(input.Trim())
        .Fields(f => f.Fields(f3 => f3.doc_text))
        .Slop(2)
        .Operator(Operator.And)
        .Fuzziness(Fuzziness.Auto)
    )
    && q.Percolate(f => f
           .Field(p => p.Query)
           .DocumentType<Document>()
           .Document(myDocument)
    )
)

Not the use of && to create a bool query with the two queries as must clauses. The unary ! operator negates the query by wrapping the query in a bool and placing the query in a must_not clause.

See more info https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/writing-queries.html

and

https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/bool-queries.html