# Sort not working with complex query

**URL:** <https://discuss.elastic.co/t/sort-not-working-with-complex-query/135452>\
**Category:** Elasticsearch\
**Created:** [June 12, 2018, 3:37am UTC](https://discuss.elastic.co/t/sort-not-working-with-complex-query/135452 "2018-06-12T03:37:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joropito](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joropito/32/32138_2.png) [@joropito](https://discuss.elastic.co/u/joropito)\
**Post date:** [June 12, 2018, 3:37am UTC](https://discuss.elastic.co/t/sort-not-working-with-complex-query/135452/1 "2018-06-12T03:37:21Z")

</div>

I'm doing a complex query and sort returns the same results. Can anybody help me?

The query works fine returning the correct values but with or without sort option the results are the same.

Here's the search query

{  
"limit": 2000,  
"sort":["created": { "order": "desc" }],  
"filter": {  
"bool": {  
"must": [{  
"geo\_polygon": {  
"location.geopoint.coordinates": {  
"points": [  
[-90, -90],  
[-90, 90],  
[90, 90],  
[90, -90]  
]  
}  
}  
}, {  
"term": {  
"status": "active"  
}  
}, {  
"bool": {  
"should": [{  
"term": {  
"basic.pSubType": "kljhfgdfgvf"  
}  
}, {  
"term": {  
"basic.pSubType": "asdqwesdf"  
}  
}]  
}  
}, {  
"range": {  
"property.bthrms": {  
"gte": 0,  
"lte": 20  
}  
}  
}, {  
"bool": {  
"should": [{  
"term": {  
"property.energy": 2  
}  
}, {  
"term": {  
"property.energy": 3  
}  
}]  
}  
}, {  
"bool": {  
"should": [{  
"term": {  
"building.bFacils": "asdqwe"  
}  
}, {  
"term": {  
"building.bFacils": "asdasd"  
}  
}]  
}  
}, {  
"bool": {  
"should": [{  
"bool": {  
"must": [{  
"term": {  
"basic.pSubType": "loort"  
}  
}, {  
"range": {  
"building.height": {  
"gte": 6  
}  
}  
}, {  
"range": {  
"property.flrNr": {  
"lte": 6  
}  
}  
}]  
}  
}, {  
"bool": {  
"must": [{  
"term": {  
"basic.pSubType": "eirthertr"  
}  
}, {  
"range": {  
"building.height": {  
"gte": 2,  
"lte": 20  
}  
}  
}, {  
"range": {  
"property.flrNr": {  
"gte": -5  
}  
}  
}]  
}  
}]  
}  
}, {  
"term": {  
"basic.operation": "buy"  
}  
}, {  
"range": {  
"basic.price": {  
"gte": 466  
}  
}  
}, {  
"range": {  
"basic.priceRatio": {  
"gte": 50,  
"lte": 500000  
}  
}  
}, {  
"range": {  
"property.surface": {  
"gte": 30,  
"lte": 56000  
}  
}  
}, {  
"range": {  
"property.bdrms": {  
"lte": 50  
}  
}  
}]  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [June 12, 2018, 7:10am UTC](https://discuss.elastic.co/t/sort-not-working-with-complex-query/135452/2 "2018-06-12T07:10:47Z")

</div>

What is the datatype of the `created` field?

---

<div class="post-metadata">

**Author:** ![joropito](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joropito/32/32138_2.png) [@joropito](https://discuss.elastic.co/u/joropito)\
**Post date:** [June 12, 2018, 11:13am UTC](https://discuss.elastic.co/t/sort-not-working-with-complex-query/135452/3 "2018-06-12T11:13:26Z")

</div>

It's date type. But even if I try to sort by \_id doesn't work with this query.

If I do a simple match\_all query, sort goes well.

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [June 12, 2018, 11:21am UTC](https://discuss.elastic.co/t/sort-not-working-with-complex-query/135452/4 "2018-06-12T11:21:22Z")

</div>

Check again your sort parameter, the syntax is wrong, it must be like this :

```
  "sort": [
    {
      "created": {
        "order": "desc"
      }
    }
  ]
```

---

<div class="post-metadata">

**Author:** ![joropito](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joropito/32/32138_2.png) [@joropito](https://discuss.elastic.co/u/joropito)\
**Post date:** [June 12, 2018, 1:26pm UTC](https://discuss.elastic.co/t/sort-not-working-with-complex-query/135452/5 "2018-06-12T13:26:17Z")

</div>

I found it was my error running the search from command line with curl.

My query was ok but "limit" should be "size".

Thanks

---

<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 10, 2018, 1:27pm UTC](https://discuss.elastic.co/t/sort-not-working-with-complex-query/135452/6 "2018-07-10T13:27:15Z")

</div>

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