簡體   English   中英

如何通過Elasticsearch 6.7版中的無痛腳本獲取“嵌套”類型數組的大小?

[英]How do I get the size of a 'nested' type array through a Painless script in Elasticsearch version 6.7?

我正在使用Elasticsearch 6.7版。 我有以下映射:

{
    "customers": {
        "mappings": {
            "customer": {
                "properties": {
                    "name": {
                        "type": "keyword"
                    },
                    "permissions": {
                        "type": "nested",
                        "properties": {
                            "entityId": {
                                "type": "keyword"
                            },
                            "entityType": {
                                "type": "keyword"
                            },
                            "permission": {
                                "type": "keyword"
                            },
                            "permissionLevel": {
                                "type": "keyword"
                            },
                            "userId": {
                                "type": "keyword"
                            }
                        }
                    }
                }
            }
        }
    }
}

我想運行一個查詢,以顯示所有具有> 0權限的客戶。 我嘗試了以下方法:

{
    "query": {
        "bool": {
            "filter": {
                "script": {
                    "script": {
                        "lang": "painless",
                        "source": "params._source != null && params._source.permissions != null && params._source.permissions.size() > 0"
                    }
                }
            }
        }
    }
}

但這不會返回任何結果,因為params._source為null,因為根據此Stackoverflow帖子, params._source無法訪問_source文檔。 如何編寫無痛腳本,為我提供所有權限> 0的客戶?

解決方案1:將腳本與必須查詢一起使用

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "script": {
            "script": {
              "lang": "painless",
              "inline": """
                ArrayList st = params._source.permissions;
                if(st!=null && st.size()>0)
                  return true;
              """
            }
          }
        }
      ]
    }
  }
}

解決方案2:對嵌套字段使用存在查詢

您可以簡單地使用Exists查詢如下所示,以獲取具有> 0權限的客戶。

查詢:

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "must": [
       {
        "nested": {
          "path": "permissions",
          "query": {
            "bool": {
              "should": [
                {
                  "exists":{
                    "field": "permissions.permission"
                  }
                },
                {
                  "exists":{
                    "field": "permissions.entityId"
                  }
                },
                {
                  "exists":{
                    "field": "permissions.entityType"
                  }
                },
                {
                  "exists":{
                    "field": "permissions.permissionLevel"
                  }
                }
              ]
            }
          }
        }
      }]
    }
  }
}

解決方案3:創建確定的結構,但將空值添加到字段

另一種選擇是確保所有文檔都具有字段。 基本上,

  • 確保所有文檔都具有嵌套permissions文檔
  • 但是,對於那些沒有權限的人,只需將字段permissions.permission設置為0
  • 構造一個查詢,以幫助您相應地獲取此類文檔

以下是沒有權限的用戶的示例文檔:

POST mycustomers/customer/1
{
  "name": "john doe",
  "permissions": [
    {
      "entityId" : "null",
      "entityType": "null",
      "permissionLevel": 0,
      "permission": 0
    }
  ]
}

在這種情況下,查詢將像這樣簡單:

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "permissions",
            "query": {
              "range": {
                "permissions.permission": {
                  "gte": 1
                }
              }
            }
          }
        }
      ]
    }
  }    
}

希望這可以幫助!

暫無
暫無

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

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