弹性搜索按数组中的单个嵌套文档键排序 [英] Elasticsearch sort by single nested document key in array

查看:151
本文介绍了弹性搜索按数组中的单个嵌套文档键排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的文件(这里有两个例子):

I have documents which look like this (here are two examples):

{
    "id": 1234,
    "title": "the title",
    "body": "the body",
    "examples": [
        {
            "evidence_source": "friend",
            "source_score": 15
        },
        {
            "evidence_source": "parent",
            "source_score": 12
        }
    ]
}

{
    "id": 6346,
    "title": "new title",
    "body": "lots of content",
    "examples": [
        {
            "evidence_source": "friend",
            "source_score": 10
        },
        {
            "evidence_source": "parent",
            "source_score": 27
        },
        {
            "evidence_source": "child",
            "source_score": 4
        }
    ]
}

示例数组中子文档的格式将始终有一个 evidence_source 和一个 source_score ,但是这些子文档将有一个变量,每一个都有不同的 evidence_source 值。

The format of the sub-documents in the examples array will always have an evidence_source and a source_score but there will be a variable amount of these sub-documents, each with different evidence_source values.

我想知道是否可以根据与特定的 source_score 值匹配的其中一个 evidence_source 值。我真的很想这样做:

I am wondering if it is possible to sort documents with this format based on one of the source_score values matched to a specific evidence_source value. I'd really like to be able to do this:


  • 通过 source_score 相关的 evidence_source 朋友下降。文件 id 的排序将为1234,6346。

  • 通过 source_score descending相关的 evidence_source parent 。文件 id 的顺序将为6346,1234。

  • Sort documents by source_score descending where the related evidence_source is friend. The resulting ordering of the document ids would be 1234,6346.
  • Sort documents by source_score descending where the related evidence_source is parent. The resulting ordering of the document ids would be 6346,1234.

我想出的最接近的结果是这样做: 1 2 ,但我不相信他们完全符合我想要做的。

The closest results that I'm come up with for doing something like this are 1 and 2 but I don't believe that they get at exactly what I want to do.

有关我如何去做的任何想法?我已经考虑了基于这些示例子文档的索引分开的一些想法,但我对弹性搜索是相当新的,因此我正在寻找一个如何实现我的建议目标最直接的方式(可能是管道梦想...)

Any ideas about how I might go about this? I've contemplated some ideas based on indexing these examples sub-documents separately, but I'm fairly new to elasticsearch and so am looking for some advice on a how to achieve my goal in the most straightforward manner (which may be a pipe-dream...)

更新弹性搜索邮件列表似乎表明这是不可能的,但我

Update: A post on the elasticsearch mailing list seems to indicate that this is NOT possible, but I'm wondering if someone else here has any different ideas!

推荐答案

根据嵌套文档中的字段进行排序的支持被添加到弹性搜索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"
          }
        }
      }
    }
  ]
}

这篇关于弹性搜索按数组中的单个嵌套文档键排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆