ELASTICSERCH - Inner_hits-Aggregationen

Aug 26 2020

Ich versuche eine Aggregation der {"wildcare": {"data.addresses.ces.cp": "maria *"}, {"macth": {"data.addresses.ces.direction": "rodriguez" durchzuführen. }} Felder, gibt jedoch nicht die Ergebnisse der Abfrage zurück.

{ "_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": 
                  {"wildcare": {"data.addresses.ces.cp": "maria*"},
                  {"macth": { "data.addresses.ces.direction": "rodriguez"}}
              }
            }
          }
        }
      }

Wie kann ich eine Aggregation durchführen, die die Werte der Abfrage und nicht alle Werte des JSON zurückgibt? Falls die Aggregationen inner_hits nicht unterstützen, wie könnte ich Wildcare und Macth in Aggs bekommen?

Antworten

1 Val Aug 26 2020 at 15:02

Sie müssen die Filterbedingungen im Aggregationsteil wiederholen, damit die Aggregation nur für die ausgewählten verschachtelten Dokumente ausgeführt wird:

{
  "_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"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}