# Example on Field Collapsing not working for me

**URL:** <https://discuss.elastic.co/t/example-on-field-collapsing-not-working-for-me/24743>\
**Category:** Elasticsearch\
**Created:** [July 1, 2015, 3:49pm UTC](https://discuss.elastic.co/t/example-on-field-collapsing-not-working-for-me/24743 "2015-07-01T15:49:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deepak\_Paramanand](https://avatars.discourse-cdn.com/v4/letter/d/ee59a6/32.png) [@Deepak\_Paramanand](https://discuss.elastic.co/u/Deepak_Paramanand)\
**Post date:** [July 1, 2015, 3:49pm UTC](https://discuss.elastic.co/t/example-on-field-collapsing-not-working-for-me/24743/1 "2015-07-01T15:49:56Z")

</div>

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](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": []  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![dantuff](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dantuff](https://discuss.elastic.co/u/dantuff)\
**Post date:** [July 1, 2015, 4:01pm UTC](https://discuss.elastic.co/t/example-on-field-collapsing-not-working-for-me/24743/2 "2015-07-01T16:01:56Z")

</div>

Can you post your mapping and an example doc?

---

<div class="post-metadata">

**Author:** ![Deepak\_Paramanand](https://avatars.discourse-cdn.com/v4/letter/d/ee59a6/32.png) [@Deepak\_Paramanand](https://discuss.elastic.co/u/Deepak_Paramanand)\
**Post date:** [July 1, 2015, 6:10pm UTC](https://discuss.elastic.co/t/example-on-field-collapsing-not-working-for-me/24743/3 "2015-07-01T18:10:24Z")

</div>

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"  
}  
}

---

<div class="post-metadata">

**Author:** ![dantuff](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dantuff](https://discuss.elastic.co/u/dantuff)\
**Post date:** [July 1, 2015, 6:40pm UTC](https://discuss.elastic.co/t/example-on-field-collapsing-not-working-for-me/24743/4 "2015-07-01T18:40:32Z")

</div>

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
                    }
                }
            ]
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![Deepak\_Paramanand](https://avatars.discourse-cdn.com/v4/letter/d/ee59a6/32.png) [@Deepak\_Paramanand](https://discuss.elastic.co/u/Deepak_Paramanand)\
**Post date:** [July 1, 2015, 6:49pm UTC](https://discuss.elastic.co/t/example-on-field-collapsing-not-working-for-me/24743/5 "2015-07-01T18:49:28Z")

</div>

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..

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:04am UTC](https://discuss.elastic.co/t/example-on-field-collapsing-not-working-for-me/24743/6 "2017-07-06T00:04:07Z")

</div>


