2015-02-23 36 views
2

我需要做一個查詢,以聚合方式列出整個對象。映射是這樣的:Elasticsearch - Lsit整個對象聚合

{ 
    "travelers": { 
     "properties": { 
      "traveler": "string", 
      "cars": { 
       "type":"nested", 
       "properties": { 
        "type": { 
         "type":"string" 
        }, 
        "color": { 
         "type":"string" 
        } 
       } 
      } 
     } 
    } 
} 

和查詢我可以是這樣的:

{ 
    "aggregations": { 
     "people": { 
      "terms": { 
       "field":"traveler" 
      } 
     }, 
     "aggregations": { 
      "cars": { 
       "nested": { 
        "path":"cars" 
       }, 
       "aggregations": { 
        "types": { 
         "terms": { 
          "field":"cars.type" 
         } 
        } 
       } 
      } 
     } 
    } 
} 

但此查詢只返回類型的汽車。我可以修改它來返回類型和顏色,但是我不能那樣告訴哪種顏色與哪種類型的汽車有關。我怎樣才能做到這一點?

回答

2

嵌套聚合只是簡單地確保每個嵌套對象都被視爲文檔,並且聚合發生在嵌套文檔級別而不是實際文檔級別。

因此,您需要使用顏色再進行一級聚合才能獲得所需的內容。

{ 
    "aggregations": { 
    "people": { 
     "terms": { 
     "field": "traveler" 
     } 
    }, 
    "aggregations": { 
     "cars": { 
     "nested": { 
      "path": "cars" 
     }, 
     "aggregations": { 
      "types": { 
      "terms": { 
       "field": "cars.type" 
      }, 
      "aggs": { 
       "colors": { 
       "terms": { 
        "field": "cars.color" 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
}