使用弹性搜索在文档中查找最小值

时间:2018-01-03 12:00:27

标签: elasticsearch

我们尝试聚合它返回所有文档的最小值。

我们有什么方法可以从每个文档中找到开始日期的最小值,比如使用elasticsearch吗?

输出应为“1989-03-01T00:00:00Z”:

{
"experience": [{
        "isCurrent": false,
        "endDate": "2000-06-01T00:00:00Z",
        "jobTitle": "a",
        "company": "a",
        "title": "a",
        "startDate": "1996-05-01T00:00:00Z"
    }, {
        "isCurrent": false,
        "endDate": "2012-01-01T00:00:00Z",
        "jobTitle": "b",
        "company": "B",
        "title": "b",
        "startDate": "2008-01-01T00:00:00Z"
    }, {
        "isCurrent": false,
        "endDate": "2007-10-01T00:00:00Z",
        "company_org": "",
        "jobTitle": "c",
        "company": "C",
        "companyUrl": "",
        "title": "c",
        "startDate": "2004-09-01T00:00:00Z"
    }, {
        "isCurrent": false,
        "endDate": "1993-03-01T00:00:00Z",
        "jobTitle": "d",
        "company": "D",
        "title": "d",
        "startDate": "1991-05-01T00:00:00Z"
    }, {
        "isCurrent": false,
        "endDate": "1991-05-01T00:00:00Z",
        "company_org": "",
        "jobTitle": "e",
        "company": "E",
        "companyUrl": "",
        "title": "e",
        "startDate": "1989-03-01T00:00:00Z"
    }]
}

1 个答案:

答案 0 :(得分:1)

如果您的experience字段具有nested数据类型,那么您可以使用以下带有nested inner_hits的查询检索您所期望的内容:

{
  "query": {
    "nested": {
      "path": "experience",
      "query": {
        "query_string": { "query": "*" }
      },
      "inner_hits": {
        "size": 1,
        "sort": { "startDate": "asc" }
      } 
    }
  }
}

这将为您提供experience具有最早startDate

的文档