如何按ID中的文档对Elasticsearch进行排序?

问题描述

我正在使用Bonsai的免费层,并试图编写一个脚本来管理我的Elastic索引中的文档数。为了最大程度地保存文档,我想开始删除其中嵌套文档很多的文档。

示例:

{   
 "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个文档,因为存在大量的嵌套?理想情况下,我将能够通过此“文档计数”对弹性记录进行排序。

谢谢!

解决方法

如果您的authorscommentsphotos是专用弹性搜索nested数据类型的琐碎嵌套(对象数组) OR ,您可以执行以下操作:

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开始计数。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...