簡體   English   中英

如何按id中的文檔對Elasticsearch進行排序?

[英]How to sort Elasticsearch by documents in an id?

我正在使用 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是普通嵌套(對象數組)專用 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 開始計數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM