Search code examples
c#elasticsearchnest

C# Nest Elastic Search Wildcard Query - No Results Returned


I'm trying to set up a query in C# using a wildcard. I can get the correct data returned using MultiMatch but have not had success with wildcard. Here is what I have so far:

public async Task<IEnumerable<SearchResultModel>> Search(string query)
     var queryResults = await elasticApp.SearchAsync<dynamic>(s => s
        .From(0)
        .Take(10)
        .Index(activeIndices)
        .Type(dataTypes)
        .Query(q => q
            .Bool(b => b
                .Should(m => m
                    .Wildcard(w => w
                        .Field("name")
                            .Value(w + "*")                            
                     )
                 )
             )
         )

So as for the function parameter query when using MultiMatch, you would do .Query(query) at the end, but when I try to put that in using Wildcard I get syntax errors. And according to the NEST ES docs it seems that you do not use .Query at the end.

Currently an empty array is returned.

Any insight on this would be appreciated as I am new to using Nest and C#.


Solution

  • I was able to resolve my issue by doing the following:

    .Query(q => q                    
      .Bool(b => b                     
        .Should(m => m
          .Wildcard(c => c
            .Field("name").Value(query.ToLower() + "*")
          )
        )
      )
    )
    

    query is passed in as a function argument and I want the asterisk to take everything after query.