I am learning to use elasticsearch and while following the documentation I came across this link:
https://www.elastic.co/guide/en/elasticsearch/guide/current/top-hits.html
where you explain how to use field collapsing. Initially the dynamic scripting did not work, so i added script.inline: on
script.indexed: on
to my elasticsearch.yml
and the GET queries worked, but the output is not according to the example:
Query:
GET /my_index/blogpost/_search?search_type=count
{
"query": {
"bool": {
"must": [
{ "match": { "title": "relationships" }},
{ "match": { "user.name": "John" }}
]
}
},
"aggs": {
"users": {
"terms": {
"field": "user.name.raw",
"order": { "top_score": "desc" }
},
"aggs": {
"top_score": { "max": { "script": "_score" }},
"blogposts": { "top_hits": { "_source": "title", "size": 5 }}
}
}
}
}
My Output:
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"users": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": []
}
}
}
dantuff
(Dan Tuffery)
July 1, 2015, 4:01pm
2
Can you post your mapping and an example doc?
Same as in the original example, here goes..
PUT /my_index/_mapping/blogpost
{
"properties": {
"user": {
"properties": {
"name": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
PUT /my_index/user/1
{
"name": "John Smith",
"email": "john@smith.com ",
"dob": "1970/10/24"
}
PUT /my_index/blogpost/2
{
"title": "Relationships",
"body": "It's complicated...",
"user": {
"id": 1,
"name": "John Smith"
}
}
PUT /my_index/user/3
{
"name": "Alice John",
"email": "alice@john.com ",
"dob": "1979/01/04"
}
PUT /my_index/blogpost/4
{
"title": "Relationships are cool",
"body": "It's not complicated at all...",
"user": {
"id": 3,
"name": "Alice John"
}
}
dantuff
(Dan Tuffery)
July 1, 2015, 6:40pm
4
I created a demo
index with your mapping and docs. When I run the query I get two aggregation results as expected
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"users": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "John Smith",
"doc_count": 1,
"blogposts": {
"hits": {
"total": 1,
"max_score": 0.35258877,
"hits": [
{
"_index": "demo",
"_type": "blogpost",
"_id": "2",
"_score": 0.35258877,
"_source": {
"title": "Relationships"
}
}
]
}
},
"top_score": {
"value": 0.3525887727737427
}
},
{
"key": "Alice John",
"doc_count": 1,
"blogposts": {
"hits": {
"total": 1,
"max_score": 0.24409992,
"hits": [
{
"_index": "demo",
"_type": "blogpost",
"_id": "4",
"_score": 0.24409992,
"_source": {
"title": "Relationships are cool"
}
}
]
}
},
"top_score": {
"value": 0.2440999150276184
}
}
]
}
}
}
darndest thing! I simply re-ran the mapping and place the documents again. I then re-ran the query and the results appear! I dont know what corrected it..