首页 > 解决方案 > '在 [field] 下预期 [START_OBJECT],但得到了 [VALUE_STRING] - 范围聚合 - elasticserch 7.9

问题描述

我有一个long名为 的类型字段notice_period,其中我的值从15 daysto开始120 days。我正在寻找一个aggregate查询来获取所有间隔的频率(count),即{'to': 15}, {'from': 16, 'to': 30}, {'from': 31, 'to': 45}, {'from': 46, 'to': 60}, {'from': 61, 'to': 90}, {'from': 91}

bodyfor search 查询对我来说如下所示

{
    'size': 101,
    'query': {
        'ids': {
            'values': ['id1', 'id2']
        }
    },
    'aggs': {
        'notice_period': {
            'field': 'notice_period',
            'ranges': [
                {'to': 15},
                {'from': 16, 'to': 30},
                {'from': 31, 'to': 45},
                {'from': 46, 'to': 60},
                {'from': 61, 'to': 90},
                {'from': 91}]
        }
    }
}

因为我正在使用 python,所以我的代码摘录看起来像

from elasticsearch import Elasticsearch
es_client = Elasticsearch('localhost', 9200)
result  = es_client.search(index='my_index', body=body_mentioned_above)

但是由于我是 elasticsearch 新手,所以我遇到了错误'parsing_exception', 'Expected [START_OBJECT] under [field], but got a [VALUE_STRING] in [notice_period]' ,有人可以帮我解决这个问题,或者如果我以错误的方式进行操作,任何方向都会非常有帮助。提前致谢。

标签: elasticsearch-7

解决方案


我发现了我正在做的错误,我在范围级别又错过了一个键

所以基本上它适用于

{
    'size': 101,
    'query': {
        'ids': {
            'values': ['id1', 'id2']
        }
    },
    'aggs': {
        'notice_period': {
            'range': {
                'field': 'notice_period',
                'ranges': [
                    {'to': 15},
                    {'from': 16, 'to': 30},
                    {'from': 31, 'to': 45},
                    {'from': 46, 'to': 60},
                    {'from': 61, 'to': 90},
                    {'from': 91}]
        }}
    }
}

推荐阅读