Query with multimatch and wildcard

I'm trying to execute a query on multi fields but also using a wildcard in the MobileNumber, basically, if a mobile number is for example 3530831112233 if I search by 831122 I want to return this record. This is what I have done so far.

var response = await this.client.SearchAsync<ElasticCustomer>(searchDescriptor => searchDescriptor
          .AllTypes()
            .Query(q => q
                     .MultiMatch(m => m
                            .Fields(f => f
                                .Field(u => u.CustomerName)
                                .Field(u => u.MobileNumber))
                          .Query(query)))
          .Size(pageSize)
          .Sort(q => q.Descending(u => u.CustomerLastUpdated)));

multimatch query requires match queries as its internal queries. And a match query doesn't support wildcards. Depending how you want to combine scores from multiple fields, you may use dis_max query, that can accept wildcard queries as well.

{
    "query": {
       "dis_max" : {
            "queries" : [
            	{"wildcard" : { "customername" : {"value" : "*831112*"}}},
                {"wildcard" : { "mobilenumber" : {"value" : "*831112*"}}}
            ],
            "tie_breaker" : 0.7
        } 
    }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.