0

My goal is to find max value in one field and print another field in this found document. My query so far:

{
        "fields": ["text"], //NOT WORKING
  "query": {
    "query_string": {
      "query": "_type:bmw AND _exists_:car_type",
      "analyze_wildcard": True
    }
  },
  "size": 0,
  "aggs": {
    "2": {
      "terms": {
        "field": "compound",
        "size": 5,
        "order": {
          "2-orderAgg": "desc"
        }
      },
      "aggs": {
        "2-orderAgg": {
          "max": {
            "field": "compound"
          }
        }
      }
    }
  }
}

Result is

'buckets': [{'doc_count': 1, '2-orderAgg': {'value': 0.8442}, 'key': 0.8442}, {'doc_count': 1, '2-orderAgg': {'value': 0.7777}, 'key': 0.7777}, {'doc_count': 1, '2-orderAgg': {'value': 0.7579}, 'key': 0.7579}, {'doc_count': 1, '2-orderAgg': {'value': 0.6476}, 'key': 0.6476}, {'doc_count': 1, '2-orderAgg': {'value': 0.6369}, 'key': 0.6369}]

Now I need to print text field in document contains compound value 0.8442 and so on.. Thank you for your advice.

bzadm
  • 133
  • 2
  • 9

1 Answers1

0

I achieved this with a small workaroud. It's not pretty but at final I get what I wanted. Firstly I used response from first query. Than I grabbed all keys from those dictionary and perform new query to find certain document's id.

{
  "size": 0,
  "query": {
    "query_string": {
      "analyze_wildcard": True,
      "query": "_type:bmw AND compound:"+str(0.8442)+" AND _exists_:car_type"
    }
  },
  "aggs": {
    "3": {
      "terms": {
        "field": "id_str",
        "size": 20,
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}

than iterate through response and search document by this id field

for y in res1:
    res3 = es.search(index='indexname', body={
                      "size" : 1,
                      "query": {
                        "bool": {
                          "must": [
                            {
                              "match": {
                                "id_str": y['key']
                              }
                            }
                          ]
                        }
                      }
                    })
                    for x in res3['hits']['hits']:
                        print (x['_source']['text'])

now result is

Diamond stitch leather is a great addition to any custom vehicle. Prices start from 2k! @bmw i8 getting under car... 

which is text what I wanted.

bzadm
  • 133
  • 2
  • 9