Search code examples
elasticsearch.net-corenest

ElasticSearch NEST simple Terms query requires .keyword


I am trying to retrieve a single document with a secific name (exactly that name) using NEST 7.5.1 (.NET Core 3.1):

var queryByTerm = client.Search<SomeDto>(s =>s.Query(q => q.Term(p => p.NameField, "example name")));

But it does not return any documents (the call succeeds).

The actual query being sent (as seen in DebugInformation with .EnableDebugMode on client's ConnectionSettings):

{"query":{"term":{"nameField":{"value":"example name"}}}}

But it only works (in Kibana) when I add .keyword fo the nameField:

{"query":{"term":{"nameField.keyword":{"value":"example name"}}}}

Do I somehow have to force NEST to use nameField.keyword instead of nameField?


Solution

  • You can do this with .Suffix() extension method. Docs.

    var queryByTerm = client.Search<SomeDto>(s =>s.Query(q => q.Term(p => p.NameField.Suffix("keyword"), "example name")));
    

    Hope that helps.