ElasticSearch父子查询结果与预期不符

时间:2018-11-16 11:27:11

标签: elasticsearch

我创建索引父级和子级,父级文档存储用户基本信息,而子级文档存储登录信息。

在2018年1月1日至2018年1月10日期间,我如何查询18岁以上且已登录5次以上的用户信息?

PUT merit_parent/merit_mapping/1
{
  "name": "test1",
  "sex": "female",
  "age": 18
}
PUT merit_parent/merit_mapping/2
{
  "name": "test2",
  "sex": "male",
  "age": 20
}

PUT merit_parent/child_mapping/1?parent=1
{
  "str_id": "1",
  "str_pid": "1",
  "double_date": 20180101,
  "double_loginNum": 2
}
PUT merit_parent/child_mapping/2?parent=1
{
  "str_id": "2",
  "str_pid": "1",
  "double_date": 20180102,
  "double_loginNum": 2
}
PUT merit_parent/child_mapping/3?parent=2
{
  "str_id": "3",
  "str_pid": "2",
  "double_date": 20180101,
  "double_loginNum": 10
}

我的查询

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "sex": "male"
          }
        },
        {
          "has_child": {
            "type": "child_mapping",
            "query": {
              "bool": {
                "must": [
                  {
                    "range": {
                      "double_date": {
                        "gte": "20180101",
                        "lte": "20180110"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "child": {
      "children": {
        "type": "child_mapping"
      },
      "aggs": {
        "ipo_year": {
          "terms": {
            "field": "str_pid",
            "size": 0
          },
          "aggs": {
            "login_num": {
              "sum": {
                "field": "double_loginNum"
              }
            },
            "having": {
              "bucket_selector": {
                "buckets_path": {
                  "count": "login_num"
                },
                "script": {
                  "lang": "expression",
                  "inline": "count>10"
                }
              }
            }
          }
        }
      }
    }
  }
}

0 个答案:

没有答案