# Field collapse - can't sort inner hits to take latest document?

**URL:** <https://discuss.elastic.co/t/field-collapse-cant-sort-inner-hits-to-take-latest-document/117506>\
**Category:** Elasticsearch\
**Created:** [January 29, 2018, 5:54pm UTC](https://discuss.elastic.co/t/field-collapse-cant-sort-inner-hits-to-take-latest-document/117506 "2018-01-29T17:54:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Millward\_Wrig](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Thomas\_Millward\_Wrig](https://discuss.elastic.co/u/Thomas_Millward_Wrig)\
**Post date:** [January 29, 2018, 5:54pm UTC](https://discuss.elastic.co/t/field-collapse-cant-sort-inner-hits-to-take-latest-document/117506/1 "2018-01-29T17:54:15Z")

</div>

I'm trying to perform a search that returns the most recent document per unique value of field "unique\_hash". I'm using a "field collapse" aggregation, like so:

```
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "filtering_on_this": "value"
          }
        }
      ]
    }
  },
  "collapse": {
    "field": "unique_hash.keyword",
    "inner_hits": {
      "name": "latest",
      "size": 1,
      "sort": {
        "created_at": "asc"
      }
    }
  },
  "size": 30,
  "from": 30
}

```

As you can see, I am sorting the inner hits on "created\_at", using a descending sort. This field is mapped to a "date" type. However, this does not return the latest document for each value of unique hash, and appears to take the earliest instead. Swapping "asc" for "desc" has no effect on the results returned.

Any pointers would be much appreciated!

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 29, 2018, 5:59pm UTC](https://discuss.elastic.co/t/field-collapse-cant-sort-inner-hits-to-take-latest-document/117506/2 "2018-01-29T17:59:38Z")

</div>

At first glance - shouldn't `sort` be an array?

---

<div class="post-metadata">

**Author:** ![Thomas\_Millward\_Wrig](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Thomas\_Millward\_Wrig](https://discuss.elastic.co/u/Thomas_Millward_Wrig)\
**Post date:** [January 30, 2018, 11:00am UTC](https://discuss.elastic.co/t/field-collapse-cant-sort-inner-hits-to-take-latest-document/117506/3 "2018-01-30T11:00:18Z")

</div>

@Mark_Harwood Changing it to an array like:

`'sort' => [{'created_at' => 'asc'}]`

has no effect ☹

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 30, 2018, 11:34am UTC](https://discuss.elastic.co/t/field-collapse-cant-sort-inner-hits-to-take-latest-document/117506/4 "2018-01-30T11:34:41Z")

</div>

Working for me on 6.0 with this repro:

```
DELETE test
PUT test
{
  "settings": {
	"number_of_replicas": 0,
	"number_of_shards": 1
  },
  "mappings": {
	"doc":{
	  "properties": {
		"filtering_on_this":{
		  "type":"keyword"
		},
		"created_at":{
		  "type":"date",
		  "format":"yyyy-MM-dd"
		},
		"unique_hash":{
		  "type":"text",
		  "fields":{
			"keyword":{
			  "type":"keyword"
			}
		  }
		}
	  }
	}
  }
}
POST test/doc/_bulk
{"index":{}}
{"filtering_on_this":"value","unique_hash":"A","created_at":"2018-01-1"}
{"index":{}}
{"filtering_on_this":"value","unique_hash":"A","created_at":"2018-01-2"}

```

Search asc request:

```
GET test/_search
{
  "query": {
	"bool": {
	  "filter": [
		{
		  "term": {
			"filtering_on_this": "value"
		  }
		}
	  ]
	}
  },
  "collapse": {
	"field": "unique_hash.keyword",
	"inner_hits": {
	  "name": "latest",
	  "size": 1,
	  "sort": {
		"created_at": "asc"
	  }
	}
  }
}

```

Response is single doc dated `2018-01-1`  
With `desc` sort response is single doc dated `2018-01-2`

Can you provide a reproducible simplified example like the above that demonstrates the error?

---

<div class="post-metadata">

**Author:** ![Thomas\_Millward\_Wrig](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Thomas\_Millward\_Wrig](https://discuss.elastic.co/u/Thomas_Millward_Wrig)\
**Post date:** [January 30, 2018, 11:52am UTC](https://discuss.elastic.co/t/field-collapse-cant-sort-inner-hits-to-take-latest-document/117506/5 "2018-01-30T11:52:42Z")

</div>

Hey @Mark_Harwood. Unfortunately I cannot - I have found the source of my error, and it was not Elasticsearch. I have some minimal ORM that grabbed the wrong part of the response, when I inspected the raw response it became clear that the inner\_hits part of the result contained the expected document.

Thanks for your help, sorry for wasting your time!

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 30, 2018, 11:55am UTC](https://discuss.elastic.co/t/field-collapse-cant-sort-inner-hits-to-take-latest-document/117506/6 "2018-01-30T11:55:22Z")

</div>

One of those bugs just waiting for a bigger audience 🙂

---

<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:** [February 27, 2018, 11:55am UTC](https://discuss.elastic.co/t/field-collapse-cant-sort-inner-hits-to-take-latest-document/117506/7 "2018-02-27T11:55:32Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
