簡體   English   中英

如何通過 Elasticsearch 中的腳本過濾嵌套文檔長度

[英]How to filter on nested document length by script in Elasticsearch

我正在嘗試過濾嵌套字段中至少包含給定數量的項目的文檔,但我不斷收到以下異常:

“caused_by”:{“type”:“illegal_argument_exception”,“reason”:“在映射中找不到[items]的字段”}

這是一個重現的示例代碼:

PUT store
{
  "mappings": {
    "properties": {
      "subject": {
        "type": "keyword"
      },
      "items": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "keyword"
          },
          "count": {
            "type": "integer"
          }
        }
      }
    }
  }
}

POST store/_bulk?refresh=true
{"create":{"_index":"store","_id":"1"}}
{"type":"appliance","items":[{"name":"Color TV"}]}
{"create":{"_index":"store","_id":"2"}}
{"type":"vehicle","items":[{"name":"Car"},{"name":"Bicycle"}]}
{"create":{"_index":"store","_id":"3"}}
{"type":"instrument","items":[{"name":"Guitar"},{"name":"Piano"},{"name":"Drums"}]}

GET store/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "script": {
            "script": {
              "source": "doc['items'].size() > 1"
            }
          }
        }
      ]
    }
  }
}

請注意,這只是我真正想做的一個簡化的過濾器腳本,如果我能克服這個問題,我很可能也能解決我的任務。 任何幫助,將不勝感激。

我最終用自定義分數方法解決了這個問題:

GET store/_search
{
  "min_score": 0.1,
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "params['_source']['items'].length > 1 ? 1 : 0"
            }
          }
        }
      ]
    }
  }
}

暫無
暫無

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

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