ElasticSearch parent-child query results are not what I want, please help me see my problem


(zzalyp) #1

Hi:

I create Indexing Parents and Children, the parent document stores user basic information, and the child document stores login information.

How do I query user information that is older than 18 years old and has logged in more than 5 times during the time period from 2018-01-01 to 2018-01-10?

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
}

(zzalyp) #2

My query

{
  "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"
                }
              }
            }
          }
        }
      }
    }
  }
}