0

I am trying to do an aggregation of the {"wildcard": {"data.addresses.ces.cp": "maria*"}, {"match": { "data.addresses.ces.direction": "rodriguez"}} fields, but it does not return the results of the query.

{ 
  "_source": "created_at",
  "size": 1, 
  "sort": [{
    "created_at.keyword": {
      "order": "desc"
     }
   }], 
   "query": {
     "nested": {
       "path": "data.addresses",
       "inner_hits": {
       },
       "query": {
         "nested": {
           "path": "data.addresses.ces",
           "query": 
             {"wildcard": {"data.addresses.ces.cp": "maria*"},
             {"match": { "data.addresses.ces.direction": "rodriguez"}}
         }
       }
     }
   }
 }

How can I perform an aggregation that returns the values ​​of the query, and not all the values ​​of the JSON? In case the aggregations don't support inner_hits, how could I get wildcard and match in aggs?

0

1 Answer 1

3

You need to repeat the filter conditions in the aggregation part so that the aggregation only runs on the selected nested documents:

{
  "_source": "created_at",
  "size": 1,
  "sort": [
    {
      "created_at.keyword": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "nested": {
      "path": "data.addresses",
      "inner_hits": {},
      "query": {
        "nested": {
          "path": "data.addresses.ces",
          "query": {
            "bool": {
              "filter": [
                {
                  "wildcard": {
                    "data.addresses.ces.cp": "maria*"
                  }
                },
                {
                  "match": {
                    "data.addresses.ces.direction": "rodriguez"
                  }
                }
              ]
            }
          }
        }
      }
    }
  },
  "aggs": {
    "addresses": {
      "nested": {
        "path": "data.addresses"
      },
      "aggs": {
        "ces": {
          "nested": {
            "path": "data.addresses.ces"
          },
          "aggs": {
            "query": {
              "filter": {
                "bool": {
                  "filter": [
                    {
                      "wildcard": {
                        "data.addresses.ces.cp": "maria*"
                      }
                    },
                    {
                      "match": {
                        "data.addresses.ces.direction": "rodriguez"
                      }
                    }
                  ]
                }
              },
              "aggs": {
                "cp": {
                  "terms": {
                    "field": "data.addresses.ces.cp"
                  }
                },
                "direction": {
                  "terms": {
                    "field": "data.addresses.ces.direction"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Val, I am improving my level of elasticsearch a lot of, and much of it is for your answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.