0

I have a long type field named notice_period , where I have values starting from 15 days to 120 days. I was looking for an aggregate query to get frequency(count) of all the intervals , i.e {'to': 15}, {'from': 16, 'to': 30}, {'from': 31, 'to': 45}, {'from': 46, 'to': 60}, {'from': 61, 'to': 90}, {'from': 91} .

the body for search query looks as following for me

{
    '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}]
        }
    }
}

as I am using python so my code excerpt looks like

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

but I am getting error as 'parsing_exception', 'Expected [START_OBJECT] under [field], but got a [VALUE_STRING] in [notice_period]' I am new to elasticsearch , Can someone please help me out with this, or any direction if I am doing it in a wrong way would be very helpful. Thanks in Advance.

lazarus
  • 677
  • 1
  • 13
  • 27

1 Answers1

0

I figured out the mistake I was doing, I was missing one more key at range level

so basically it worked with

{
    '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}]
        }}
    }
}
lazarus
  • 677
  • 1
  • 13
  • 27