Elasticsearch-嵌套字段排序

丹·罗布

我有一个由以下定义的索引:

    {
    "mappings": {
        "properties": {
            "firstName": {
                "type": "keyword"
            },
            "lastName": {
                "type": "keyword"
            },
            "affiliations": {
                "type": "nested",
                "properties": {
                    "organisation": {
                        "type": "keyword"
                    },
                    "team": {
                        "type": "keyword"
                    },
                    "dateBeginning": {
                        "type": "date",
                        "format": "yyyy-MM-dd"
                    },
                    "dateEnding": {
                        "type": "date",
                        "format": "yyyy-MM-dd"
                    },
                    "country": {
                        "type": "keyword"
                    }
                }
            }
        }
    }
    }

基本上,对于每个研究人员(researchers我是如何命名索引的),我都希望按照dateBeginning的降序对从属关系进行排序。我已经阅读了EL官方文档中的内部命中资料,但不确定其工作原理,我尝试通过以下方法为研究人员尝试_id : 3

    {
    "query": {
    "nested": {
        "path": "affiliations",
        "query": {
            "match": { "_id": 3 }
        },
        "inner_hits": {
            "sort" : [
            {
                "affiliations.dateBeginning" : {
                    "order" : "desc",
                    "nested": {
                        "path": "affiliations",
                        "filter": {
                            "term": { "_id": 3 }
                        }
                    }   
                }
            }
            ]
        } 
      }
      }
    }

它实际上并没有工作。

为研究者建立了两个隶属关系_id : 3,一个设置了dateBeginning,另一个设置2015-06-302017-06-30所以我也尝试过这个:

    {
    "sort" : [
            {
                "affiliations.dateBeginning" : {
                    "order" : "desc",
                    "nested": {
                        "path": "affiliations"
                }   
            }
        }
    ],
    "query": {
        "nested": {
            "path": "affiliations",
            "query": {
                "match": { "_id": 3 }
            }
        }
      }
    }

并且它不会按dateBeginning对从属关系进行排序。

我也曾尝试使用SQL API进行此操作(因为我对SQL语言更加熟悉),但仍然无法获得所需的数据。

因此,我对ElasticSearch还是很陌生,正在使用version 7.10,但我不知道该怎么办。

关于我在这里做错的任何建议吗?

编辑

这是该索引中文档的示例:

            {
            "took": 1,
            "timed_out": false,
            "_shards": {
                "total": 1,
                "successful": 1,
                "skipped": 0,
                "failed": 0
            },
            "hits": {
                "total": {
                    "value": 1,
                    "relation": "eq"
                },
                "max_score": 1.0,
                "hits": [{
                    "_index": "researchers",
                    "_type": "_doc",
                    "_id": "3",
                    "_score": 1.0,
                    "_source": {
                        "firstName": "Kimmich",
                        "lastName": "Yoshua",
                        "affiliations": [{
                                "organisation": "University of Ottawa",
                                "team": "Neural Network Elite Team",
                                "dateBeginning": "2015-06-30",
                                "datEnding": "2017-01-31",
                                "country": "Canada"
                            },
                            {
                                "organisation": "University of Montréal",
                                "team": "Picture processing team",
                                "dateBeginning": "2017-06-30",
                                "dateEnding": null,
                                "country": "Canada"
                            }
                        ]
                    }
                }]
            }
        }
乔·索罗辛

一旦进入嵌套查询,内部匹配就不需要额外的嵌套查询。删除它,排序将正常工作:

{
  "query": {
    "nested": {
      "path": "affiliations",
      "query": {
        "match": {
          "_id": 3
        }
      },
      "inner_hits": {
        "sort": [
          {
            "affiliations.dateBeginning": {
              "order": "desc"
            }
          }
        ]
      }
    }
  }
}

请注意,这不会对顶级匹配进行排序-仅对内部匹配进行排序。但是您可以按如下所示的值在顶层进行排序affiliations.dateBeginning

POST researchers/_search
{
  "sort": [
    {
      "affiliations.dateBeginning": {
        "order": "desc",
        "nested_path": "affiliations"
      }
    }
  ]
}

但请注意,语法现在略有不同:path我们不是在说nested_path

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章