0

I am trying to add an aggs query in Python's sdk for elasticsarch. Btu I can't seem to be able to make it work. E.g

body = {
    "query":{
        "bool":{
            "must_not":{
                "term":{
                    "dst":"-"
                }
            }
        }
    },
    "aggs":{
        "sources":{
            "terms":{
                "field":'src'
            }
        }
    }
}

Executing

>>es.indices.validate_query(index="myindex", body=body)
{u'valid': False, u'_shards': {u'successful': 11, u'failed': 0, u'total': 11}}

Executing the same without the 'aggs' key validates correctly. How do I pass the aggregations in a dict like query to use with python library?

Apostolos
  • 7,763
  • 17
  • 80
  • 150
  • I think it's simply because you have single quotes `'src'` instead of double quotes `"src"`. JSON needs double quotes. Add `explain=true` in your call and you should get some insights. – Val Sep 30 '16 at 09:43
  • using explain I get request does not support [aggs]'....Shouldn't it not support it? – Apostolos Sep 30 '16 at 09:50
  • Yes, my bad, somehow forgot that the Validation API only checks queries and not aggs. It's not supported, indeed. – Val Sep 30 '16 at 09:54
  • So how do I execute aggregations with python library? – Apostolos Sep 30 '16 at 09:56
  • You're not using the appropriate method, simply use [`es.search()`](https://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch.Elasticsearch.search) instead – Val Sep 30 '16 at 10:01
  • What I meant to ask was can I not validate a query with aggregations? – Apostolos Sep 30 '16 at 10:02
  • 1
    Apparently not :) – Val Sep 30 '16 at 10:28

0 Answers0