Uros Uros - 1 year ago 80
ASP.NET (C#) Question

How to get search results that contain given subword with NEST?

We are using NEST in c# .NET project to get search results from Elasticsearch with this query:

var response = await client.SearchAsync<Document>(
s => s.Query(q => q.Match(m => m
.Field(f => f.displayName)
.Query(s))));


Problem is it returns all results that contain separate word s in displayName. We would like to get all results where displayName contains subword s. For example if s="ca" we would like to get document with displayName="my cat". How can I do it in NEST or Sense?

Answer Source

I solved my problem with this query:

// Make transformation "my cat" => "*my* *cat*"
var splited = searchTerms.Split(new char[] { ' ' });
var transformedSearchTerms = splited.Aggregate("", (w1, w2) => w1 + (w1 == "" ? "" : " ") + "*" + w2 + "*");

// Search for documents that contain all given subwords
var response = await client.SearchAsync<Document>(
                s => s.Query(q =>
                        q.QueryString(qs => qs
                            .DefaultField(f => f.displayName)
                            .Query(transformedSearchTerms)
                            .DefaultOperator(Operator.And)
                        )));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download