0
votes

I am able to get all of the values in the database for a text field using an aggregate query in elastic search 6.8:

{
  aggs: {
    values: {
      terms: { field: 'City.keyword', size: 200 },
    },
  },
  size: 0,
};

I am trying to do the same thing for nested field.

Below is an example of a text field (City) and a nested field (Cooling)

"City": "Fredericksburg",
"Cooling": [
    {
        "key": "Fuel",
        "value": "Electric"
    },
    {
        "key": "Central Air",
        "value": "Y"
    },
    {
        "key": "Central A/C",
        "value": "true"
    }
],

And here's the documentation I've been referencing: https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-aggregations-bucket-terms-aggregation.html

Here is the relevant mapping:

{
  "listings:366": {
    "mappings": {
      "_default_": {
        "_all": {
          "enabled": false
        },
        "dynamic_templates": [
          ...
          {
            "Cooling": {
              "path_match": "Cooling.*",
              "mapping": {
                "type": "text"
              }
            }
          },
          ...
        ],
        "properties": {
          ...
          "Cooling": {
            "type": "nested"
          },
          ...
        }
      },
      "listings": {
        "_all": {
          "enabled": false
        },
        "dynamic_templates": [
          ...
          {
            "Cooling": {
              "path_match": "Cooling.*",
              "mapping": {
                "type": "text"
              }
            }
          },
          ...
        ],
        "properties": {
          ...
          "Cooling": {
            "type": "nested",
            "properties": {
              "key": {
                "type": "text"
              },
              "value": {
                "type": "text"
              }
            }
          },
        }
      }
    }
  }
}
2

2 Answers

1
votes

If Cooling is of nested type, then you can achieve what you want by nesting the terms aggregation in a top-level nested aggregation first in order to "dive into" the Cooling nested documents:

{
  "size": 0,
  "aggs": {
    "cooling": {
      "nested": {
        "path": " Cooling"
      },
      "aggs": {
        "values": {
          "terms": {
            "field": "Cooling.key",  <--- make sure this field is mapped as keyword
            "size": 200
          }
        }
      }
    }
  }
}

UPDATE

Your mapping needs to change to this:

    "properties": {
      ...
      "Cooling": {
        "type": "nested",
        "properties": {
          "key": {
            "type": "keyword"       <--- change this
          },
          "value": {
            "type": "text"
          }
        }
      },
    }
0
votes

try to use composite aggregation : see composite Aggregation