Elasticsearch:查询不包含字段'X'的最新内容

时间:2017-09-22 19:18:17

标签: elasticsearch

我有以下搜索查询:

{
  "query": {
    "match": {
      "name": "testlib"
    }
  }
}

当我执行此查询时,我得到以下三个结果。我现在要做的只是返回一个结果:不包含@timestamp的最新version_pre。所以在这种情况下,只返回AV6qvDXDyHw9vNh6Wlpl

[
    {
        "_index": "testsoftware",
        "_type": "software",
        "_id": "AV6qvDXDyHw9vNh6Wlpl",
        "_score": 0.2876821,
        "_source": {
            "@timestamp": "2017-09-21T11:02:15-04:00",
            "name": "testlib",
            "version_major": 1,
            "version_minor": 0,
            "version_patch": 1
        }
    },
    {
        "_index": "testsoftware",
        "_type": "software",
        "_id": "AV6qvDF5MtcMTuGknsVs",
        "_score": 0.18232156,
        "_source": {
            "@timestamp": "2017-09-20T17:21:35-04:00",
            "name": "testlib",
            "version_major": 1,
            "version_minor": 0,
            "version_patch": 0
        }
    },
    {
        "_index": "testsoftware",
        "_type": "software",
        "_id": "AV6qvDnVyHw9vNh6Wlpn",
        "_score": 0.18232156,
        "_source": {
            "@timestamp": "2017-09-22T13:56:55-04:00",
            "name": "testlib",
            "version_major": 1,
            "version_minor": 0,
            "version_patch": 2,
            "version_pre": 0
        }
    }
]

1 个答案:

答案 0 :(得分:2)

使用排序(https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html)和https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-exists-query.html

import { Ng4DropdownModule } from 'ng4-material-dropdown';

甚至,通过查询字符串:

{
"size" : 1,
"sort" : [{ "@timestamp" : {"order" : "asc"}}],
"query" : {
"bool": {
    "must_not": {
        "exists": {
            "field": "version_pre"
        }
    }
}