如何按id中的文档对elasticsearch进行排序?

myss37ts  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(461)

我正在使用免费的盆景层,并试图编写一个脚本来管理我的弹性索引中的文档数量。为了最大限度地增加可以保存的文档数量,我想开始删除其中有许多嵌套文档的文档。
例子:

{   
 "title": "Spiderman saves child from well",   
 "body":  "Move over, Lassie! New York has a new hero. But is he also a menace?",   
 "authors": [
   { 
      "name":  "Jonah Jameson",       
      "title": "Sr. Editor",     
   },     
   {       
      "name":  "Peter Parker",       
      "title": "Photos",     
   }   
  ],   
 "comments": [     
   {       
      "username": "captain_usa",       
      "comment":  "I understood that reference!",     
   },     
   {       
      "username": "man_of_iron",       
      "comment":  "Congrats on being slightly more useful than a ladder.",     
   }   
  ],   
 "photos": [ 
   {       
      "url":      "https://assets.dailybugle.com/12345",       
      "caption":  "Spiderman delivering Timmy back to his mother",     
   }   
  ] 
 }

elastic中有没有什么东西会告诉我,由于大量嵌套,这个文档实际上是6个文档?理想情况下,我可以通过这个“文档计数”对弹性记录进行排序。
谢谢!

cngwdvgl

cngwdvgl1#

如果你的 authors , comments 以及 photos 是普通嵌套的(对象数组)或专用的elasticsearch nested 数据类型,可以执行以下操作:

GET bonsai/_search
{
  "_source": [""], 
  "sort": [
    {
      "_script": {
        "type": "number",
        "script": {

          "source": """
            def count = 1; // top level doc count is 1
            for (def entry : params._source.values()) {
              if (entry instanceof ArrayList) {
                count += entry.size()
              }
            }
            return count;
          """
        }
      }
    }
  ]
}

我真的不知道上面的文档大小是6——所以我推测这是因为你也计算了顶级文档。可以在脚本中从0开始计算。

相关问题