ElasticSearch在所有文档的嵌套字段上排序

时间:2016-01-04 23:08:40

标签: elasticsearch

我试图弄清楚是否可以基于所有文档的嵌套字段进行排序,例如。在JSON下面,字段是一个嵌套对象,我要找的是从所有文档中检索所有fields.name,然后按名称字段排序。我知道如果它是单个文档的一部分,我可以使用内部命中查询来做到这一点。

另一种选择是检索所有文档,然后在前端/中间层进行排序,这不是一个好的选择。

[ {
          "id": "42ddf6e1-23f5-4aed-9d3a-1ba3ac3677b1",
          "fields": [
            {
              "name": "asofheading"
            },
            {
              "name": "SEC_TYPE"
            }
          ],
},
{
          "id": "7579928e-2196-4f7d-aaf8-4bfe9e67b330",
          "fields": [
            {
              "name": "asofheading"
            },
            {
              "name": "SEC_TYPE"
            },
            {
              "name": "CUSIP"
            }
          ]
 },
        {
          "id": "3a0940c1-7495-400c-a204-cd9bc6966fae",
          "fields": [
            {
              "name": "AsofHeading"
            },
            {
              "name": "SECT_PROFILE"
            },
            {
              "name": "SECT_PROFILESP"
            }
        },

]

我想从所有文档中仅提取字段名称,然后按字段名称

排序

2 个答案:

答案 0 :(得分:5)

使用nested sorting(假设内部文档的类型为nested)。

因此,例如,您提供的文件:

POST /test_index/_search
{
    "sort" : [
       {
          "fields.name" : {
             "mode" :  "max",
             "order" : "asc",
             "nested_path" : "fields"
          }
       }
    ]
}

返回:

{
   "took": 8,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": null,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "2",
            "_score": null,
            "_source": {
               "id": "7579928e-2196-4f7d-aaf8-4bfe9e67b330",
               "fields": [
                  {
                     "name": "asofheading"
                  },
                  {
                     "name": "SEC_TYPE"
                  },
                  {
                     "name": "CUSIP"
                  }
               ]
            },
            "sort": [
               "sec_type"
            ]
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": null,
            "_source": {
               "id": "42ddf6e1-23f5-4aed-9d3a-1ba3ac3677b1",
               "fields": [
                  {
                     "name": "asofheading"
                  },
                  {
                     "name": "SEC_TYPE"
                  }
               ]
            },
            "sort": [
               "sec_type"
            ]
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "3",
            "_score": null,
            "_source": {
               "id": "3a0940c1-7495-400c-a204-cd9bc6966fae",
               "fields": [
                  {
                     "name": "AsofHeading"
                  },
                  {
                     "name": "SECT_PROFILE"
                  },
                  {
                     "name": "SECT_PROFILESP"
                  }
               ]
            },
            "sort": [
               "sect_profilesp"
            ]
         }
      ]
   }
}

以下是我用来测试它的代码:

http://sense.qbox.io/gist/2de98ca3e72663ce7af63c435deb5e85c6070088

答案 1 :(得分:0)

对于最新版本(此处为6.8),语法略有更改(请参见documentation),因此现在是:

POST /test_index/_search
{
    "sort" : [
       {
          "fields.name" : {
             "mode" :  "max",
             "order" : "asc",
             "nested": { "path": "fields" }
          }
       }
    ]
}