2017-10-18 76 views
1

我有一個(簡單)的結構是這樣的文件:如何在布爾每場比賽只返回一個文件

{uuid: 1, timestamp: "2017-10-18T01:30:00.000Z", "key": "value"} 
{uuid: 2, timestamp: "2017-10-18T01:30:00.000Z", "key": "value"} 
{uuid: 1, timestamp: "2017-10-18T01:25:00.000Z", "key": "value"} 
{uuid: 2, timestamp: "2017-10-18T01:25:00.000Z", "key": "value"} 
{uuid: 1, timestamp: "2017-10-18T01:20:00.000Z", "key": "value"} 
{uuid: 2, timestamp: "2017-10-18T01:20:00.000Z", "key": "value"} 

現在,我試圖讓每uuid只有一個單證相符,像這樣(與最新的時間戳):

query: { 
    bool: { 
     should: [{ 
      match: { 
       uuid: 1 
      } 
     }, 
     { 
      match: { 
       uuid: 2 
      } 
     }] 
    } 
} 

然而,上述查詢返回兩個uuid的多重文件。如何修改我的查詢與uuid12返回兩個最新的文件,像這樣:

{uuid: 1, timestamp: "2017-10-18T01:30:00.000Z", "key": "value"} 
{uuid: 2, timestamp: "2017-10-18T01:30:00.000Z", "key": "value"} 

回答

3

我建議使用再加上top_hits子聚集terms聚集,像這樣:

{ 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "term": { 
      "uuid.keyword": 1 
      } 
     }, 
     { 
      "term": { 
      "uuid.keyword": 2 
      } 
     } 
     ] 
    } 
    }, 
    "aggs": { 
    "uuids": { 
     "terms": { 
     "field": "uuid.keyword" 
     }, 
     "aggs": { 
     "latest": { 
      "top_hits": { 
      "size": 1, 
      "sort": { 
       "timestamp": "desc" 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

這查詢結果時出現以下錯誤消息:'{「error」:{「root_cause」:[{「type」:「parsing_exception」,「reason」:「[field] [Expected [START_OBJECT],但[VALUE_STRING] in [術語]「,」line「:1,」col「:99}],」type「:」parsing_exception「,」reason「:」[字段]下的期望[START_OBJECT],但得到[V ALUE_STRING] in [terms]「,」line「:1,」col「:99},」status「:400}' – Pono

+0

我的不好...修正了! – Val

+0

現在,ES關於「默認情況下禁用文本字段中的Fielddata」的嗚聲。我應該在uuid字段中啓用它嗎? – Pono