Elasticsearch如何查询嵌套范围

时间:2018-10-15 11:39:35

标签: elasticsearch nested

在我的ES(6.4)中有一些数据,例如:

"hits": {
"total": 1,
"max_score": 1,
"hits": [
  {
    "_index": "tracking",
    "_type": "activity",
    "_id": "u9hzd2YB_M8C5mR3Zhkn",
    "_score": 1,
    "_source": {
      "locationid": "test1",
      "quality": [
        {
          "dataTimeBegin": "2018-08-13 00:00",
          "dataTimeEnd": "2018-08-13 00:00",
          "contacts": 0
        },
        {
          "dataTimeBegin": "2018-08-13 00:01",
          "dataTimeEnd": "2018-08-13 00:01",
          "contacts": 5
        },
   ...

我的问题现在是:如何通过嵌套数组进行查询? 我有一个映射:

"mappings": {
"activity": {
  "properties": {
    "quality": {
      "type": "nested",
      "properties": {
        "dataTimeBegin": {
          "type":   "date",
          "format": "yyyy-MM-dd HH:mm"
        },
        "dataTimeEnd": {
          "type":   "date",
          "format": "yyyy-MM-dd HH:mm"
        }
      }
    }
  }
}

并尝试以这种方式查询:

"query": {
  "bool": {
    "must": [
      {
        "match": {
          "locationid": "test1"
        }
      },
      {
      "nested": {
        "path": "quality",
        "query": {
          "bool": {
            "must": [
              {
                "range": {
                  "quality.dataTimeBegin": {
                    "from": "2018-08-13 00:00",
                    "to": "2018-08-13 00:00",
                    "include_lower": true,
                    "include_upper": true
                  }
                }
              }
            ]
          }
        }
      }
    ]
  }
}

它应该给我确切的一条记录,但是我收到了所有文件。 我也尝试了嵌套映射,也没有嵌套映射。 我该怎么办,错误在哪里?

0 个答案:

没有答案