7

我有看起来像这样的文件(这里有两个例子):

{
    "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
        }
    ]
}

数组中子文档的格式examples总是有 anevidence_source和 asource_score但是这些子文档的数量是可变的,每个都有不同的evidence_source值。

我想知道是否可以根据source_score与特定值匹配的值之一对具有这种格式的文档进行排序evidence_source。我真的很想能够做到这一点:

  • 按相关的source_score位置降序对文档进行排序。文档s 的结果排序将是 1234,6346。evidence_sourcefriendid
  • 按相关的source_score位置降序对文档进行排序。文档s 的结果排序将是 6346,1234。evidence_sourceparentid

我为做这样的事情而得出的最接近的结果是12,但我不相信它们完全符合我想要做的事情。

关于我如何解决这个问题的任何想法?我已经考虑了一些基于examples单独索引这些子文档的想法,但我对弹性搜索相当陌生,因此我正在寻找一些关于如何以最直接的方式实现我的目标的建议(这可能是一个梦想...)

更新elasticsearch 邮件列表上的帖子似乎表明这是不可能的,但我想知道这里的其他人是否有任何不同的想法!

4

1 回答 1

19

在 0.90 中,elasticsearch 添加了对基于嵌套文档内的字段进行排序的支持:

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

嵌套字段支持的排序在现有的排序选项之上具有以下参数:

  • nested_path- 定义要排序的嵌套对象。实际的排序字段必须是此嵌套对象内的直接字段。默认是使用从排序字段中最直接继承的嵌套对象。
  • nested_filter- 嵌套路径内的内部对象的过滤器应匹配,以便通过排序考虑其字段值。常见的情况是在嵌套过滤器或查询中重复查询/过滤器。默认情况下 nonested_filter 处于活动状态。

鉴于您的示例数据,以下查询应该为您提供您所追求的:

{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "examples.source_score": {
        "order": "desc",
        "nested_path": "examples",
        "nested_filter": {
          "term": {
            "examples.evidence_source": "friend"
          }
        }
      }
    }
  ]
}
于 2013-05-23T13:59:47.783 回答