ElasticSearch group by multiple fields

Pavel

The only close thing that I've found was: Multiple group-by in Elasticsearch

Basically I'm trying to get the ES equivalent of the following MySql query:

select gender, age_range, count(distinct profile_id) as count 
FROM TABLE group by age_range, gender

The age and gender by themselves were easy to get:

{
  "query": {
    "match_all": {}
  },
  "facets": {
    "ages": {
      "terms": {
        "field": "age_range",
        "size": 20
      }
    },
    "gender_by_age": {
      "terms": {
        "fields": [
          "age_range",
          "gender"
        ]
      }
    }
  },
  "size": 0
}

which gives:

{
  "ages": {
    "_type": "terms",
    "missing": 0,
    "total": 193961,
    "other": 0,
    "terms": [
      {
        "term": 0,
        "count": 162643
      },
      {
        "term": 3,
        "count": 10683
      },
      {
        "term": 4,
        "count": 8931
      },
      {
        "term": 5,
        "count": 4690
      },
      {
        "term": 6,
        "count": 3647
      },
      {
        "term": 2,
        "count": 3247
      },
      {
        "term": 1,
        "count": 120
      }
    ]
  },
  "total_gender": {
    "_type": "terms",
    "missing": 0,
    "total": 193961,
    "other": 0,
    "terms": [
      {
        "term": 1,
        "count": 94799
      },
      {
        "term": 2,
        "count": 62645
      },
      {
        "term": 0,
        "count": 36517
      }
    ]
  }
}

But now I need something that looks like this:

[breakdown_gender] => Array
    (
        [1] => Array
            (
                [0] => 264
                [1] => 1
                [2] => 6
                [3] => 67
                [4] => 72
                [5] => 40
                [6] => 23
            )

        [2] => Array
            (
                [0] => 153
                [2] => 2
                [3] => 21
                [4] => 35
                [5] => 22
                [6] => 11
            )

    )

Please note that 0,1,2,3,4,5,6 are "mappings" for the age ranges so they actually mean something :) and not just numbers. e.g. Gender[1] (which is "male") breaks down into age range [0] (which is "under 18") with a count of 246.

moliware

As you only have 2 fields a simple way is doing two queries with single facets. For Male:

{
    "query" : {
      "term" : { "gender" : "Male" }
    },
    "facets" : {
        "age_range" : {
            "terms" : {
                "field" : "age_range"
            }
        }
    }
}

And for female:

{
    "query" : {
      "term" : { "gender" : "Female" }
    },
    "facets" : {
        "age_range" : {
            "terms" : {
                "field" : "age_range"
            }
        }
    }
}

Or you can do it in a single query with a facet filter (see this link for further information)

{
    "query" : {
       "match_all": {}
    },
    "facets" : {
        "age_range_male" : {
            "terms" : {
                "field" : "age_range"
            },
            "facet_filter":{
                "term": {
                    "gender": "Male"
                }
            }
        },
        "age_range_female" : {
            "terms" : {
                "field" : "age_range"
            },
            "facet_filter":{
                "term": {
                    "gender": "Female"
                }
            }
        }
    }
}

Update:

As facets are about to be removed. This is the solution with aggregations:

{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "male": {
      "filter": {
        "term": {
          "gender": "Male"
        }
      },
      "aggs": {
        "age_range": {
          "terms": {
            "field": "age_range"
          }
        }
      }
    },
    "female": {
      "filter": {
        "term": {
          "gender": "Female"
        }
      },
      "aggs": {
        "age_range": {
          "terms": {
            "field": "age_range"
          }
        }
      }
    }
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Elasticsearch match multiple fields

From Dev

Search on multiple fields with Elasticsearch

From Dev

Elasticsearch multiple fields autosuggestion

From Dev

Elasticsearch match multiple fields

From Dev

Elasticsearch multiple fields OR query

From Dev

group plots by multiple fields

From Dev

Group by for multiple fields in MYSQL

From Dev

SQL: Group by multiple fields

From Dev

multiple fields in a group by statement

From Dev

Group an array by multiple fields

From Dev

ElasticSearch sort order for multiple fields

From Dev

Search on multiple fields with ElasticSearch with or, and operators

From Dev

ElasticSearch and searching on multiple fields in PHP

From Dev

ElasticSearch filtering on multiple fields (with aggregration)

From Dev

ElasticSearch and searching on multiple fields in PHP

From Dev

Elasticsearch sort based on multiple fields

From Dev

Group with multiple fields, but treat the fields as equal

From Java

mongodb group values by multiple fields

From Dev

Group objects in list by multiple fields

From Dev

Order By clause on multiple fields with group

From Java

How to search on multiple fields of array in elasticsearch

From Dev

Elasticsearch phrase prefix query on multiple fields

From Dev

Elasticsearch: can't filter on multiple fields

From Dev

ElasticSearch: Finding documents with multiple identical fields

From Dev

Java API to match multiple fields in elasticsearch

From Dev

ElasticSearch match multiple fields with different values

From Dev

ElasticSearch NEST Search Multiple Types & All Fields

From Dev

Elasticsearch - Aggregation on multiple fields in the same nested scope

Related Related

HotTag

Archive