# Sort by preferred nested object

**URL:** <https://discuss.elastic.co/t/sort-by-preferred-nested-object/125134>\
**Category:** Elasticsearch\
**Created:** [March 22, 2018, 8:33am UTC](https://discuss.elastic.co/t/sort-by-preferred-nested-object/125134 "2018-03-22T08:33:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dstrop](https://avatars.discourse-cdn.com/v4/letter/d/b782af/32.png) [@dstrop](https://discuss.elastic.co/u/dstrop)\
**Post date:** [March 22, 2018, 8:33am UTC](https://discuss.elastic.co/t/sort-by-preferred-nested-object/125134/1 "2018-03-22T08:33:52Z")

</div>

running docker image [docker.elastic.co/elasticsearch/elasticsearch:6.2.3](http://docker.elastic.co/elasticsearch/elasticsearch:6.2.3)

queries bellow do not return same result  
I am trying to sort by nested object, and if main user matches my nested filter, sort by it.  
It works as expected if i leave must empty, but any condition in must results in should being ignored

> **query with must match all**
>
> ```
> POST my_index/_search
> {
> "sort": [
> {
> "user.height": {
> "mode": "max",
> "order": "desc",
> "nested": {
> "path": "user",
> "filter": {
> "bool": {
> "minimum_should_match": 0,
> "must": [
> {
> "match_all": {}
> }
> ],
> "should": [
> {
> "term": {
> "user.main": true
> }
> }
> ]
> }
> }
> }
> }
> }
> ]
> }
> 
> ```

> **response**
>
> ```
> {
> "took": 10,
> "timed_out": false,
> "_shards": {
> "total": 5,
> "successful": 5,
> "skipped": 0,
> "failed": 0
> },
> "hits": {
> "total": 2,
> "max_score": null,
> "hits": [
> {
> "_index": "my_index",
> "_type": "_doc",
> "_id": "1",
> "_score": null,
> "_source": {
> "group": "fans",
> "user": [
> {
> "name": "John",
> "height": 200,
> "main": false
> },
> {
> "name": "Alice",
> "height": 150,
> "main": true
> }
> ]
> },
> "sort": [
> 200
> ]
> },
> {
> "_index": "my_index",
> "_type": "_doc",
> "_id": "2",
> "_score": null,
> "_source": {
> "group": "groupies",
> "user": [
> {
> "name": "Malcolm",
> "height": 175,
> "main": true
> }
> ]
> },
> "sort": [
> 175
> ]
> }
> ]
> }
> }
> 
> ```

> **query with empty must**
>
> ```
> POST my_index/_search
> {
> "sort": [
> {
> "user.height": {
> "mode": "max",
> "order": "desc",
> "nested": {
> "path": "user",
> "filter": {
> "bool": {
> "minimum_should_match": 0,
> "must": [
> ],
> "should": [
> {
> "term": {
> "user.main": true
> }
> }
> ]
> }
> }
> }
> }
> }
> ]
> }
> 
> ```

> **response**
>
> ```
> {
> "took": 15,
> "timed_out": false,
> "_shards": {
> "total": 5,
> "successful": 5,
> "skipped": 0,
> "failed": 0
> },
> "hits": {
> "total": 2,
> "max_score": null,
> "hits": [
> {
> "_index": "my_index",
> "_type": "_doc",
> "_id": "2",
> "_score": null,
> "_source": {
> "group": "groupies",
> "user": [
> {
> "name": "Malcolm",
> "height": 175,
> "main": true
> }
> ]
> },
> "sort": [
> 175
> ]
> },
> {
> "_index": "my_index",
> "_type": "_doc",
> "_id": "1",
> "_score": null,
> "_source": {
> "group": "fans",
> "user": [
> {
> "name": "John",
> "height": 200,
> "main": false
> },
> {
> "name": "Alice",
> "height": 150,
> "main": true
> }
> ]
> },
> "sort": [
> 150
> ]
> }
> ]
> }
> }
> 
> ```

> **how to reproduce**
>
> ```
> PUT my_index
> {
> "mappings": {
> "_doc": {
> "properties": {
> "user": {
> "type": "nested" 
> }
> }
> }
> }
> }
> 
> PUT my_index/_doc/1
> {
> "group": "fans",
> "user": [
> {
> "name": "John",
> "height": 150,
> "main": true
> },
> {
> "name": "Alice",
> "height": 200,
> "main": false
> }
> ]
> }
> 
> PUT my_index/_doc/2
> {
> "group": "groupies",
> "user": [
> {
> "name": "Malcolm",
> "height": 175,
> "main": true
> }
> ]
> }
> 
> ```

---

<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:** [April 19, 2018, 8:34am UTC](https://discuss.elastic.co/t/sort-by-preferred-nested-object/125134/2 "2018-04-19T08:34:02Z")

</div>

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