Elastic Search 显示“START_OBJECT 的未知键”异常

问题描述 投票:0回答:4

我将以下查询发送到弹性搜索,以便获取

from
to
之间的值范围内的数据:

{
  "range" : {
    "variables.value.long" : {
      "from" : -1.0E19,
      "to" : 9.1E18,
      "include_lower" : true,
      "include_upper" : true,
      "boost" : 1.0
    }.
  }
}

尽管弹性搜索抛出以下错误:

{
    "error": {
        "root_cause": [
            {
                "type": "parsing_exception",
                "reason": "Unknown key for a START_OBJECT in [range].",
                "line": 2,
                "col": 13
            }
        ],
        "type": "parsing_exception",
        "reason": "Unknown key for a START_OBJECT in [range].",
        "line": 2,
        "col": 13
    },
    "status": 400
}

有人知道这个错误是什么意思以及为什么我会收到它吗?

elasticsearch
4个回答
1
投票

这里缺乏上下文,例如您的映射或您正在运行的完整查询,但这就是范围查询应如何查找您的文档。

创建索引

PUT test_andromachiii
{
  "mappings": {
    "properties": {
      "variables": {
        "properties": {
          "values": {
            "properties": {
              "long": {
                "type": "double"
              }
            }
          }
        }
      }
    }
  }
}

索引文件

POST test_andromachiii/_doc
{
  "variables": {
    "values": {
      "long": 9.1E18
    }
  }
}

运行查询

POST test_andromachiii/_search
{
  "query": {
    "range": {
      "variables.values.long": {
        "lte": -1.0E19,
        "gte": 9.1E18,
        "boost": 1
      }
    }
  }
}

注意 lte 表示小于或等于,gte 表示大于或等于。

回复

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_andromachiii",
        "_type" : "_doc",
        "_id" : "gtGj73cBbr4pOF0Is9my",
        "_score" : 1.0,
        "_source" : {
          "variables" : {
            "values" : {
              "long" : 9.1E18
            }
          }
        }
      }
    ]
  }
}

1
投票

此错误(有点神秘)表明您有一个带有对象值的密钥

range
,但该密钥无法识别。

这里的具体原因是您的

range
需要成为更高查询键的一部分,例如(即)
bool
查询
,而不是主查询的一部分。

信用:https://discuss.elastic.co/t/unknown-key-for-a-start-object-in-should/140008/3


0
投票

看起来您正在使用版本

<0.90.4
。如果是这种情况,只需将您的
range
包装在父
query
对象中即可:

{
  "query":{
    "range":{
      "variables.value.long":{
        "from":-1.0E19,
        "to":9.1E18,
        "include_lower":true,
        "include_upper":true,
        "boost":1.0
      }
    }
  }
}

如果您使用的是比该版本更新的版本,请注意:

0.90.4 中已弃用

from
to
include_lower
include_upper
参数,转而使用
gt
gte
lt
lte


0
投票

我在 OpenSearch 中遇到了同样的错误。我在查询中传递了两次主体,然后也在 API 调用中传递了两次。我删除了主体部分并仅传递了一次,它解决了问题。

代码错误:

const finalQuery = {
  index: this.configService.get('ALERT_INDEX'),
  body: {
    query,
    aggs: {
      offense_source_buckets: {
        terms: { field: 'offense_source' },
        aggs,
      },
    },
    size: 0, // No need to return individual documents, only aggregated results
  },
};

更正代码:

const finalQuery = {
    
  query,
    aggs: {
      offense_source_buckets: {
        terms: { field: ⁠ offense_source.keyword ⁠ },
        aggs,
      },
    },
    size: 0, // No need to return individual documents, only aggregated results
    
  };

return JSON.stringify(finalQuery, null, 2); // Return as JSON}

我收到此错误:

响应错误:parsing_exception:[parsing_exception]原因:[body]中的 START_OBJECT 的密钥未知。

© www.soinside.com 2019 - 2024. All rights reserved.