How to strongly type specify fields using NEST?

176 views Asked by At

Following Elasticsearch's documentation here, we should specify our shingle subfields like in this example:

GET my-index-000001/_search
{
  "query": {
    "multi_match": {
      "query": "brown f",
      "type": "bool_prefix",
      "fields": [
        "my_field",
        "my_field._2gram",
        "my_field._3gram"
      ]
    }
  }
}

How can we strongly type specify these fields using NEST, Elasticsearch's official client?

ISearchRequest Selector(SearchDescriptor<PointOfInterest> s)
{
    return s
        .Index(IndexName)
        .Query(q => q
            .MultiMatch(c => c
                .Fields(f => f
                    .Field(p => p.MyField) // This one we can easily specify
                    .Field("my_field._2gram") // This one needs to be hard coded? Field(p => $"{p.MyField}._2gram")) doesn't work 
                    .Field("my_field._3gram"))
              .Type(TextQueryType.BoolPrefix)
              .Query(query.Query)));
}
1

There are 1 answers

0
Rob On

Suffix extension method will help you with this

ISearchRequest Selector(SearchDescriptor<PointOfInterest> s)
{
    return s
        .Index(IndexName)
        .Query(q => q
            .MultiMatch(c => c
                .Fields(f => f
                    .Field(p => p.MyField)
                    .Field(p => p.MyField.Suffix("_2gram"))
                    .Field(p => p.MyField.Suffix("_3gram")))
              .Type(TextQueryType.BoolPrefix)
              .Query(query.Query)));
}