Search code examples
.netelasticsearchnest

Elastic NEST using Term filter on text field with inner keyword field


I'm trying to do a Term filter on a text property accessing its inner keyword field following the new standard on Elastic 5.x...

I have a property like this one:

{
  "foo": {
    "type" "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  }
}

And I'm running the below code to filter using the inner keyword field...

var searchResult = _elasticClient.Search<InvoiceResult>(x => x
 .Index("my_index")
 .Query(query => query
  .Term(term => term
   .Field(new Field("foo.keyword"))
   .Value("TEST")
  )
 )
);

Is there any way to achieve the same result using the model class? When I try the code below it never uses the keyword inner field.

var searchResult = _elasticClient.Search<InvoiceResult>(x => x
 .Index("my_index")
 .Query(query => query
  .Term(term => term
   .Field(field => field.Foo)
   .Value("TEST")
  )
 )
);

Cheers!


Solution

  • NEST has really handy extension method for this case.

    var searchResult = _elasticClient.Search<InvoiceResult>(x => x
        .Index("my_index")
        .Query(query => query
            .Term(term => term
                .Field(field => field.Foo.Suffix("keyword"))
                .Value("TEST")
            )
        )
    );
    

    Hope that helps 🤠.