弹性搜索子项的总和

时间:2017-01-20 05:58:12

标签: elasticsearch

我尝试在弹性搜索中执行此类查询:

  

返回我在两个日期之间有一些日志的应用程序的所有设备,并为每个设备返回日志总数

为此我有亲子关系。我拥有设备信息的父设备类型,然后是具有每天日志数的子实体 device_logs

我尝试使用自定义分数功能运行以下查询。我确实得到了正确的设备,但得分中包含所有 device_logs 条目的总和,而不是日期范围中的条目。 不知道是否可以进行这种查询?

{
    "query": {
        "bool": {
            "filter" : 
            [
                {
                    "term": {"app": 347}
                }
            ],
            "must" :
            [
                {
                    "has_child": {
                        "type": "device_logs",
                        "inner_hits" : {},
                        "query": {
                            "bool": {
                                "filter": {
                                    "range": {
                                        "date": {
                                            "from": "2017-01-15T00:00:00Z",
                                            "include_lower": true,
                                            "include_upper": true,
                                            "to": "2017-01-17T23:59:59Z"
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                {
                    "has_child": {
                        "type": "device_logs",
                        "score_mode": "sum",

                        "query" : {
                            "function_score" : {
                                "script_score": {
                                    "script": "_score * doc['logs'].value"
                                }
                            }
                        }
                    }
                }
            ]
        }
    }
}

编辑:添加映射和一些文档

这里有映射:

"mappings": {
      "device": {
        "properties": {
          "app": {
            "type": "long",
            "include_in_all": false
          },
          "created_at": {
            "type": "date",
            "include_in_all": false
          },
          "id": {
            "type": "long",
            "include_in_all": false
          },
          "language": {
            "type": "keyword",
            "include_in_all": false,
            "ignore_above": 256
          },
          "last_log_at": {
            "type": "date",
            "include_in_all": false
          },
          "last_ping_at": {
            "type": "date",
            "include_in_all": false
          },
          "last_seen_at": {
            "type": "date"
          },
          "log_enabled": {
            "type": "boolean"
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              },
              "lowercase": {
                "type": "text",
                "analyzer": "case_insensitive_sort"
              }
            }
          },
          "os_version": {
            "type": "keyword",
            "include_in_all": false,
            "ignore_above": 256
          },
          "timezone": {
            "type": "keyword",
            "include_in_all": false,
            "ignore_above": 256
          },
          "type": {
            "type": "keyword",
            "ignore_above": 256
          },
          "udid": {
            "type": "keyword",
            "ignore_above": 256
          },
          "version": {
            "properties": {
              "build": {
                "type": "keyword",
                "include_in_all": false,
                "ignore_above": 256
              },
              "id": {
                "type": "long",
                "include_in_all": false
              },
              "version": {
                "type": "keyword",
                "include_in_all": false,
                "ignore_above": 256
              }
            }
          }
        }
      },
      "device_logs": {
        "_parent": {
          "type": "device"
        },
        "_routing": {
          "required": true
        },
        "properties": {
          "_": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "app": {
            "type": "long",
            "include_in_all": false
          },
          "date": {
            "type": "date",
            "include_in_all": false
          },
          "errors": {
            "type": "long",
            "include_in_all": false
          },
          "logs": {
            "type": "long",
            "include_in_all": false
          },
          "warnings": {
            "type": "long",
            "include_in_all": false
          }
        }
      }
    }

还有一些文件:

  {
        "_index": "devices",
        "_type": "device_logs",
        "_id": "22466_2017.01.17",
        "_score": 1,
        "_routing": "22466",
        "_parent": "22466",
        "_source": {
          "_": "22466_2017.01.17",
          "app": 200,
          "date": "2017-01-17T00:00:00Z",
          "logs": 660,
          "warnings": 238,
          "errors": 217
        }
      }


{
    "_index": "devices",
    "_type": "device",
    "_id": "22466",
    "_score": 1,
    "_source": {
      "id": 22466,
      "udid": "770CA14ED7FE861EC452",
      "name": "Edward's iPhone",
      "type": "iPhone7,2",
      "app": 200,
      "log_enabled": false,
      "created_at": "2016-12-21T10:55:02Z",
      "last_seen_at": "2017-01-19T10:07:33Z",
      "last_log_at": "2017-01-19T11:07:40.756275026+01:00",
      "language": "en-US",
      "os_version": "9.2",
      "timezone": "GMT+1",
      "version.id": 7305,
      "version.version": "1",
      "version.build": "100"
    }
  }

1 个答案:

答案 0 :(得分:0)

我已经解决了您的问题。

从第一次查看查询开始,我怀疑在对子文档应用函数得分之前,您没有过滤其中一个必须过滤器中的子文档。

我为此查询使用了以下文档集

parent doc
{
      "id": 22466,
      "udid": "770CA14ED7FE861EC452",
      "name": "Edward's iPhone",
      "type": "iPhone7,2",
      "app": 347,
      "log_enabled": false,
      "created_at": "2016-12-21T10:55:02Z",
      "last_seen_at": "2017-01-19T10:07:33Z",
      "last_log_at": "2017-01-19T11:07:40.756275026+01:00",
      "language": "en-US",
      "os_version": "9.2",
      "timezone": "GMT+1",
      "version.id": 7305,
      "version.version": "1",
      "version.build": "100"
    }

儿童文档

{
                  "_type": "device_logs",
                  "_id": "22466_2017.01.17",
                  "_score": 0,
                  "_routing": "22466",
                  "_parent": "22466",
                  "_source": {
                    "_": "22466_2017.01.17",
                    "app": 200,
                    "date": "2017-01-17T00:00:00Z",
                    "logs": 660,
                    "warnings": 238,
                    "errors": 217
                  }
                },
                {
                  "_type": "device_logs",
                  "_id": "22466_2017.02.17",
                  "_score": 0,
                  "_routing": "22466",
                  "_parent": "22466",
                  "_source": {
                    "_": "22466_2017.02.17",
                    "app": 200,
                    "date": "2017-01-17T00:00:00Z",
                    "logs": 200,
                    "warnings": 238,
                    "errors": 217
                  }
                },
                {
                  "_type": "device_logs",
                  "_id": "22466_2017.02.20",
                  "_score": 0,
                  "_routing": "22466",
                  "_parent": "22466",
                  "_source": {
                    "_": "22466_2017.02.20",
                    "app": 200,
                    "date": "2017-01-20T00:00:00Z",
                    "logs": 200,
                    "warnings": 238,
                    "errors": 217
                  }
                }

注意 - 第一个必须过滤仅过滤内部文件的文件。

请使用以下查询:

{
    "query": {
        "bool": {
            "filter": [{
                "term": {
                    "app": 347
                }
            }],
            "must": [{
                "has_child": {
                    "type": "device_logs",

                    "inner_hits": {},
                    "query": {
                        "bool": {
                            "filter": {
                                "range": {
                                    "date": {
                                        "from": "2017-01-15T00:00:00Z",
                                        "include_lower": true,
                                        "include_upper": true,
                                        "to": "2017-01-17T23:59:59Z"
                                    }
                                }
                            }
                        }

                    }
                }
            }, {
                "has_child": {
                    "type": "device_logs",
                    "score_mode": "sum",
                    "query": {
                        "function_score": {
                            "query": {
                                "bool": {
                                    "filter": {
                                        "range": {
                                            "date": {
                                                "from": "2017-01-15T00:00:00Z",
                                                "include_lower": true,
                                                "include_upper": true,
                                                "to": "2017-01-17T23:59:59Z"
                                            }
                                        }
                                    }
                                }
                            },
                            "score_mode": "sum",
                            "boost_mode": "sum",
                            "script_score": {
                                "script": "_score + doc['logs'].value"
                            }
                        }
                    }
                }
            }]
        }
    }
}

很少有人参考https://github.com/elastic/elasticsearch/issues/10051

以下是我将解释bool设置为true的响应

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 861,
    "hits": [
      {
        "_shard": "[array_index1][0]",
        "_node": "nnauJDrIS8-QCqicOMF23g",
        "_index": "array_index1",
        "_type": "device",
        "_id": "22466",
        "_score": 861,
        "_source": {
          "id": 22466,
          "udid": "770CA14ED7FE861EC452",
          "name": "Edward's iPhone",
          "type": "iPhone7,2",
          "app": 347,
          "log_enabled": false,
          "created_at": "2016-12-21T10:55:02Z",
          "last_seen_at": "2017-01-19T10:07:33Z",
          "last_log_at": "2017-01-19T11:07:40.756275026+01:00",
          "language": "en-US",
          "os_version": "9.2",
          "timezone": "GMT+1",
          "version.id": 7305,
          "version.version": "1",
          "version.build": "100"
        },
        "_explanation": {
          "value": 861,
          "description": "sum of:",
          "details": [
            {
              "value": 1,
              "description": "A match, join value 22466",
              "details": []
            },
            {
              "value": 860,
              "description": "A match, join value 22466",
              "details": []
            },
            {
              "value": 0,
              "description": "match on required clause, product of:",
              "details": [
                {
                  "value": 0,
                  "description": "# clause",
                  "details": []
                },
                {
                  "value": 1,
                  "description": "app:[347 TO 347], product of:",
                  "details": [
                    {
                      "value": 1,
                      "description": "boost",
                      "details": []
                    },
                    {
                      "value": 1,
                      "description": "queryNorm",
                      "details": []
                    }
                  ]
                }
              ]
            }
          ]
        },
        "inner_hits": {
          "device_logs": {
            "hits": {
              "total": 2,
              "max_score": 0,
              "hits": [
                {
                  "_type": "device_logs",
                  "_id": "22466_2017.01.17",
                  "_score": 0,
                  "_routing": "22466",
                  "_parent": "22466",
                  "_source": {
                    "_": "22466_2017.01.17",
                    "app": 200,
                    "date": "2017-01-17T00:00:00Z",
                    "logs": 660,
                    "warnings": 238,
                    "errors": 217
                  }
                },
                {
                  "_type": "device_logs",
                  "_id": "22466_2017.02.17",
                  "_score": 0,
                  "_routing": "22466",
                  "_parent": "22466",
                  "_source": {
                    "_": "22466_2017.02.17",
                    "app": 200,
                    "date": "2017-01-17T00:00:00Z",
                    "logs": 200,
                    "warnings": 238,
                    "errors": 217
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

请验证您的结果。