# Global direction: Parent-Child: Grouping and Sorting

**URL:** <https://discuss.elastic.co/t/global-direction-parent-child-grouping-and-sorting/20816>\
**Category:** Elasticsearch\
**Created:** [November 18, 2014, 3:41pm UTC](https://discuss.elastic.co/t/global-direction-parent-child-grouping-and-sorting/20816 "2014-11-18T15:41:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wolfgang\_Werner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wolfgang_werner/32/1089_2.png) [@Wolfgang\_Werner](https://discuss.elastic.co/u/Wolfgang_Werner)\
**Post date:** [November 18, 2014, 3:41pm UTC](https://discuss.elastic.co/t/global-direction-parent-child-grouping-and-sorting/20816/1 "2014-11-18T15:41:13Z")

</div>

Hi all,

I have a question regarding grouping and sorting - I can give complete gist  
examples, but would like to check first the overall perspective on this -  
am I on the right track, do I miss something, what will be the future  
direction on this.

My Usecase: I have a lot of press articles, some of them are very similar  
in content. I have to provide a search interface, that groups these  
duplets, while giving the user a lot of search possibilities on article  
content / meta data.

I decided to create a parent child mapping (parent: group, child: article)  
for this for the following reasons:

- the grouping will change over time: new articles are added constantly,  
and I do not want to reindex a lot of stuff
- articles have their own visibility restrictions, but should be indexed up  
front
- I strive for simple pagination and do not want to collect groups without  
knowing how many child documents I have to fetch

My current search strategy has two stages  
(1) search with a has\_child query for groups  
(2) resolve all children for the groups with a has\_parent query

The problem is, that I _need to sort the parents_/groups (result of the  
first has\_child query) _by values of the children_ (articles). As I  
understand, this is currently not possible.

The only solution around is to wrap the has\_child query with a function  
score and use that score for the sorting. Something like (bold the relevant  
parts):

curl -XGET '[http://localhost:9200/index/](http://localhost:9200/index/)_group_/\_search?pretty=1' -d '{  
"query" : {  
"_has\_child_" : {  
"query" : {  
"_function\_score_" : {  
"query" : {  
}  
},  
"functions" : [ {  
"_script\_score_" : {  
"script" : "doc['_article_.publicationNameSort'].value"  
}  
} ],  
"_boost\_mode_" : "replace"  
}  
},  
"child\_type" : "_article_",  
"_score\_type_" : "max"  
}  
},  
"sort" : [ {  
"_\_score_" : { }  
} ]  
}'

The problem in my use case is, that the sort often needs _more than one  
field_ or even _several string values_ to sort on. (com)pressing these to a  
single double is not always possible.

_My questions_

_(A) will there be sorting support for has\_child queries in the (near)  
future_

There are different comments on this in the community. Is this easy (as  
supported by lucene) or a very high hanging fruit?

\*(B) is there an other way to achieve the grouping \*

The grouping could be solved by doing by hand - getting child values with a  
simple query, scanning results, gathering some type of 'parent/group' field  
and returning the result when enough groups have been resolved. A nightmare  
regarding pagination. This looks a lot look the problems Elasticsearch  
already has solved in parent-child queries / top-children query.

All other comments and suggestions are very appreciated.

Best regards, Wolfgang

--  
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/45239716-7962-4272-9d9e-1a3b811460b4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/45239716-7962-4272-9d9e-1a3b811460b4%40googlegroups.com).  
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:49am UTC](https://discuss.elastic.co/t/global-direction-parent-child-grouping-and-sorting/20816/2 "2017-07-06T00:49:12Z")

</div>

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


