# Problem with sorting results of query with multiple attributes

**URL:** <https://discuss.elastic.co/t/problem-with-sorting-results-of-query-with-multiple-attributes/14011>\
**Category:** Elasticsearch\
**Created:** [October 18, 2013, 10:37am UTC](https://discuss.elastic.co/t/problem-with-sorting-results-of-query-with-multiple-attributes/14011 "2013-10-18T10:37:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Szymon\_Baranowski](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Szymon\_Baranowski](https://discuss.elastic.co/u/Szymon_Baranowski)\
**Post date:** [October 18, 2013, 10:37am UTC](https://discuss.elastic.co/t/problem-with-sorting-results-of-query-with-multiple-attributes/14011/1 "2013-10-18T10:37:11Z")

</div>

Hey,  
I'm just starting with ElasticSearch now and tried simple index schema :

"mappings" : {  
"clip" : {  
"properties" : {  
"subject" : {  
"properties" : {  
subjectId: {  
index: "analyzed",  
type: "integer"  
},  
insertDate: {  
index: "analyzed",  
type: "integer"  
}  
}  
}  
}  
}  
}

Then I inserted few clips, some of them with multiple atribute such as :

- subject: [
  - {
    - subjectId: 4
    - insertDate: 5  
}

  - {
    - subjectId: 3
    - insertDate: 6  
}  
]

Problem is, when I try to search for clips that got subjectId = 3 and sort  
result by insertDate what happens is ES would find  
{subjectId=3,insertDate=6} but would sort results using FIRST attribute  
(which is (subjectId=4,insertDate=5). So it finally finds data that I want  
to find, but sorts them not with the attribute that MATCH to my query but  
the first attribute. Is there any chance to fix this one?

Thats my query by the way

{"fields":["\_parent","\_source"],"query":{"bool":{"must":[{"range":{  
"subjectId":{"gte":3,"lte":3}}}],"must\_not":[],"should":[]}},"from":0,"size"  
:50,"sort":[{"insertDate":{"reverse":false}}],"facets":{},"version":true}

Thanks for checking out

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [October 18, 2013, 12:34pm UTC](https://discuss.elastic.co/t/problem-with-sorting-results-of-query-with-multiple-attributes/14011/2 "2013-10-18T12:34:45Z")

</div>

What you're looking for are nested objects:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Also see:  
\*

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

\*

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

clint

On 18 October 2013 12:37, Szymon Baranowski  
[szymon.baranowski.pl@gmail.com](mailto:szymon.baranowski.pl@gmail.com)wrote:

> Hey,  
> I'm just starting with Elasticsearch now and tried simple index schema :
> 
> "mappings" : {  
> "clip" : {  
> "properties" : {  
> "subject" : {  
> "properties" : {  
> subjectId: {  
> index: "analyzed",  
> type: "integer"  
> },  
> insertDate: {  
> index: "analyzed",  
> type: "integer"  
> }  
> }  
> }  
> }  
> }  
> }
> 
> Then I inserted few clips, some of them with multiple atribute such as :
> 
> - subject: [
> - {
> - subjectId: 4
> - insertDate: 5  
> }
> 
> - {
> - subjectId: 3
> - insertDate: 6  
> }  
> ]
> 
> Problem is, when I try to search for clips that got subjectId = 3 and sort  
> result by insertDate what happens is ES would find  
> {subjectId=3,insertDate=6} but would sort results using FIRST attribute  
> (which is (subjectId=4,insertDate=5). So it finally finds data that I want  
> to find, but sorts them not with the attribute that MATCH to my query but  
> the first attribute. Is there any chance to fix this one?
> 
> Thats my query by the way
> 
> {"fields":["\_parent","\_source"],"query":{"bool":{"must":[{"range":{  
> "subjectId":{"gte":3,"lte":3}}}],"must\_not":,"should":}},"from":0,  
> "size":50,"sort":[{"insertDate":{"reverse":false}}],"facets":{},"version":  
> true}
> 
> Thanks for checking out
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Szymon\_Baranowski](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Szymon\_Baranowski](https://discuss.elastic.co/u/Szymon_Baranowski)\
**Post date:** [October 21, 2013, 6:38am UTC](https://discuss.elastic.co/t/problem-with-sorting-results-of-query-with-multiple-attributes/14011/3 "2013-10-21T06:38:11Z")

</div>

Thanks clint

W dniu piątek, 18 października 2013 12:37:11 UTC+2 użytkownik Szymon  
Baranowski napisał:

> Hey,  
> I'm just starting with Elasticsearch now and tried simple index schema :
> 
> "mappings" : {  
> "clip" : {  
> "properties" : {  
> "subject" : {  
> "properties" : {  
> subjectId: {  
> index: "analyzed",  
> type: "integer"  
> },  
> insertDate: {  
> index: "analyzed",  
> type: "integer"  
> }  
> }  
> }  
> }  
> }  
> }
> 
> Then I inserted few clips, some of them with multiple atribute such as :
> 
> - subject: [
> - {
> - subjectId: 4
> - insertDate: 5  
> }
> 
> - {
> - subjectId: 3
> - insertDate: 6  
> }  
> ]
> 
> Problem is, when I try to search for clips that got subjectId = 3 and sort  
> result by insertDate what happens is ES would find  
> {subjectId=3,insertDate=6} but would sort results using FIRST attribute  
> (which is (subjectId=4,insertDate=5). So it finally finds data that I want  
> to find, but sorts them not with the attribute that MATCH to my query but  
> the first attribute. Is there any chance to fix this one?
> 
> Thats my query by the way
> 
> {"fields":["\_parent","\_source"],"query":{"bool":{"must":[{"range":{  
> "subjectId":{"gte":3,"lte":3}}}],"must\_not":,"should":}},"from":0,  
> "size":50,"sort":[{"insertDate":{"reverse":false}}],"facets":{},"version":  
> true}
> 
> Thanks for checking out

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:11am UTC](https://discuss.elastic.co/t/problem-with-sorting-results-of-query-with-multiple-attributes/14011/4 "2017-07-06T02:11:29Z")

</div>


