# Sort script using object fields

**URL:** <https://discuss.elastic.co/t/sort-script-using-object-fields/22357>\
**Category:** Elasticsearch\
**Created:** [February 24, 2015, 2:52pm UTC](https://discuss.elastic.co/t/sort-script-using-object-fields/22357 "2015-02-24T14:52:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christophe\_Rosko](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@Christophe\_Rosko](https://discuss.elastic.co/u/Christophe_Rosko)\
**Post date:** [February 24, 2015, 2:52pm UTC](https://discuss.elastic.co/t/sort-script-using-object-fields/22357/1 "2015-02-24T14:52:49Z")

</div>

Hi !

I have in my mapping a field called "topics" with type "object". Each doc  
have one or many fields "topic.t1", "topic.t8" etc, whose value are 0 or 1.  
But all docs don't have the same subfields.  
Now i'd like to use a script to sort in this kind of way :  
"(doc['topic.t1'].value \> 0 || doc['topic.t4'].value \> 0 ||  
doc['topic.t7'].value \> 0) ? 1 : 0" DESC

But i saw in the documentation that "the doc notation only allows for  
simple valued fields".  
So is there another way to do that ?

Thanks in advance for your help.  
I hope I clearly exposed my problem, as I'm new to ElasticSearch.

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/552491b2-195e-4708-9b3c-1cb6f57d0faa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/552491b2-195e-4708-9b3c-1cb6f57d0faa%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Christophe\_Rosko](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@Christophe\_Rosko](https://discuss.elastic.co/u/Christophe_Rosko)\
**Post date:** [February 26, 2015, 9:58am UTC](https://discuss.elastic.co/t/sort-script-using-object-fields/22357/2 "2015-02-26T09:58:33Z")

</div>

Up !

Le mardi 24 février 2015 15:52:49 UTC+1, Christophe Rosko a écrit :

> Hi !
> 
> I have in my mapping a field called "topics" with type "object". Each doc  
> have one or many fields "topic.t1", "topic.t8" etc, whose value are 0 or 1.  
> But all docs don't have the same subfields.  
> Now i'd like to use a script to sort in this kind of way :  
> "(doc['topic.t1'].value \> 0 || doc['topic.t4'].value \> 0 ||  
> doc['topic.t7'].value \> 0) ? 1 : 0" DESC
> 
> But i saw in the documentation that "the doc notation only allows for  
> simple valued fields".  
> So is there another way to do that ?
> 
> Thanks in advance for your help.  
> I hope I clearly exposed my problem, as I'm new to Elasticsearch.

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5f432e67-ddd9-4169-8606-08381adb3e0e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5f432e67-ddd9-4169-8606-08381adb3e0e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![masaru](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@masaru](https://discuss.elastic.co/u/masaru)\
**Post date:** [February 27, 2015, 7:45am UTC](https://discuss.elastic.co/t/sort-script-using-object-fields/22357/3 "2015-02-27T07:45:09Z")

</div>

This query should work:

{  
"query": {  
"function\_score": {  
"functions": [  
{  
"script\_score": {  
"script": "doc['topic.t1'].value \> 0 || doc['topic.t4'].value \> 0 || doc['topic.t7'].value \> 0 ? 1 : 0"  
}  
}  
]  
}  
}  
}

Masaru

On February 26, 2015 at 18:58:38, Christophe Rosko ([christophe@digiteka.com](mailto:christophe@digiteka.com)) wrote:

> Up !
> 
> Le mardi 24 février 2015 15:52:49 UTC+1, Christophe Rosko a écrit :
> 
> > Hi !
> > 
> > I have in my mapping a field called "topics" with type "object". Each doc  
> > have one or many fields "topic.t1", "topic.t8" etc, whose value are 0 or 1.  
> > But all docs don't have the same subfields.  
> > Now i'd like to use a script to sort in this kind of way :  
> > "(doc['topic.t1'].value \> 0 || doc['topic.t4'].value \> 0 ||  
> > doc['topic.t7'].value \> 0) ? 1 : 0" DESC
> > 
> > But i saw in the documentation that "the doc notation only allows for  
> > simple valued fields".  
> > So is there another way to do that ?
> > 
> > Thanks in advance for your help.  
> > I hope I clearly exposed my problem, as I'm new to Elasticsearch.
> 
> --  
> 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).  
> To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5f432e67-ddd9-4169-8606-08381adb3e0e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5f432e67-ddd9-4169-8606-08381adb3e0e%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.54f020c1.2ae8944a.129%40citra.local](https://groups.google.com/d/msgid/elasticsearch/etPan.54f020c1.2ae8944a.129%40citra.local).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:29am UTC](https://discuss.elastic.co/t/sort-script-using-object-fields/22357/4 "2017-07-06T00:29:37Z")

</div>

Closed on July 6, 2017, 12:29am UTC


