Search code examples
python-2.7elasticsearchelasticsearch-dsl

elasticsearch-dsl-py query filter with term and range


I'm trying to filter a query with term and range along with query-string. filter(range) and query string works but not filter(term). am i doing something wrong?

es = Elasticsearch([{'host': '192.168.121.121', 'port': 9200}])

index = Index("filebeat-*",using=es)
search = index.search()
searchStr = "OutOfMemoryError"

search = search.query("query_string", query=searchStr)
search = search.filter('range' ,  **{'@timestamp': {'gte': 1589399137000 , 'lt': 1589399377000, 'format' : 'epoch_millis'}})
search = search.filter('term'  , **{'can.deployment': 'can-*' })
response = search.execute(ignore_cache=True)
print(response.hits.total)
print(response.hits.hits._source.can.deployment)

json:

filter-term - ['hits']['hits']['_source']['can']['deployment']
filter-range- ['hits']['hits']['_source']['@timestamp']
{
  "hits" : {
    "total" : 138351328,
    "max_score" : 6.5700893,
    "hits" : [
      {
        "_index" : "filebeat-6.1.2-2020.05.13",
        "_type" : "doc",
        "_score" : 2.0166037,
        "_source" : {
          "@timestamp" : "2020-05-13T01:14:03.354Z",
          "source" : "/var/log/gw_rest/gw_rest.log",
          "message" : "[2020-05-13 01:14:03.354] WARN can_gw_rest [EventLoopGroup-3-2]: An exceptionCaught() event was fired.OutOfMemoryError,
          "fileset" : {...},
          "can" : {
            "level" : "WARN",
>>>>>>>>    "message" : "An exceptionCaught() event was fired- OutOfMemoryError,
            "timestamp" : "2020-05-13 01:14:03.354",
>>>>>>>>    "deployment" : "can-6b721b93965b-w3we4-4074-9903"
          }
        }
      }
    ]
  }
}

Solution

  • I actually didn't need a filter(term). this worked:

    dIds=response['hits']['hits'][1]['_source']['can']['deployment']
    print(dIds)
    #loop through the response
    for i in response['hits']['hits']:
              id = i['_source']['can']['deployment']
              print(id)