Elasticsearch按嵌套对象排序

时间:2017-10-21 18:01:17

标签: sorting elasticsearch

我刚刚发现了弹性搜索,我试图进行初步查询。例如在我的情况下,每篇文章有几个价格,我想按最低价格对这些文章进行排序。因此我不会考虑其他的。这只是我的文章排序的最低价格 这是我的文章:

{
           "name": "john",
           "prices": [
              {
                 "price": "22"
              },
              {
                 "price": "28"
              },
              {
                 "price": "8"
              }
           ]
        },
        {
           "name": "peter",
           "prices": [
              {
                 "price": "7"
              },
              {
                 "price": "5"
              },
              {
                 "price": "2"
              }
           ]
        },
        {
           "name": "billy",
           "prices": [
              {
                 "price": "15"
              },
              {
                 "price": "17"
              }
           ]
        }

1 个答案:

答案 0 :(得分:1)

POST /_search

{
    "query" : {
        "match_all": {}
    },
    "sort" : [
        {
            "prices.price": {
                "order": "asc",
                "mode" : "min"
            }
        }
    ]
}

注意:如果您的prices个对象属于nested类型(而不是object),那么您还需要在"nested_path": "prices"内添加prices.price

有关排序的更多信息,请点击此处: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html