Named Query Confusion

I understand that named queries add the matched_queries property to the response, but is there any way to access documents that match the named queries in aggregations?

The goal is to be able to use the "%dashboard_context-must_clause%" for dynamic time ranges, but I haven't been able to find a way to do bool queries inside of aggregations.

"query": {
    "bool": {
      "must": [
        {"match": {"year-month": {"query": "2021-01", "_name": "A"}}},
        {"match": {"year-month": {"query": "2021-03", "_name": "B"}}}
      ]
    }
  },
"aggs": {
  "calcs": {
     "terms": {
        "script": "'do stuff'"
      },
      "aggs": {
        "A_calc":{ 
          //I want to only use the results from query A here
          "filter": { "term": { "meat":"steak" }  },
          "aggs" : {
            "steakcount": {
              "value_count": {
                "field": "meat"
              }
            }
          }
        },
        "B_calc":{ 
          //I want to only use the results from query B here
          "filter": { "term": { "meat":"chicken"} },
          "aggs" : {
            "chickencount": {
              "value_count": {
                "field": "meat"
              }
            }
          }
        },




Hey,

you need to repeat the query within each aggregation filter, so you would have a bool query that contains meat:streak and year-month: 2021-01 in its filter clause.

Hope that helps!

--Alex

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.