# DSL query, fields not coming in same order in result

**URL:** <https://discuss.elastic.co/t/dsl-query-fields-not-coming-in-same-order-in-result/7907>\
**Category:** Elasticsearch\
**Created:** [May 30, 2012, 7:40am UTC](https://discuss.elastic.co/t/dsl-query-fields-not-coming-in-same-order-in-result/7907 "2012-05-30T07:40:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sumitimes](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@sumitimes](https://discuss.elastic.co/u/sumitimes)\
**Post date:** [May 30, 2012, 7:40am UTC](https://discuss.elastic.co/t/dsl-query-fields-not-coming-in-same-order-in-result/7907/1 "2012-05-30T07:40:13Z")

</div>

I have a query like this the output expected is in order of fields specified.  
name (String)  
price (Double)  
year (Integer)

but i am getting  
price  
year  
name

so it is automatically getting sorted to put string last, is there anyway to enforce that i recieve result in same order as whatever is specified in fields

"fields": [  
"name",  
"price",  
"year"  
]

Query:  
{  
"fields": [  
"name",  
"price",  
"year"  
],  
"from": 10,  
"size": 5,  
"sort": [],  
"filter": {  
"query": {  
"query\_string": {  
"query": "/products(name:name)"  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![sumitimes](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@sumitimes](https://discuss.elastic.co/u/sumitimes)\
**Post date:** [May 30, 2012, 8:19am UTC](https://discuss.elastic.co/t/dsl-query-fields-not-coming-in-same-order-in-result/7907/2 "2012-05-30T08:19:47Z")

</div>

can anyone suggest how to solve it, really appreciate any help

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [June 3, 2012, 10:07am UTC](https://discuss.elastic.co/t/dsl-query-fields-not-coming-in-same-order-in-result/7907/3 "2012-06-03T10:07:52Z")

</div>

You mean sorting by it? When you specify fields, those are the fields that  
will be returned as part of each hit, not sorting. There is a whole sorting  
section that controls sorting.

On Wed, May 30, 2012 at 9:40 AM, sumitimes [sumitimes@gmail.com](mailto:sumitimes@gmail.com) wrote:

> I have a query like this the output expected is in order of fields  
> specified.  
> name (String)  
> price (Double)  
> year (Integer)
> 
> but i am getting  
> price  
> year  
> name
> 
> so it is automatically getting sorted to put string last, is there anyway  
> to  
> enforce that i recieve result in same order as whatever is specified in  
> fields
> 
> "fields": [  
> "name",  
> "price",  
> "year"  
> ]
> 
> Query:  
> {  
> "fields": [  
> "name",  
> "price",  
> "year"  
> ],  
> "from": 10,  
> "size": 5,  
> "sort": ,  
> "filter": {  
> "query": {  
> "query\_string": {  
> "query": "/products(name:name)"  
> }  
> }  
> }  
> }
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/DSL-query-fields-not-coming-in-same-order-in-result-tp4018535.html](http://elasticsearch-users.115913.n3.nabble.com/DSL-query-fields-not-coming-in-same-order-in-result-tp4018535.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![sumitimes](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@sumitimes](https://discuss.elastic.co/u/sumitimes)\
**Post date:** [June 4, 2012, 1:40am UTC](https://discuss.elastic.co/t/dsl-query-fields-not-coming-in-same-order-in-result/7907/4 "2012-06-04T01:40:38Z")

</div>

Hi kimchy,

Thanks for the response, i am not talking about sorting, sorting decides  
order of each record, here i am concerned about order of fields in each  
record  
say for example

Sorting will do something like this

record 1

Columns fName Lname age

Record 1 sam tom 20

Record 2 vim jones 25

sorted by Lname will return

Record 1 vim jones 25

Record 2 sam tom 20

for me this is will be worong  
\*  
Columns age fName Lname \*  
I expect  
\*Columns fName Lname age \*only, not worried by order of records but order  
of columns

sorted by fname will return again records which are arranged

On Sun, Jun 3, 2012 at 3:38 PM, kimchy [via Elasticsearch Users] \<  
[ml-node+s115913n4018741h52@n3.nabble.com](mailto:ml-node+s115913n4018741h52@n3.nabble.com)\> wrote:

> You mean sorting by it? When you specify fields, those are the fields that  
> will be returned as part of each hit, not sorting. There is a whole sorting  
> section that controls sorting.
> 
> On Wed, May 30, 2012 at 9:40 AM, sumitimes \<[hidden email][http://user/SendEmail.jtp?type=node&node=4018741&i=0](http://user/SendEmail.jtp?type=node&node=4018741&i=0)
> 
> > wrote:
> 
> > I have a query like this the output expected is in order of fields  
> > specified.  
> > name (String)  
> > price (Double)  
> > year (Integer)
> > 
> > but i am getting  
> > price  
> > year  
> > name
> > 
> > so it is automatically getting sorted to put string last, is there anyway  
> > to  
> > enforce that i recieve result in same order as whatever is specified in  
> > fields
> > 
> > "fields": [  
> > "name",  
> > "price",  
> > "year"  
> > ]
> > 
> > Query:  
> > {  
> > "fields": [  
> > "name",  
> > "price",  
> > "year"  
> > ],  
> > "from": 10,  
> > "size": 5,  
> > "sort": ,  
> > "filter": {  
> > "query": {  
> > "query\_string": {  
> > "query": "/products(name:name)"  
> > }  
> > }  
> > }  
> > }
> > 
> > --  
> > View this message in context:  
> > [http://elasticsearch-users.115913.n3.nabble.com/DSL-query-fields-not-coming-in-same-order-in-result-tp4018535.html](http://elasticsearch-users.115913.n3.nabble.com/DSL-query-fields-not-coming-in-same-order-in-result-tp4018535.html)  
> > Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> * * *
> 
> If you reply to this email, your message will be added to the discussion  
> below:
> 
> [http://elasticsearch-users.115913.n3.nabble.com/DSL-query-fields-not-coming-in-same-order-in-result-tp4018535p4018741.html](http://elasticsearch-users.115913.n3.nabble.com/DSL-query-fields-not-coming-in-same-order-in-result-tp4018535p4018741.html)  
> To unsubscribe from DSL query, fields not coming in same order in result, click  
> here[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe\_by\_code&node=4018535&code=c3VtaXRpbWVzQGdtYWlsLmNvbXw0MDE4NTM1fDcwMzYwNDU3OQ==](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4018535&code=c3VtaXRpbWVzQGdtYWlsLmNvbXw0MDE4NTM1fDcwMzYwNDU3OQ==)  
> .  
> NAML[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro\_viewer&id=instant\_html!nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify\_subscribers!nabble%3Aemail.naml-instant\_emails!nabble%3Aemail.naml-send\_instant\_email!nabble%3Aemail.naml](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml)

---

<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:** [June 8, 2012, 2:49pm UTC](https://discuss.elastic.co/t/dsl-query-fields-not-coming-in-same-order-in-result/7907/5 "2012-06-08T14:49:58Z")

</div>

> Thanks for the response, i am not talking about sorting, sorting  
> decides order of each record, here i am concerned about order of  
> fields in each record

JSON objects are by definition, an unordered collection of key:value  
pairs. Even if ES were to return the "fields" object in a particular  
order, as soon as you parse JSON the order may be changed.

clint

---

<div class="post-metadata">

**Author:** ![sumitimes](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@sumitimes](https://discuss.elastic.co/u/sumitimes)\
**Post date:** [June 10, 2012, 2:07pm UTC](https://discuss.elastic.co/t/dsl-query-fields-not-coming-in-same-order-in-result/7907/6 "2012-06-10T14:07:56Z")

</div>

Thanks for you response, we are using Elasticsearch as a intermediate  
data, we are doing very complex compiling and de compiling of data , and  
our front end expects order of fields to be maintained, so i wanted as much  
functionality i can get directly, anyways we ended up writing a json parser  
which will maintain field order.

Thank you all for all your suggestions !!!

On Fri, Jun 8, 2012 at 8:20 PM, Clinton Gormley-2 [via Elasticsearch Users]  
[ml-node+s115913n4018977h68@n3.nabble.com](mailto:ml-node+s115913n4018977h68@n3.nabble.com) wrote:

> > Thanks for the response, i am not talking about sorting, sorting  
> > decides order of each record, here i am concerned about order of  
> > fields in each record
> 
> JSON objects are by definition, an unordered collection of key:value  
> pairs. Even if ES were to return the "fields" object in a particular  
> order, as soon as you parse JSON the order may be changed.
> 
> clint
> 
> * * *
> 
> If you reply to this email, your message will be added to the discussion  
> below:
> 
> [http://elasticsearch-users.115913.n3.nabble.com/DSL-query-fields-not-coming-in-same-order-in-result-tp4018535p4018977.html](http://elasticsearch-users.115913.n3.nabble.com/DSL-query-fields-not-coming-in-same-order-in-result-tp4018535p4018977.html)  
> To unsubscribe from DSL query, fields not coming in same order in result, click  
> here[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe\_by\_code&node=4018535&code=c3VtaXRpbWVzQGdtYWlsLmNvbXw0MDE4NTM1fDcwMzYwNDU3OQ==](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4018535&code=c3VtaXRpbWVzQGdtYWlsLmNvbXw0MDE4NTM1fDcwMzYwNDU3OQ==)  
> .  
> NAML[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro\_viewer&id=instant\_html!nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify\_subscribers!nabble%3Aemail.naml-instant\_emails!nabble%3Aemail.naml-send\_instant\_email!nabble%3Aemail.naml](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml)

---

<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, 3:25am UTC](https://discuss.elastic.co/t/dsl-query-fields-not-coming-in-same-order-in-result/7907/7 "2017-07-06T03:25:02Z")

</div>


