Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 727
Felipe Nipo Ferreira Avatar asked Nov 09 '17 11:11

Felipe Nipo Ferreira


1 Answers

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 🤠.

like image 185
Rob Avatar answered Oct 22 '22 08:10

Rob