且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

弹性搜索按数组中的单个嵌套文档键排序

更新时间:2023-02-08 20:19:55

根据嵌套文档中的字段进行排序的支持被添加到弹性搜索0.90:

Support for sorting based on fields inside of nested documents was added to elasticsearch in 0.90:

https:// github.com/elasticsearch/elasticsearch/issues/2662


嵌套字段支持的排序具有以下功能已经存在的排序选项的
顶部的g参数:

The sorting by nested field support has the following parameters on top of the already existing sort options:


  • nested_pa​​th - 定义要排序的嵌套对象。实际的
    排序字段必须是此嵌套对象内的直接字段。
    默认值是使用
    排序字段中最直接继承的嵌套对象。

  • nested_filter 过滤
    嵌套路径内的内部对象应该匹配,以便通过排序考虑其字段值为
    。常见的情况是在嵌套过滤器或查询内重复查询/
    过滤器。默认情况下,没有 nested_filter
    是活动的。

  • nested_path - Defines the on what nested object to sort. The actual sort field must be a direct field inside this nested object. The default is to use the most immediate inherited nested object from the sort field.
  • nested_filter - A filter the inner objects inside the nested path should match with in order for its field values to be taken into account by sorting. Common case is to repeat the query / filter inside the nested filter or query. By default no nested_filter is active.

鉴于您的示例数据,以下查询应该提供您以后的内容:

Given your example data, the following query should give you what you're after:

{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "examples.source_score": {
        "order": "desc",
        "nested_path": "examples",
        "nested_filter": {
          "term": {
            "examples.evidence_source": "friend"
          }
        }
      }
    }
  ]
}