ElasticSearch - how to 'collapse' documents within an aggregation

2.4k Views Asked by At

struggling with this, so any help would be much appreciated!

I have an aggregation that provides counts of documents grouped by role, gender and age within date histogram buckets:

"aggs": {
"period": {
  "date_histogram": {
    "field": "timestamp",
    "fixed_interval": "15m",
    "time_zone": "America/Los_Angeles",
    "order": {
      "_key": "desc"
    }
  },
  "aggs": {
    "role": {
      "terms": {
        "field": "role",
        "size": 3
      },
      "aggs": {
        "gender": {
          "terms": {
            "field": "gender",
            "size": 3
          },
          "aggs": {
            "age": {
              "terms": {
                "field": "age",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

}

Each document has a visitorId, there may be many documents within the same date histogram bucket with the same visitorId.

I would like only unique visitorId's to be included in each date histogram bucket. Effectively I want to avoid double / triple etc counting where it's the same visitor. Is that possible?

1

There are 1 best solutions below

6
On BEST ANSWER

Each document has a visitorId, there may be many documents within the same date histogram bucket with the same visitorId.

If only for each visitor role, gender & age are same then below query (adding a cardinality sub-aggregation on visitorId) should work:

"aggs": {
"period": {
  "date_histogram": {
    "field": "timestamp",
    "fixed_interval": "15m",
    "time_zone": "America/Los_Angeles",
    "order": {
      "_key": "desc"
    }
  },
  "aggs": {
    "role": {
      "terms": {
        "field": "role",
        "size": 3
      },
      "aggs": {
        "gender": {
          "terms": {
            "field": "gender",
            "size": 3
          },
          "aggs": {
            "age": {
              "terms": {
                "field": "age",
                "size": 10
              },"aggs": {
                 "visitors": {
                   "cardinality": {
                    "field": "visitorId"
                    }
                   }
                }
            }
          }
        }
      }
    }
  }
}
}