Elasticsearch 无痛脚本查询无法获取记录

Elasticsearch painless script query not working to fetch the records

我是弹性搜索的新手。我有一个带映射的索引

{
    "dda3dff12bd247d3_uat_gamma": {
        "aliases": {},
        "mappings": {
            "properties": {
                "actions": {
                    "properties": {
                        "action": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "actionDateTime": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "from_dc": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "logReferenceId": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "references": {
                            "properties": {
                                "consignment_ref": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "eu_ref": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "fulfillRef": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "grn_ref": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "irn_ref": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "order_item_ref": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

将动作清楚地作为一个数组。现在我必须在我的索引中找到带有空操作数组的文档。 我试过这个查询:

{
    "query": {
        "bool" : {
          "must" : {
            "match_all": {}
            },
            "filter" : {
                "script" : {
                    "script" : "doc['actions'].value.length >= 0"
                }
            }
        }
    }
}

但它给我错误,因为

{
    "error": {
        "root_cause": [
            {
                "type": "script_exception",
                "reason": "runtime error",
                "script_stack": [
                    "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:90)",
                    "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
                    "doc['actions'].value.length >= 0",
                    "    ^---- HERE"
                ],
                "script": "doc['actions'].value.length >= 0",
                "lang": "painless",
                "position": {
                    "offset": 4,
                    "start": 0,
                    "end": 32
                }
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "dda3dff12bd247d3_uat_gamma",
                "node": "xNE9fq6eR5-3hRq9y5Y3sQ",
                "reason": {
                    "type": "script_exception",
                    "reason": "runtime error",
                    "script_stack": [
                        "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:90)",
                        "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
                        "doc['actions'].value.length >= 0",
                        "    ^---- HERE"
                    ],
                    "script": "doc['actions'].value.length >= 0",
                    "lang": "painless",
                    "position": {
                        "offset": 4,
                        "start": 0,
                        "end": 32
                    },
                    "caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "No field found for [actions] in mapping with types []"
                    }
                }
            }
        ]
    },
    "status": 400
}

显然有此类文档可用。我正在使用 Elasticsearch 7.9。提前致谢。

curl --location --request GET 'http://localhost:4317/dda3dff12bd247d3_uat_gamma/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "query": {
        "bool" : {
          "must" : {
            "match_all": {}
            },
            "filter" : {
                "script" : {
                    "script" : "doc['\''actions'\''].value.length >= 0"
                }
            }
        }
    }
}
'

根据您的索引映射,actions 字段定义为 Object type 字段。您可以使用以下查询获取 actions 字段不可用或为空对象的文档。

{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "actions"
          }
        }
      ]
    }
  }
}