Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 29, 2015 14:17
Show Gist options
  • Save up1/aaab8b230f11f813e704 to your computer and use it in GitHub Desktop.
Save up1/aaab8b230f11f813e704 to your computer and use it in GitHub Desktop.
Elasticsearch Top Hit
PUT ec/product/1
{
"name" : "ABC",
"price" : 123,
"category" : ["1", "2"]
}
PUT ec/product/2
{
"name" : "ABC",
"price" : 1,
"category" : ["1", "2"]
}
PUT ec/product/3
{
"name" : "XYZ",
"price" : 1,
"category" : ["1", "2"]
}
GET ec/_search?search_type=count
{
"aggs": {
"terms": {
"terms": {
"field": "name",
"size" : 10
},
"aggs": {
"top_tag_hits": {
"top_hits": {
"_source": {
"include": [
"name", "price", "category"
]
}
, "size" : 1
}
}
}
}
}
}
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"terms": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "abc",
"doc_count": 2,
"top_tag_hits": {
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "ec",
"_type": "product",
"_id": "1",
"_score": 1,
"_source": {
"price": 123,
"name": "ABC",
"category": [
"1",
"2"
]
}
}
]
}
}
},
{
"key": "xyz",
"doc_count": 1,
"top_tag_hits": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "ec",
"_type": "product",
"_id": "3",
"_score": 1,
"_source": {
"price": 1,
"name": "XYZ",
"category": [
"1",
"2"
]
}
}
]
}
}
}
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment