我需要一些帮助来查询来自 ELasticSearch (1.7.3) 的记录 . 我们将获得执行的评估列表,并仅显示上次完成的评估,如下所示:

evaluation: [
  {
    id: 2,
    breaches: null
  },
  {
    id: 6,
    breaches: null
  },
  {
    id: 7,
    breaches: null
  },
  {
    id: 15,
    breaches: null
  },
  {
    id: 18,
    breaches: [
      "rule_one",
      "rule_two",
      "rule_three"
    ]
  },
  {
    id: 19,
    breaches: [
      "rule_one",
      "rule_two",
      "rule_three"
    ]
  }
]

现在我们需要根据执行的最新评估查询记录,即仅查询评估数组的最后一个对象 . 我们发现有一个inner_hits支持来排序和限制嵌套记录 . 为此,我们编写了一个查询,以desc顺序在 evaluation id 的基础上进行排序,并将其大小限制为1,如下所示:

{
  "query": {
    "bool": {
      "must": {
        "nested": {
          "path": " evaluation",
          "query": {
            "bool": {
              "must": {
                "term": {
                  " evaluation. breaches": "rule_one"
                }
              }
            }
          },
          "inner_hits": {
            "sort": {
              " evaluation.id": {
                "order": "desc"
              }
            },
            "size": 1
          }
        }
      }
    }
  }
}

请在下面找到映射:

evaluation: {
  type: "nested",
  properties: {
    id: {
      type: "long"
    },
    breaches: {
      type: "string"
    }
  }
}

我们尝试对记录进行排序,但它没有用,你能否建议一些其他方法来搜索嵌套记录的最后一个对象 .

谢谢 .