当前位置:  开发笔记 > 前端 > 正文

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

如何解决《ElasticSearch在所有文档的嵌套字段上排序》经验,为你挑选了1个好方法。

我试图弄清楚是否可以基于所有文档的嵌套字段进行排序,例如.在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"
            }
        },

]

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



1> Sloan Ahrens..:

使用嵌套排序(假设内部文档是类型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

推荐阅读
kikokikolove
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有