# Sorting by values in children objects using a custom score?

**URL:** <https://discuss.elastic.co/t/sorting-by-values-in-children-objects-using-a-custom-score/11060>\
**Category:** Elasticsearch\
**Created:** [March 7, 2013, 5:16pm UTC](https://discuss.elastic.co/t/sorting-by-values-in-children-objects-using-a-custom-score/11060 "2013-03-07T17:16:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![grantgochnauer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/grantgochnauer/32/2300_2.png) [@grantgochnauer](https://discuss.elastic.co/u/grantgochnauer)\
**Post date:** [March 7, 2013, 5:16pm UTC](https://discuss.elastic.co/t/sorting-by-values-in-children-objects-using-a-custom-score/11060/1 "2013-03-07T17:16:14Z")

</div>

I'm trying to sort parent objects by values in the child object by  
leveraging the custom score query. I want to sort a title by either asc or  
desc. I've attempted to write a simple query to get the custom score  
working but for some reason it's always returning a score of "1".

Here is the query I'm attempting:

{  
"from" : 0,  
"size" : 20,  
"query" : {  
"has\_child" : {  
"query" : {  
"custom\_score" : {  
"query" : {  
"bool" : {  
"must" : [ {  
"range" : {  
"createDate" : {  
"from" : 0,  
"to" : null,  
"include\_lower" : false,  
"include\_upper" : true  
}  
}  
}, {  
"terms" : {  
"category" : ["instrument"]  
}  
}]  
}  
},  
"script" : "-doc['sortTitle'].value"  
}  
},  
"child\_type" : "com.MyChild"  
}  
},  
"sort" : [ {  
"\_score" : {  
"order" : "asc"  
}  
} ]  
}

Anyone have any ideas on 1) why my score is always 1 which I assume because  
my script is totally wrong 😉 and 2) whether there is a more appropriate  
way to sort parent objects by values in their children?

I'm using ES 0.20.5

thanks!

--  
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:** [March 8, 2013, 12:31pm UTC](https://discuss.elastic.co/t/sorting-by-values-in-children-objects-using-a-custom-score/11060/2 "2013-03-08T12:31:49Z")

</div>

Hi Grant

Presumably your sortTitle field is a string? The script needs to return a  
number - you are returning a "negative string".

Clint

On Thursday, 7 March 2013 18:16:14 UTC+1, Grant Gochnauer wrote:

> I'm trying to sort parent objects by values in the child object by  
> leveraging the custom score query. I want to sort a title by either asc or  
> desc. I've attempted to write a simple query to get the custom score  
> working but for some reason it's always returning a score of "1".
> 
> Here is the query I'm attempting:
> 
> {  
> "from" : 0,  
> "size" : 20,  
> "query" : {  
> "has\_child" : {  
> "query" : {  
> "custom\_score" : {  
> "query" : {  
> "bool" : {  
> "must" : [ {  
> "range" : {  
> "createDate" : {  
> "from" : 0,  
> "to" : null,  
> "include\_lower" : false,  
> "include\_upper" : true  
> }  
> }  
> }, {  
> "terms" : {  
> "category" : ["instrument"]  
> }  
> }]  
> }  
> },  
> "script" : "-doc['sortTitle'].value"  
> }  
> },  
> "child\_type" : "com.MyChild"  
> }  
> },  
> "sort" : [ {  
> "\_score" : {  
> "order" : "asc"  
> }  
> } ]  
> }
> 
> Anyone have any ideas on 1) why my score is always 1 which I assume  
> because my script is totally wrong 😉 and 2) whether there is a more  
> appropriate way to sort parent objects by values in their children?
> 
> I'm using ES 0.20.5
> 
> thanks!

--  
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:** ![grantgochnauer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/grantgochnauer/32/2300_2.png) [@grantgochnauer](https://discuss.elastic.co/u/grantgochnauer)\
**Post date:** [March 8, 2013, 12:47pm UTC](https://discuss.elastic.co/t/sorting-by-values-in-children-objects-using-a-custom-score/11060/3 "2013-03-08T12:47:17Z")

</div>

Hi Clint,

Thanks - I assumed as much but am trying to figure out how I can calculate  
a numerical value that I could leverage to sort a list by alpha order via  
script. I didn't see any functions in ES that may provide this so it may be  
that it's not possible. Any thoughts?

Thanks!

On Fri, Mar 8, 2013 at 6:31 AM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:

> Hi Grant
> 
> Presumably your sortTitle field is a string? The script needs to return a  
> number - you are returning a "negative string".
> 
> Clint
> 
> On Thursday, 7 March 2013 18:16:14 UTC+1, Grant Gochnauer wrote:
> 
> > I'm trying to sort parent objects by values in the child object by  
> > leveraging the custom score query. I want to sort a title by either asc or  
> > desc. I've attempted to write a simple query to get the custom score  
> > working but for some reason it's always returning a score of "1".
> > 
> > Here is the query I'm attempting:
> > 
> > {  
> > "from" : 0,  
> > "size" : 20,  
> > "query" : {  
> > "has\_child" : {  
> > "query" : {  
> > "custom\_score" : {  
> > "query" : {  
> > "bool" : {  
> > "must" : [ {  
> > "range" : {  
> > "createDate" : {  
> > "from" : 0,  
> > "to" : null,  
> > "include\_lower" : false,  
> > "include\_upper" : true  
> > }  
> > }  
> > }, {  
> > "terms" : {  
> > "category" : ["instrument"]  
> > }  
> > }]  
> > }  
> > },  
> > "script" : "-doc['sortTitle'].value"  
> > }  
> > },  
> > "child\_type" : "com.MyChild"  
> > }  
> > },  
> > "sort" : [ {  
> > "\_score" : {  
> > "order" : "asc"  
> > }  
> > } ]  
> > }
> > 
> > Anyone have any ideas on 1) why my score is always 1 which I assume  
> > because my script is totally wrong 😉 and 2) whether there is a more  
> > appropriate way to sort parent objects by values in their children?
> > 
> > I'm using ES 0.20.5
> > 
> > thanks!
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/KMmEOuc9Ce0/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/KMmEOuc9Ce0/unsubscribe?hl=en-US)  
> > .  
> > To unsubscribe from this group and all its topics, 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).

--  
Grant Gochnauer | Vodori Inc | [grant.gochnauer@vodori.com](mailto:grant.gochnauer@vodori.com) | (w)  
312.324.0124 | (c) 312.504.5004

--  
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:47am UTC](https://discuss.elastic.co/t/sorting-by-values-in-children-objects-using-a-custom-score/11060/4 "2017-07-06T02:47:35Z")

</div>


