gpt4 book ai didi

elasticsearch - 无痛但 'No field found'遍历所有项目

转载 作者:行者123 更新时间:2023-12-02 22:54:47 26 4
gpt4 key购买 nike

我正在尝试使用无痛脚本进行选择查询,但是在响应中我不断收到错误消息,指出该脚本无效。

我要在这个简化脚本中尝试做的是检查给定的参数年龄是否都在“unit.info”中的年龄之后。

错误:

    "if(containsAge(doc['unitHolder.units'], params.ages)){",
" ^---- HERE",...
"caused_by": {
"type": "illegal_argument_exception",
"reason": "No field found for [unitHolder.units] in mapping with types [pack]"
}

请求查询:
    {
"query": {
"bool": {
"must": [
{
"script": {
"script": {
"source": "boolean containsAge(def unit, def ages)
{
if(unit.info.children.minAge != null)
{
int nrAdults = 0;
int nrChildren = 0;
int nrInfants = 0;

for (age in ages)
{
if (age < unit.info.children.maxAge.value)
{
nrAdults++;
}else if(age > unit.info.children.minAge.value)
{
nrInfants++;
}else{
nrChildren++;
}
}
if (nrAdults > 2)
{
return true;
}
}
return false;
}
if(containsAge(doc['unitHolder.units'], params.ages))
{
return true;
}
return false;",
"lang": "painless",
"params": {
"ages": [
50,
35
]
}
}
}
}
]
}
},
"size": 10
}

映射:
"mappings": {
"pack": {
"properties": {
"unitHolder": {
"properties": {
"createDate": {
"type": "date"
},
"units": {
"properties": {
"info": {
"properties": {
"children": {
"properties": {
"maxAge": {
"type": "long"
},
"minAge": {
"type": "long"
}
}
}
}
}
}
}
}
}
}
}
}

最佳答案

这是因为doc[<fieldName>]访问doc值,所以它不适用于嵌套结构。从Elasticsearch文档中:

"Doc-values can only return "simple" field values like numbers, dates, geo- points, terms, etc, or arrays of these values if the field is multi-valued. It can not return JSON objects."



您需要将 params._source[unitHolder]放入Map型变量中,然后查看该变量是否包含 unit

更新为包括一些示例:
Map unitHolderMap = params._source['unitHolder`];
if (unitHolderMap.containsKey('units')){
// get the ages from their respective properties and evaluate them
}

关于elasticsearch - 无痛但 'No field found'遍历所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58643559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com