1
votes

I'm trying to run the following Filter Aggregation query against ElasticSearch via Nest 2.3.2.

GET workitems_v2/mail/_search
{
  size:0,
  "aggs" : {
    "AutoComplete" : {
      "filter" : { "match": { "claimData.claimOwner":"dav" } },
      "aggs": {
        "Suggestions": {
          "terms": {"field":"claimData.claimOwner.raw"}
        }
      }
    }
  }
}

Here's what I have in Nest (VB.Net) - note how the second Aggregations() function is a child of the Filter() function.

Dim queryResults = elasticClient.Search(Of Mail)(Function(s) s.
    Size(0).
    Aggregations(Function(a) a.
        Filter("AutoComplete", Function(f) f.
            Filter(Function(ff) ff.
                Match(Function(m) m.
                    Field("claimData.claimOwner").
                    Query("dav")
                )
            ).
            Aggregations(Function(aa) a.
                Terms("Suggestions", Function(t) t.
                    Field("claimData.claimOwner.raw")
                )
            )
        )
    )
)

But the query that Nest generates looks like:

POST /workitems_v2/mail/_search
{
    "size" : 0,
    "aggs" : {
        "Suggestions" : {
            "terms" : {
                "field" : "claimData.claimOwner.raw"
            }
        },
        "AutoComplete" : {
            "filter" : {
                "match" : {
                    "claimData.claimOwner" : {
                        "query" : "dav"
                    }
                }
            }
        }
    }
}

... which doesn't give me what I want. How do I tell Nest that the "Suggestions" aggregation is part of the first Filter Aggregation?

1

1 Answers

2
votes

Your query is not quite correct; the sub aggregation should be using the AggregationContainerDescriptor<T> passed as the argument for aa in the anonymous function

Dim queryResults = elasticClient.Search(Of Mail)(Function(s) s.
Size(0).
Aggregations(Function(a) a.
    Filter("AutoComplete", Function(f) f.
        Filter(Function(ff) ff.
            Match(Function(m) m.
                Field("claimData.claimOwner").
                Query("dav")
            )
        ).
        Aggregations(Function(aa) aa.
            Terms("Suggestions", Function(t) t.
                Field("claimData.claimOwner.raw")
            )
        )
    )
)

which results in

{
  "size": 0,
  "aggs": {
    "AutoComplete": {
      "filter": {
        "match": {
          "claimData.claimOwner": {
            "query": "dav"
          }
        }
      },
      "aggs": {
        "Suggestions": {
          "terms": {
            "field": "claimData.claimOwner.raw"
          }
        }
      }
    }
  }
}