我不認爲有一種方法可以做你直接問什麼,但(雖然見GitHub的問題here和here)。你或許可以用scripted metric aggregation一起破解一些東西,雖然這也不是很理想(我假設不會很好地擴展,儘管我沒有嘗試過)。
與你貼什麼,它可以找出很多語言用戶如何說話很輕鬆:
POST /test_index/_search?search_type=count
{
"aggs": {
"humans": {
"terms": { "field": "hid" },
"aggs": {
"num_of_langs": {
"value_count": { "field": "lang" }
}
}
}
}
}
但是,這似乎並沒有真正的是你在問什麼。
但是,如果稍微修改架構,則可以使用bool和has_child過濾器的組合來解決問題(或多或少)。這是一種方法。
我拿走了您發佈的文檔,併爲每個"hid"
提取了「父」對象。我用的是建立了一個父子關係的映射,然後批量索引的文檔:
DELETE /test_index
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"human": {
"properties": {
"hid": { "type": "long" }
}
},
"has_lang": {
"_parent": { "type": "human" },
"properties": {
"hid": { "type": "long" },
"lang": { "type": "string" }
}
}
}
}
PUT /test_index/_bulk
{"index":{"_index":"test_index", "_type":"human", "_id":1}}
{"hid":1}
{"index":{"_index":"test_index", "_type":"human", "_id":2}}
{"hid":2}
{"index":{"_index":"test_index", "_type":"human", "_id":3}}
{"hid":3}
{"index":{"_index":"test_index", "_type":"human", "_id":4}}
{"hid":4}
{"index":{"_index":"test_index", "_type":"human", "_id":5}}
{"hid":5}
{"index":{"_index":"test_index", "_type":"human", "_id":6}}
{"hid":6}
PUT /test_index/_bulk
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":1, "_id":1}}
{"hid":1,"lang":"eng"}
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":1, "_id":2}}
{"hid":1,"lang":"ger"}
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":1, "_id":3}}
{"hid":1,"lang":"fr"}
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":2, "_id":4}}
{"hid":2,"lang":"eng"}
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":2, "_id":5}}
{"hid":2,"lang":"jap"}
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":3, "_id":6}}
{"hid":3,"lang":"eng"}
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":4, "_id":7}}
{"hid":4,"lang":"ger"}
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":5, "_id":8}}
{"hid":5,"lang":"eng"}
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":5, "_id":9}}
{"hid":5,"lang":"ger"}
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":6, "_id":10}}
{"hid":6,"lang":"eng"}
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":6, "_id":11}}
{"hid":6,"lang":"jap"}
然後我就可以查詢誰講某種語言的人,但沒有其他,具體如下:
POST /test_index/human/_search
{
"filter": {
"bool": {
"must": [
{
"has_child": {
"type": "has_lang",
"filter": { "term": { "lang": "ger" } }
}
}
],
"must_not": [
{
"has_child": {
"type": "has_lang",
"filter": {
"not": {
"filter": { "term": { "lang": "ger" } }
}
}
}
}
]
}
}
}
...
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "human",
"_id": "4",
"_score": 1,
"_source": {
"hid": 4
}
}
]
}
}
你仍然需要爲每種語言做到這一點,所以這種方法可能並不理想,但希望它能讓你更接近。
我也嘗試使用聚合來獲得你想要的答案,但從來沒有找到一種方法來使它工作。如果/當減速器集合得到實施時,如果我正確理解了這個想法,那麼大概會解決這類問題。
這裏是我使用的代碼:
http://sense.qbox.io/gist/0615ec52346ae6e547988b156b221484dbfde50c
複雜的解決方案,我想以取代快速elasticsearch統計經典的關係數據庫,但似乎難以查詢數據 –