2017-03-15 97 views
0

我的目標是在一個字段中查找最大值並在此找到的文檔中打印另一個字段。 到目前爲止,我查詢:Elasticsearch查詢 - 根據其他字段打印特定字段

{ 
     "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" 
      } 
     } 
     } 
    } 
    } 
} 

結果是

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

現在我需要打印text場在文檔中包含compound值0.8442等..謝謝你的忠告。

回答

0

我用小小的工作計劃實現了這個目標。這不是很美,但最終我得到了我想要的。 首先,我使用了第一個查詢的響應。比我從這些字典中抓取所有keys並執行新的查詢以查找某個文檔的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" 
     } 
     } 
    } 
    } 
} 

不是通過響應迭代,並通過該id領域

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']) 

現在的結果搜索文檔

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

這是文本我想要的東西。