Anda dapat mencoba kueri seperti berikut:
GET _search
{
"query": {
"query_string": {
"query": "*"
}
},
"size": 0,
"aggs": {
"@user_id": {
"terms": {
"field": "user_id",
"size": 0,
"order": {
"_term": "asc"
}
},
"aggs": {
"@image": {
"terms": {
"field": "image",
"size": 2,
"order": {
"_term": "asc"
}
},
"aggs": {
"@source": {
"top_hits": {
"size": 1
}
}
}
}
}
}
}
}
"size": 2 in @image
aggs memungkinkan Anda menentukan jumlah gambar. Outputnya akan seperti:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 16,
"max_score": 0,
"hits": []
},
"aggregations": {
"@user_id": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "user1",
"doc_count": 4,
"@image": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 2,
"buckets": [
{
"key": "image1",
"doc_count": 1,
"@source": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "AVQVaph2LY9bI6YPTNWp",
"_score": 1,
"_source": {
"user_id": "user1",
"image": "image1"
}
}
]
}
}
},
{
"key": "image2",
"doc_count": 1,
"@source": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "AVQVareVLY9bI6YPTPCg",
"_score": 1,
"_source": {
"user_id": "user1",
"image": "image2"
}
}
]
}
}
}
]
}
},
{
"key": "user2",
"doc_count": 4,
"@image": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 2,
"buckets": [
{
"key": "image1",
"doc_count": 1,
"@source": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "AVQVau9JLY9bI6YPTSPx",
"_score": 1,
"_source": {
"user_id": "user2",
"image": "image1"
}
}
]
}
}
},
{
"key": "image2",
"doc_count": 1,
"@source": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "AVQVavh1LY9bI6YPTSP0",
"_score": 1,
"_source": {
"user_id": "user2",
"image": "image2"
}
}
]
}
}
}
]
}
},
{
"key": "user3",
"doc_count": 4,
"@image": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 2,
"buckets": [
{
"key": "image1",
"doc_count": 1,
"@source": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "AVQVayTFLY9bI6YPTVZA",
"_score": 1,
"_source": {
"user_id": "user3",
"image": "image1"
}
}
]
}
}
},
{
"key": "image2",
"doc_count": 1,
"@source": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "AVQVazP4LY9bI6YPTWnK",
"_score": 1,
"_source": {
"user_id": "user3",
"image": "image2"
}
}
]
}
}
}
]
}
},
{
"key": "user4",
"doc_count": 4,
"@image": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 2,
"buckets": [
{
"key": "image1",
"doc_count": 1,
"@source": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "AVQVa16pLY9bI6YPTbgL",
"_score": 1,
"_source": {
"user_id": "user4",
"image": "image1"
}
}
]
}
}
},
{
"key": "image2",
"doc_count": 1,
"@source": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "AVQVa2a3LY9bI6YPTcuX",
"_score": 1,
"_source": {
"user_id": "user4",
"image": "image2"
}
}
]
}
}
}
]
}
}
]
}
}
}
person
Кирилл Полищук
schedule
14.04.2016