Elasticsearch - Aggregation on multiple fields in the same nested scope

Redtopia

I'm aggregating product search results by tags, which have a name and ID fields. How do I get both fields back in an aggregation bucket? I can get one or the other, but I can't figure out how to get both. BTW, script access is turned off on my cluster, so I can't use that.

Here's my product mapping (simplified for this question):

"mappings": {
    "products": {
        "properties": { 
            "title": {
                "type": "string"
            },
            "description": {
                "type": "string"
            },
            "topics": {
                "properties": {
                    "id": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "name": {
                        "type" : "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
}

Here's my query:

"query": {
    "multi_match": {
        "query": "Testing 1 2 3",
        "fields": ["title", "description"]
    },
    "aggs": {
        "Topics": {
            "terms": {
                "field": "topics.id",
                "size": 15
            }
        }
    }
}

My aggregation buckets look like this:

enter image description here

...the "key" value in the first bucket is the topics.id field value. Is there a way to add my topics.name field to the bucket?

Rahul

If you want to add another field as a key in your bucket, then (id,name) will act as a unique bucket. You need an association between an id and a name. Without nested mapping, the list of ids and names are separate arrays. Hence, you need to map it as nested.

topics": { "type":"nested", "properties": { "id": { "type": "string", "index": "not_analyzed" }, "name": { "type" : "string", "index": "not_analyzed" } } }

For aggregation on multiple fields, you need to use sub-aggregations.

Here is a sample aggregation query:

 {
      "aggs": {
        "topics_agg": {
          "nested": {
            "path": "topics"
          },
          "aggs": {
            "name": {
              "terms": {
                "field": "topics.id"
              },
              "aggs": {
                "name": {
                  "terms": {
                    "field": "topics.name"
                  }
                }
              }
            }
          }
        }
      }
    }

Aggregation Sample Result :

    "aggregations": {
          "topics_agg": {
             "doc_count": 5,
             "name": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": [
                   {
                      "id": 123,
                      "doc_count": 6,
                      "name": {
                         "doc_count_error_upper_bound": 0,
                         "sum_other_doc_count": 0,
                         "buckets": [
                            {
                               "key": "topic1",
                               "doc_count": 3
                            },
                            {
                               "key": "topic2",
                               "doc_count": 3
                            }
                         ]
                      }
                   },
                   {
                      "key": 456,
                      "doc_count": 2,
                      "name": {
                         "doc_count_error_upper_bound": 0,
                         "sum_other_doc_count": 0,
                         "buckets": [
                            {
                               "key": "topic1",
                               "doc_count": 2
                            }
                         ]
                      }
                   },
..............

Note : For id : 123, there are multiple names buckets. As there are multiple name values for the same id. To create separate unique buckets, just create all the parent-child combinations.

For eg. 123-topic1, 123-topic2, 456-topic1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Same aggregation on multiple metrics Elasticsearch

From Dev

Elasticsearch group by multiple fields and sum the hours (aggregation)

From Dev

nested in nested aggregation in elasticsearch

From Dev

how to bucket empty and non empty fields in nested aggregation in elasticsearch?

From Dev

Elasticsearch aggregation doesn't work with nested-type fields

From Dev

Elasticsearch nested aggregation with range aggregation

From Dev

Elasticsearch aggregation by 7 fields

From Dev

Elasticsearch how count values from multiple document fields using aggregation

From Dev

Elasticsearch how count values from multiple document fields using aggregation

From Dev

Nested Aggregation Elasticsearch

From Dev

Elasticsearch nested aggregation with JAVA

From Dev

Aggregation - Unwind multiple fields

From Dev

Elasticsearch - Terms Aggregation nested field

From Dev

Elasticsearch aggregation on nested (not array) field

From Java

Elasticsearch filter by nested fields

From Dev

Elasticsearch grouping by nested fields

From Dev

Elasticsearch - get nested fields

From Dev

ElasticSearch count nested fields

From Dev

Elasticsearch - no results with nested fields

From Dev

Scope 0 count terms in aggregation in ElasticSearch

From Dev

Elasticsearch Aggregation Query with multiple excludes

From Dev

Search a nested field for multiple values on the same field with elasticsearch

From Dev

ElasticSearch - issue with sub term aggregation with array fields

From Dev

aggregation query and return all fields in elasticsearch

From Dev

Elasticsearch - get terms aggregation for specified fields

From Dev

ElasticSearch group by multiple fields

From Dev

Elasticsearch match multiple fields

From Dev

Search on multiple fields with Elasticsearch

From Dev

Elasticsearch multiple fields autosuggestion

Related Related

  1. 1

    Same aggregation on multiple metrics Elasticsearch

  2. 2

    Elasticsearch group by multiple fields and sum the hours (aggregation)

  3. 3

    nested in nested aggregation in elasticsearch

  4. 4

    how to bucket empty and non empty fields in nested aggregation in elasticsearch?

  5. 5

    Elasticsearch aggregation doesn't work with nested-type fields

  6. 6

    Elasticsearch nested aggregation with range aggregation

  7. 7

    Elasticsearch aggregation by 7 fields

  8. 8

    Elasticsearch how count values from multiple document fields using aggregation

  9. 9

    Elasticsearch how count values from multiple document fields using aggregation

  10. 10

    Nested Aggregation Elasticsearch

  11. 11

    Elasticsearch nested aggregation with JAVA

  12. 12

    Aggregation - Unwind multiple fields

  13. 13

    Elasticsearch - Terms Aggregation nested field

  14. 14

    Elasticsearch aggregation on nested (not array) field

  15. 15

    Elasticsearch filter by nested fields

  16. 16

    Elasticsearch grouping by nested fields

  17. 17

    Elasticsearch - get nested fields

  18. 18

    ElasticSearch count nested fields

  19. 19

    Elasticsearch - no results with nested fields

  20. 20

    Scope 0 count terms in aggregation in ElasticSearch

  21. 21

    Elasticsearch Aggregation Query with multiple excludes

  22. 22

    Search a nested field for multiple values on the same field with elasticsearch

  23. 23

    ElasticSearch - issue with sub term aggregation with array fields

  24. 24

    aggregation query and return all fields in elasticsearch

  25. 25

    Elasticsearch - get terms aggregation for specified fields

  26. 26

    ElasticSearch group by multiple fields

  27. 27

    Elasticsearch match multiple fields

  28. 28

    Search on multiple fields with Elasticsearch

  29. 29

    Elasticsearch multiple fields autosuggestion

HotTag

Archive