查询精确范围内的最大日期并获取结果中的选定字段

Querying on a max date in a precise range and getting selected fields in the results

我是 Elastic Search 的新手,在查询中使用多个过滤器(尤其是 "max")时遇到了一些问题

我目前正在处理由 Elastic Search 索引的庞大数据库。 有很多文件,每个文件都是关于一个特定服务器的所有信息。

软件偶尔会在这些服务器上 运行 并创建一个包含更新信息的新文档。

因此,信息存储如下:

Id : item1
ITDiscovery_Date : 29/03/2016
Information1 : ...
Information2 : ...

Id : item1
ITDiscovery_Date : 12/03/2016
Information1 : ...
Information2 : ...

Id : item2
ITDiscovery_Date : 16/02/2016
Information1 : ...
Information2 : ...

Id : item2 
ITDiscovery_Date : 27/01/2016 
Information1 : ...
Information2 : ...

以此类推

我的问题如下:

我正在尝试获取有关一台特定服务器的最新信息。为此,我想先过滤服务器的名称(例如 item456),然后获取该服务器在特定日期范围内的所有文档(例如从 01/01/2015 到今天),然后过滤最大日期,为了获得最新的信息,并得到所选字段的结果(例如 Information15、Information28 和 Information68)

我已经尝试了一些不同的请求,但无法让它工作,例如这个:

{
  "fields": [
    "Information15",
    "Information28",
    "Information68"
  ],
  "query": {
    "match": {
      "Id": "item456"
    }
  },
  "aggs": {
    "date_range": {
      "filter": {
        "range": {
          "ITDiscovery_Date": {
            "gte": 1420066800000,
            "lte": 1459241770000
          }
        }
      },
      "aggs": {
        "max_date": {
          "max": {
            "field": "ITDiscovery_Date"
          }
        }
      }
    }
  }
}

它 returns 所选日期范围内的所有文档,而不仅仅是具有最大日期的文档:

{
  "took" : 34,
  "timed_out" : false,
  "_shards" : {
    "total" : 982,
    "successful" : 982,
    "failed" : 0
  },
  "hits" : {
    "total" : 33,
    "max_score" : 15.364556,
    "hits" : [ {
      "_index" : "itdiscovery_2016.03.02",
      "_type" : "default",
      "_id" : "item456",
      "_score" : 15.364556,
      "fields" : {
        "Information15" : [ "XXX" ],
        "Information28" : [ "XXX" ],
        "Information68" : [ "XXX" ]
      }
    }, {
      "_index" : "itdiscovery_2016.03.23",
      "_type" : "default",
      "_id" : "item456",
      "_score" : 15.359651,
      "fields" : {
        "Information15" : [ "XXX" ],
        "Information28" : [ "XXX" ],
        "Information68" : [ "XXX" ]
      }
    } ]
  }, {
    ...
  },
  "aggregations" : {
    "date_range" : {
      "doc_count" : 33,
      "max_date" : {
        "value" : 1.45922382E12
      }
    }
  }
}

我终于找到了一个(临时)解决方案。

我使用筛选查询来获取指定日期范围内的结果。 然后我对 ITDiscovery_Date 使用排序并将结果限制为 1。 它得到了预期的最新结果。

例如:

{
  "fields": [
    "Information15",
    "Information28",
    "Information68"
  ],
  "sort": [
    { "ITDiscovery.Date.raw": {"order": "desc", "ignore_unmapped" : true}}
  ],
  "size": 1,
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "Id: item456",
          "analyze_wildcard": true
        }
      },
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "ITDiscovery.Date": {
                  "gte": 1420070400000,
                  "lte": 1459241770000
                }
              }
            }
          ],
          "must_not": []
        }
      }
    }
  }
}