弹性搜索嵌套聚合,后跟术语聚合,然后是嵌套聚合

时间:2018-04-16 16:07:18

标签: elasticsearch elasticsearch-aggregation elasticsearch-nested

我有一个结构没有的索引。用户登录系统的时间如下:

[{
        "users_id": 5,
        "uname": "abcdef",
        "status": "active",
        "groups_id": 2,
        "user_login": [{
                "user_logins_id": 12,
                "users_id": 5,
                "success": "t",
                "type": "login",
                "date": "2017/01/02",
                "ip_address": "198.27.146.70"
            },
            {
                "user_logins_id": 13,
                "users_id": 5,
                "success": "t",
                "type": "logout",
                "date": "2017/01/02",
                "ip_address": "198.27.146.70"
            },
{
                "user_logins_id": 12,
                "users_id": 5,
                "success": "t",
                "type": "login",
                "date": "2017/01/03",
                "ip_address": "198.27.146.70"
            },
            {
                "user_logins_id": 13,
                "users_id": 5,
                "success": "t",
                "type": "logout",
                "date": "2017/01/03",
                "ip_address": "198.27.146.70"
            }
        ],
        "role": "Student"
    },
    {
        "users_id": 2,
        "uname": "xyz",
        "status": "active",
        "groups_id": 1,
        "user_login": [{
                "user_logins_id": 16,
                "users_id": 2,
                "success": "t",
                "type": "login",
                "date": "2017/01/05",
                "ip_address": "198.27.146.70"
            },
            {
                "user_logins_id": 17,
                "users_id": 5,
                "success": "t",
                "type": "logout",
                "date": "2017/01/06",
                "ip_address": "198.27.146.70"
            }
        ],
        "role": "Professor"
    }
]

问题:需要知道特定角色的用户在给定日期范围内登录了多少次(明天结果)。 解: 我在user_login.date字段(嵌套文档)上应用了日期直方图,在根级别的角色字段上使用术语聚合(使用反向嵌套聚合),我还编写了一个嵌套聚合。日期直方图返回子桶超过指定的日期范围。

以下是我试过的查询:

 {
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "status.keyword": {
              "value": "active"
            }
          }
        },
        {
          "nested": {
            "path": "user_login",
            "query": {
              "bool": {
                "must": [
                  {
                    "range": {
                      "user_login.date": {
                        "from": "2017/01/02",
                        "to": "2017/01/02",
                        "include_lower": true,
                        "include_upper": true,
                        "format": "yyyy/MM/dd",
                        "boost": 1
                      }
                    }
                  },
                  {
                    "match": {
                      "user_login.type": "login"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "histo": {
      "nested": {
        "path": "user_login"
      },
      "aggs": {
        "histogrammm": {
          "date_histogram": {
            "field": "user_login.date",
            "interval": "day"
          },
          "aggs": {
            "reverzzzwayyy": {
              "reverse_nested": {},
              "aggs": {
                "roles": {
                  "terms": {
                    "field": "role.raw",
                    "size": 10
                  },
                  "aggs": {
                    "logins1": {
                      "nested": {
                        "path": "user_login"
                      },
                      "aggs": {
                        "logins2": {
                          "filter": {
                            "bool": {
                              "must": [
                                {
                                  "range": {
                                    "user_login.date": {
                                      "from": "2017/01/02",
                                      "to": "2017/01/02",
                                      "include_lower": true,
                                      "include_upper": true,
                                      "format": "yyyy/MM/dd",
                                      "boost": 1
                                    }
                                  }
                                },
                                {
                                  "term": {
                                    "user_login.type": {
                                      "value": "login",
                                      "boost": 1
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

上述查询返回日期直方图子存储桶,即使对于" 2017/01/03"这是错的。 解决这个问题的任何解决方案?

0 个答案:

没有答案