# Parent/child sort

**URL:** <https://discuss.elastic.co/t/parent-child-sort/13466>\
**Category:** Elasticsearch\
**Created:** [September 4, 2013, 9:06pm UTC](https://discuss.elastic.co/t/parent-child-sort/13466 "2013-09-04T21:06:05Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_Seleznev](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Max\_Seleznev](https://discuss.elastic.co/u/Max_Seleznev)\
**Post date:** [September 4, 2013, 9:06pm UTC](https://discuss.elastic.co/t/parent-child-sort/13466/1 "2013-09-04T21:06:05Z")

</div>

can you help me, how i can use correct sort for childs  
i have query but sort does not work, I know that the field is not correctly  
specified in the sorting, but how to specify the correctly for sorting for  
childs

{  
"sort": [  
{  
"\_geo\_distance": {  
"location": {  
"lat": 40.6234,  
"lon": -74.0288  
},  
"order": "asc",  
"unit": "km"  
}  
}  
],  
"fields": [],  
"query": {  
"custom\_filters\_score": {  
"query": {  
"filtered": {  
"query": {  
"bool": {  
"must": [  
{  
"multi\_match": {  
"query": "Infiniti",  
"fields": [  
"title",  
"description",  
"facets",  
"keywords",  
"categories",  
"hidden"  
],  
"operator": "and",  
"use\_dis\_max": false  
}  
}  
],  
"should": []  
}  
},  
"filter": {  
"and": [  
{  
"bool": {  
"must": [  
{  
"terms": {  
"base\_topic": [  
"1",  
"2",  
"597820",  
"599660",  
"599661",  
"5256322",  
"599666",  
"1753836",  
"599667",  
"599669",  
"5240682",  
"599662",  
"5240666",  
"599668",  
"5240672",  
"5326625",  
"5256324",  
"599663",  
"5240669",  
"599665",  
"1753835"  
]  
}  
},  
{  
"term": {  
"id\_country": 1  
}  
},  
{  
"range": {  
"date": {  
"from": 1376938800  
}  
}  
}  
]  
}  
},  
{  
"or": [  
{  
"geo\_distance": {  
"distance": "100km",  
"location": {  
"lat": 40.6234,  
"lon": -74.0288  
}  
}  
},  
{  
"missing": {  
"field": "location"  
}  
}  
]  
}  
]  
}  
}  
},  
"filters": [],  
"score\_mode": "total"  
}  
},  
"from": 0,  
"size": 60  
}

--  
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:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 5, 2013, 12:52pm UTC](https://discuss.elastic.co/t/parent-child-sort/13466/2 "2013-09-05T12:52:51Z")

</div>

The query in the email doesn't use the has\_child query, so I don't know  
what you mean with sorting for childs. So in this case you're searching for  
product documents, and from the script you send I see that this document  
type has a location field, so that will just sort any product document that  
matches.

Sorting parent document by fields that are in child documents isn't  
supported. There is a work around that uses the custom\_score query in the  
has\_child query, that allows you to psuedo sort parent document by fields  
in child docs:  
[Document relations - Berlin Buzzwords 2013 | PPT](http://www.slideshare.net/martijnvg/document-relationsbbuz2013) (last slide)

On 4 September 2013 23:06, Max Seleznev [max@inetgiant.com](mailto:max@inetgiant.com) wrote:

> can you help me, how i can use correct sort for childs  
> i have query but sort does not work, I know that the field is not  
> correctly specified in the sorting, but how to specify the correctly for  
> sorting for childs
> 
> {  
> "sort": [  
> {  
> "\_geo\_distance": {  
> "location": {  
> "lat": 40.6234,  
> "lon": -74.0288  
> },  
> "order": "asc",  
> "unit": "km"  
> }  
> }  
> ],  
> "fields": ,  
> "query": {  
> "custom\_filters\_score": {  
> "query": {  
> "filtered": {  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "multi\_match": {  
> "query": "Infiniti",  
> "fields": [  
> "title",  
> "description",  
> "facets",  
> "keywords",  
> "categories",  
> "hidden"  
> ],  
> "operator": "and",  
> "use\_dis\_max": false  
> }  
> }  
> ],  
> "should":   
> }  
> },  
> "filter": {  
> "and": [  
> {  
> "bool": {  
> "must": [  
> {  
> "terms": {  
> "base\_topic": [  
> "1",  
> "2",  
> "597820",  
> "599660",  
> "599661",  
> "5256322",  
> "599666",  
> "1753836",  
> "599667",  
> "599669",  
> "5240682",  
> "599662",  
> "5240666",  
> "599668",  
> "5240672",  
> "5326625",  
> "5256324",  
> "599663",  
> "5240669",  
> "599665",  
> "1753835"  
> ]  
> }  
> },  
> {  
> "term": {  
> "id\_country": 1  
> }  
> },  
> {  
> "range": {  
> "date": {  
> "from": 1376938800  
> }  
> }  
> }  
> ]  
> }  
> },  
> {  
> "or": [  
> {  
> "geo\_distance": {  
> "distance": "100km",  
> "location": {  
> "lat": 40.6234,  
> "lon": -74.0288  
> }  
> }  
> },  
> {  
> "missing": {  
> "field": "location"  
> }  
> }  
> ]  
> }  
> ]  
> }  
> }  
> },  
> "filters": ,  
> "score\_mode": "total"  
> }  
> },  
> "from": 0,  
> "size": 60  
> }
> 
> --  
> 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).

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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:** ![Max\_Seleznev](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Max\_Seleznev](https://discuss.elastic.co/u/Max_Seleznev)\
**Post date:** [September 5, 2013, 2:29pm UTC](https://discuss.elastic.co/t/parent-child-sort/13466/3 "2013-09-05T14:29:33Z")

</div>

sorry, I mixed queries.  
I attached file with the request, if you started it then you will see two  
requests  
can you run attached file  
{  
"sort": [  
{  
"\_geo\_distance": {  
"location": {  
"lat": 40.6234,  
"lon": -74.0288  
},  
"order": "asc",  
"unit": "km"  
}  
}  
],  
"fields": ,  
"query": {

- "has\_child": {\*  
"score\_type": "max",  
"type": "product",  
"query": {  
"custom\_filters\_score": {  
"query": {  
"filtered": {  
"query": {  
"bool": {  
"must": [  
{  
"multi\_match": {  
"query": "Infiniti",  
"fields": [  
"title",  
"description",  
"facets",  
"keywords",  
"categories",  
"hidden"  
],  
"operator": "and",  
"use\_dis\_max": false  
}  
}  
],  
"should":   
}  
},  
"filter": {  
"and": [  
{  
"bool": {  
"must": [  
{  
"terms": {  
"base\_topic": [  
"1",  
"2",  
"597820",  
"599660",  
"599661",  
"5256322",  
"599666",  
"1753836",  
"599667",  
"599669",  
"5240682",  
"599662",  
"5240666",  
"599668",  
"5240672",  
"5326625",  
"5256324",  
"599663",  
"5240669",  
"599665",  
"1753835"  
]  
}  
},  
{  
"term": {  
"id\_country": 1  
}  
},  
{  
"range": {  
"date": {  
"from": 1376938800  
}  
}  
}  
]  
}  
},  
{  
"or": [  
{  
"geo\_distance": {  
"distance": "100km",  
"location": {  
"lat": 40.6234,  
"lon": -74.0288  
}  
}  
},  
{  
"missing": {  
"field": "location"  
}  
}  
]  
}  
]  
}  
}  
},  
"filters": ,  
"score\_mode": "total"  
}  
}  
}  
},  
"from": 0,  
"size": 60  
}

On Thursday, September 5, 2013 8:52:51 AM UTC-4, Martijn v Groningen wrote:

> The query in the email doesn't use the has\_child query, so I don't know  
> what you mean with sorting for childs. So in this case you're searching for  
> product documents, and from the script you send I see that this document  
> type has a location field, so that will just sort any product document that  
> matches.
> 
> Sorting parent document by fields that are in child documents isn't  
> supported. There is a work around that uses the custom\_score query in the  
> has\_child query, that allows you to psuedo sort parent document by fields  
> in child docs:  
> [Document relations - Berlin Buzzwords 2013 | PPT](http://www.slideshare.net/martijnvg/document-relationsbbuz2013) (last  
> slide)
> 
> On 4 September 2013 23:06, Max Seleznev \<[m...@inetgiant.com](mailto:m...@inetgiant.com) \<javascript:\>\>wrote:
> 
> > can you help me, how i can use correct sort for childs  
> > i have query but sort does not work, I know that the field is not  
> > correctly specified in the sorting, but how to specify the correctly for  
> > sorting for childs
> > 
> > {  
> > "sort": [  
> > {  
> > "\_geo\_distance": {  
> > "location": {  
> > "lat": 40.6234,  
> > "lon": -74.0288  
> > },  
> > "order": "asc",  
> > "unit": "km"  
> > }  
> > }  
> > ],  
> > "fields": ,  
> > "query": {  
> > "custom\_filters\_score": {  
> > "query": {  
> > "filtered": {  
> > "query": {  
> > "bool": {  
> > "must": [  
> > {  
> > "multi\_match": {  
> > "query": "Infiniti",  
> > "fields": [  
> > "title",  
> > "description",  
> > "facets",  
> > "keywords",  
> > "categories",  
> > "hidden"  
> > ],  
> > "operator": "and",  
> > "use\_dis\_max": false  
> > }  
> > }  
> > ],  
> > "should":   
> > }  
> > },  
> > "filter": {  
> > "and": [  
> > {  
> > "bool": {  
> > "must": [  
> > {  
> > "terms": {  
> > "base\_topic": [  
> > "1",  
> > "2",  
> > "597820",  
> > "599660",  
> > "599661",  
> > "5256322",  
> > "599666",  
> > "1753836",  
> > "599667",  
> > "599669",  
> > "5240682",  
> > "599662",  
> > "5240666",  
> > "599668",  
> > "5240672",  
> > "5326625",  
> > "5256324",  
> > "599663",  
> > "5240669",  
> > "599665",  
> > "1753835"  
> > ]  
> > }  
> > },  
> > {  
> > "term": {  
> > "id\_country": 1  
> > }  
> > },  
> > {  
> > "range": {  
> > "date": {  
> > "from": 1376938800  
> > }  
> > }  
> > }  
> > ]  
> > }  
> > },  
> > {  
> > "or": [  
> > {  
> > "geo\_distance": {  
> > "distance": "100km",  
> > "location": {  
> > "lat": 40.6234,  
> > "lon": -74.0288  
> > }  
> > }  
> > },  
> > {  
> > "missing": {  
> > "field": "location"  
> > }  
> > }  
> > ]  
> > }  
> > ]  
> > }  
> > }  
> > },  
> > "filters": ,  
> > "score\_mode": "total"  
> > }  
> > },  
> > "from": 0,  
> > "size": 60  
> > }
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--  
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:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 5, 2013, 5:21pm UTC](https://discuss.elastic.co/t/parent-child-sort/13466/4 "2013-09-05T17:21:16Z")

</div>

Did you see the last slide of the presentation I mentioned in my previous  
reply?  
What you need to do is move the sorting part to a custom\_score query and  
put that in the has\_child query as inner query. Something like this:  
{  
"query": {  
"has\_child": {  
"score\_type": "max",  
"type": "product",  
"query": {  
"custom\_score": {  
"query": {  
"match\_all": {}  
},  
"script": "doc['location'].distance(lat, lon)"  
}  
}  
}  
}  
}

In the above example the sorting by distance has been replaced by the  
distance function in the script.

On 5 September 2013 16:29, Max Seleznev [max@inetgiant.com](mailto:max@inetgiant.com) wrote:

> sorry, I mixed queries.  
> I attached file with the request, if you started it then you will see two  
> requests  
> can you run attached file  
> {  
> "sort": [  
> {  
> "\_geo\_distance": {  
> "location": {  
> "lat": 40.6234,  
> "lon": -74.0288  
> },  
> "order": "asc",  
> "unit": "km"  
> }  
> }  
> ],  
> "fields": ,  
> "query": {
> 
> - "has\_child": {\*  
> "score\_type": "max",  
> "type": "product",  
> "query": {  
> "custom\_filters\_score": {  
> "query": {  
> "filtered": {  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "multi\_match": {  
> "query": "Infiniti",  
> "fields": [  
> "title",  
> "description",  
> "facets",  
> "keywords",  
> "categories",  
> "hidden"  
> ],  
> "operator": "and",  
> "use\_dis\_max": false  
> }  
> }  
> ],  
> "should":   
> }  
> },  
> "filter": {  
> "and": [  
> {  
> "bool": {  
> "must": [  
> {  
> "terms": {  
> "base\_topic": [  
> "1",  
> "2",  
> "597820",  
> "599660",  
> "599661",  
> "5256322",  
> "599666",  
> "1753836",  
> "599667",  
> "599669",  
> "5240682",  
> "599662",  
> "5240666",  
> "599668",  
> "5240672",  
> "5326625",  
> "5256324",  
> "599663",  
> "5240669",  
> "599665",  
> "1753835"  
> ]  
> }  
> },  
> {  
> "term": {  
> "id\_country": 1  
> }  
> },  
> {  
> "range": {  
> "date": {  
> "from": 1376938800  
> }  
> }  
> }  
> ]  
> }  
> },  
> {  
> "or": [  
> {  
> "geo\_distance": {  
> "distance": "100km",  
> "location": {  
> "lat": 40.6234,  
> "lon": -74.0288  
> }  
> }  
> },  
> {  
> "missing": {  
> "field": "location"  
> }  
> }  
> ]  
> }  
> ]  
> }  
> }  
> },  
> "filters": ,  
> "score\_mode": "total"  
> }  
> }  
> }  
> },  
> "from": 0,  
> "size": 60  
> }
> 
> On Thursday, September 5, 2013 8:52:51 AM UTC-4, Martijn v Groningen wrote:
> 
> > The query in the email doesn't use the has\_child query, so I don't know  
> > what you mean with sorting for childs. So in this case you're searching for  
> > product documents, and from the script you send I see that this document  
> > type has a location field, so that will just sort any product document that  
> > matches.
> > 
> > Sorting parent document by fields that are in child documents isn't  
> > supported. There is a work around that uses the custom\_score query in the  
> > has\_child query, that allows you to psuedo sort parent document by fields  
> > in child docs:  
> > [http://www.slideshare.net/\*\*martijnvg/document-\*\*relationsbbuz2013](http://www.slideshare.net/ **martijnvg/document-** relationsbbuz2013)[http://www.slideshare.net/martijnvg/document-relationsbbuz2013](http://www.slideshare.net/martijnvg/document-relationsbbuz2013)(last slide)
> > 
> > On 4 September 2013 23:06, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > 
> > > can you help me, how i can use correct sort for childs  
> > > i have query but sort does not work, I know that the field is not  
> > > correctly specified in the sorting, but how to specify the correctly for  
> > > sorting for childs
> > > 
> > > {  
> > > "sort": [  
> > > {  
> > > "\_geo\_distance": {  
> > > "location": {  
> > > "lat": 40.6234,  
> > > "lon": -74.0288  
> > > },  
> > > "order": "asc",  
> > > "unit": "km"  
> > > }  
> > > }  
> > > ],  
> > > "fields": ,  
> > > "query": {  
> > > "custom\_filters\_score": {  
> > > "query": {  
> > > "filtered": {  
> > > "query": {  
> > > "bool": {  
> > > "must": [  
> > > {  
> > > "multi\_match": {  
> > > "query": "Infiniti",  
> > > "fields": [  
> > > "title",  
> > > "description",  
> > > "facets",  
> > > "keywords",  
> > > "categories",  
> > > "hidden"  
> > > ],  
> > > "operator": "and",  
> > > "use\_dis\_max": false  
> > > }  
> > > }  
> > > ],  
> > > "should":   
> > > }  
> > > },  
> > > "filter": {  
> > > "and": [  
> > > {  
> > > "bool": {  
> > > "must": [  
> > > {  
> > > "terms": {  
> > > "base\_topic": [  
> > > "1",  
> > > "2",  
> > > "597820",  
> > > "599660",  
> > > "599661",  
> > > "5256322",  
> > > "599666",  
> > > "1753836",  
> > > "599667",  
> > > "599669",  
> > > "5240682",  
> > > "599662",  
> > > "5240666",  
> > > "599668",  
> > > "5240672",  
> > > "5326625",  
> > > "5256324",  
> > > "599663",  
> > > "5240669",  
> > > "599665",  
> > > "1753835"  
> > > ]  
> > > }  
> > > },  
> > > {  
> > > "term": {  
> > > "id\_country": 1  
> > > }  
> > > },  
> > > {  
> > > "range": {  
> > > "date": {  
> > > "from": 1376938800  
> > > }  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > },  
> > > {  
> > > "or": [  
> > > {  
> > > "geo\_distance": {  
> > > "distance": "100km",  
> > > "location": {  
> > > "lat": 40.6234,  
> > > "lon": -74.0288  
> > > }  
> > > }  
> > > },  
> > > {  
> > > "missing": {  
> > > "field": "location"  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > },  
> > > "filters": ,  
> > > "score\_mode": "total"  
> > > }  
> > > },  
> > > "from": 0,  
> > > "size": 60  
> > > }
> > > 
> > > --  
> > > 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 elasticsearc...@\*\*[googlegroups.com](http://googlegroups.com).
> > > 
> > > For more options, visit [https://groups.google.com/\*\*groups/opt\_out](https://groups.google.com/**groups/opt_out)[https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out)  
> > > .
> > 
> > --  
> > Met vriendelijke groet,
> > 
> > Martijn van Groningen

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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:** ![Max\_Seleznev](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Max\_Seleznev](https://discuss.elastic.co/u/Max_Seleznev)\
**Post date:** [September 5, 2013, 7:16pm UTC](https://discuss.elastic.co/t/parent-child-sort/13466/5 "2013-09-05T19:16:56Z")

</div>

Thanks for idea, but i run your example and have error

- error: SearchPhaseExecutionException[Failed to execute phase  
[query\_fetch], total failure; shardFailures  
{[8xa0yPbjREmk8DuC\_jlipA][testparentfacets][0]:  
RemoteTransportException[[T-Ray][inet[/10.0.10.223:9301]][search/phase/query+fetch]];  
nested: QueryPhaseExecutionException[[testparentfacets][0]:  
query[ChildrenQuery[product/group](custom score  
(ConstantScore(_:_),function=script[doc['location'].distance(lat, lon)],  
params [null]))],from[0],size[10]: _Query Failed [failed to execute  
context rewrite]]; nested: PropertyAccessException[[Error: unresolvable  
property or identifier: lat] [Near : {... doc['location'].distance(lat,  
lon) ....}] ^ [Line: 1, Column: 26]]; }]_
- status: 500

On Thursday, September 5, 2013 1:21:16 PM UTC-4, Martijn v Groningen wrote:

> Did you see the last slide of the presentation I mentioned in my previous  
> reply?  
> What you need to do is move the sorting part to a custom\_score query and  
> put that in the has\_child query as inner query. Something like this:  
> {  
> "query": {  
> "has\_child": {  
> "score\_type": "max",  
> "type": "product",  
> "query": {  
> "custom\_score": {  
> "query": {  
> "match\_all": {}  
> },  
> "script": "doc['location'].distance(lat, lon)"  
> }  
> }  
> }  
> }  
> }
> 
> In the above example the sorting by distance has been replaced by the  
> distance function in the script.
> 
> On 5 September 2013 16:29, Max Seleznev \<[m...@inetgiant.com](mailto:m...@inetgiant.com) \<javascript:\>\>wrote:
> 
> > sorry, I mixed queries.  
> > I attached file with the request, if you started it then you will see two  
> > requests  
> > can you run attached file  
> > {  
> > "sort": [  
> > {  
> > "\_geo\_distance": {  
> > "location": {  
> > "lat": 40.6234,  
> > "lon": -74.0288  
> > },  
> > "order": "asc",  
> > "unit": "km"  
> > }  
> > }  
> > ],  
> > "fields": ,  
> > "query": {
> > 
> > - "has\_child": {\*  
> > "score\_type": "max",  
> > "type": "product",  
> > "query": {  
> > "custom\_filters\_score": {  
> > "query": {  
> > "filtered": {  
> > "query": {  
> > "bool": {  
> > "must": [  
> > {  
> > "multi\_match": {  
> > "query": "Infiniti",  
> > "fields": [  
> > "title",  
> > "description",  
> > "facets",  
> > "keywords",  
> > "categories",  
> > "hidden"  
> > ],  
> > "operator": "and",  
> > "use\_dis\_max": false  
> > }  
> > }  
> > ],  
> > "should":   
> > }  
> > },  
> > "filter": {  
> > "and": [  
> > {  
> > "bool": {  
> > "must": [  
> > {  
> > "terms": {  
> > "base\_topic": [  
> > "1",  
> > "2",  
> > "597820",  
> > "599660",  
> > "599661",  
> > "5256322",  
> > "599666",  
> > "1753836",  
> > "599667",  
> > "599669",  
> > "5240682",  
> > "599662",  
> > "5240666",  
> > "599668",  
> > "5240672",  
> > "5326625",  
> > "5256324",  
> > "599663",  
> > "5240669",  
> > "599665",  
> > "1753835"  
> > ]  
> > }  
> > },  
> > {  
> > "term": {  
> > "id\_country": 1  
> > }  
> > },  
> > {  
> > "range": {  
> > "date": {  
> > "from": 1376938800  
> > }  
> > }  
> > }  
> > ]  
> > }  
> > },  
> > {  
> > "or": [  
> > {  
> > "geo\_distance": {  
> > "distance": "100km",  
> > "location": {  
> > "lat": 40.6234,  
> > "lon": -74.0288  
> > }  
> > }  
> > },  
> > {  
> > "missing": {  
> > "field": "location"  
> > }  
> > }  
> > ]  
> > }  
> > ]  
> > }  
> > }  
> > },  
> > "filters": ,  
> > "score\_mode": "total"  
> > }  
> > }  
> > }  
> > },  
> > "from": 0,  
> > "size": 60  
> > }
> > 
> > On Thursday, September 5, 2013 8:52:51 AM UTC-4, Martijn v Groningen  
> > wrote:
> > 
> > > The query in the email doesn't use the has\_child query, so I don't know  
> > > what you mean with sorting for childs. So in this case you're searching for  
> > > product documents, and from the script you send I see that this document  
> > > type has a location field, so that will just sort any product document that  
> > > matches.
> > > 
> > > Sorting parent document by fields that are in child documents isn't  
> > > supported. There is a work around that uses the custom\_score query in the  
> > > has\_child query, that allows you to psuedo sort parent document by fields  
> > > in child docs:  
> > > [http://www.slideshare.net/\*\*martijnvg/document-\*\*relationsbbuz2013](http://www.slideshare.net/ **martijnvg/document-** relationsbbuz2013)[http://www.slideshare.net/martijnvg/document-relationsbbuz2013](http://www.slideshare.net/martijnvg/document-relationsbbuz2013)(last slide)
> > > 
> > > On 4 September 2013 23:06, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > 
> > > > can you help me, how i can use correct sort for childs  
> > > > i have query but sort does not work, I know that the field is not  
> > > > correctly specified in the sorting, but how to specify the correctly for  
> > > > sorting for childs
> > > > 
> > > > {  
> > > > "sort": [  
> > > > {  
> > > > "\_geo\_distance": {  
> > > > "location": {  
> > > > "lat": 40.6234,  
> > > > "lon": -74.0288  
> > > > },  
> > > > "order": "asc",  
> > > > "unit": "km"  
> > > > }  
> > > > }  
> > > > ],  
> > > > "fields": ,  
> > > > "query": {  
> > > > "custom\_filters\_score": {  
> > > > "query": {  
> > > > "filtered": {  
> > > > "query": {  
> > > > "bool": {  
> > > > "must": [  
> > > > {  
> > > > "multi\_match": {  
> > > > "query": "Infiniti",  
> > > > "fields": [  
> > > > "title",  
> > > > "description",  
> > > > "facets",  
> > > > "keywords",  
> > > > "categories",  
> > > > "hidden"  
> > > > ],  
> > > > "operator": "and",  
> > > > "use\_dis\_max": false  
> > > > }  
> > > > }  
> > > > ],  
> > > > "should":   
> > > > }  
> > > > },  
> > > > "filter": {  
> > > > "and": [  
> > > > {  
> > > > "bool": {  
> > > > "must": [  
> > > > {  
> > > > "terms": {  
> > > > "base\_topic": [  
> > > > "1",  
> > > > "2",  
> > > > "597820",  
> > > > "599660",  
> > > > "599661",  
> > > > "5256322",  
> > > > "599666",  
> > > > "1753836",  
> > > > "599667",  
> > > > "599669",  
> > > > "5240682",  
> > > > "599662",  
> > > > "5240666",  
> > > > "599668",  
> > > > "5240672",  
> > > > "5326625",  
> > > > "5256324",  
> > > > "599663",  
> > > > "5240669",  
> > > > "599665",  
> > > > "1753835"  
> > > > ]  
> > > > }  
> > > > },  
> > > > {  
> > > > "term": {  
> > > > "id\_country": 1  
> > > > }  
> > > > },  
> > > > {  
> > > > "range": {  
> > > > "date": {  
> > > > "from": 1376938800  
> > > > }  
> > > > }  
> > > > }  
> > > > ]  
> > > > }  
> > > > },  
> > > > {  
> > > > "or": [  
> > > > {  
> > > > "geo\_distance": {  
> > > > "distance": "100km",  
> > > > "location": {  
> > > > "lat": 40.6234,  
> > > > "lon": -74.0288  
> > > > }  
> > > > }  
> > > > },  
> > > > {  
> > > > "missing": {  
> > > > "field": "location"  
> > > > }  
> > > > }  
> > > > ]  
> > > > }  
> > > > ]  
> > > > }  
> > > > }  
> > > > },  
> > > > "filters": ,  
> > > > "score\_mode": "total"  
> > > > }  
> > > > },  
> > > > "from": 0,  
> > > > "size": 60  
> > > > }
> > > > 
> > > > --  
> > > > 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 elasticsearc...@\*\*[googlegroups.com](http://googlegroups.com).
> > > > 
> > > > For more options, visit [https://groups.google.com/\*\*groups/opt\_out](https://groups.google.com/**groups/opt_out)[https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out)  
> > > > .
> > > 
> > > --  
> > > Met vriendelijke groet,
> > > 
> > > Martijn van Groningen
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--  
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:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 6, 2013, 7:23am UTC](https://discuss.elastic.co/t/parent-child-sort/13466/6 "2013-09-06T07:23:57Z")

</div>

You need to substitute the lat and long variables in the script with the  
actual values:  
"script": "doc['location'].distance(40.6234, -74.0288)"

On 5 September 2013 21:16, Max Seleznev [max@inetgiant.com](mailto:max@inetgiant.com) wrote:

> Thanks for idea, but i run your example and have error
> 
> - error: SearchPhaseExecutionException[Failed to execute phase  
> [query\_fetch], total failure; shardFailures  
> {[8xa0yPbjREmk8DuC\_jlipA][testparentfacets][0]:  
> RemoteTransportException[[T-Ray][inet[/10.0.10.223:9301]][search/phase/query+fetch]];  
> nested: QueryPhaseExecutionException[[testparentfacets][0]:  
> query[ChildrenQuery[product/group](custom score  
> (ConstantScore(_:_),function=script[doc['location'].distance(lat, lon)],  
> params [null]))],from[0],size[10]: _Query Failed [failed to execute  
> context rewrite]]; nested: PropertyAccessException[[Error: unresolvable  
> property or identifier: lat] [Near : {... doc['location'].distance(lat,  
> lon) ....}] ^ [Line: 1, Column: 26]]; }]_
> - status: 500
> 
> On Thursday, September 5, 2013 1:21:16 PM UTC-4, Martijn v Groningen wrote:
> 
> > Did you see the last slide of the presentation I mentioned in my previous  
> > reply?  
> > What you need to do is move the sorting part to a custom\_score query and  
> > put that in the has\_child query as inner query. Something like this:  
> > {  
> > "query": {  
> > "has\_child": {  
> > "score\_type": "max",  
> > "type": "product",  
> > "query": {  
> > "custom\_score": {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "script": "doc['location'].distance(lat, lon)"  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > In the above example the sorting by distance has been replaced by the  
> > distance function in the script.
> > 
> > On 5 September 2013 16:29, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > 
> > > sorry, I mixed queries.  
> > > I attached file with the request, if you started it then you will see  
> > > two requests  
> > > can you run attached file  
> > > {  
> > > "sort": [  
> > > {  
> > > "\_geo\_distance": {  
> > > "location": {  
> > > "lat": 40.6234,  
> > > "lon": -74.0288  
> > > },  
> > > "order": "asc",  
> > > "unit": "km"  
> > > }  
> > > }  
> > > ],  
> > > "fields": ,  
> > > "query": {
> > > 
> > > - "has\_child": {\*  
> > > "score\_type": "max",  
> > > "type": "product",  
> > > "query": {  
> > > "custom\_filters\_score": {  
> > > "query": {  
> > > "filtered": {  
> > > "query": {  
> > > "bool": {  
> > > "must": [  
> > > {  
> > > "multi\_match": {  
> > > "query": "Infiniti",  
> > > "fields": [  
> > > "title",  
> > > "description",  
> > > "facets",  
> > > "keywords",  
> > > "categories",  
> > > "hidden"  
> > > ],  
> > > "operator": "and",  
> > > "use\_dis\_max": false  
> > > }  
> > > }  
> > > ],  
> > > "should":   
> > > }  
> > > },  
> > > "filter": {  
> > > "and": [  
> > > {  
> > > "bool": {  
> > > "must": [  
> > > {  
> > > "terms": {  
> > > "base\_topic": [  
> > > "1",  
> > > "2",  
> > > "597820",  
> > > "599660",  
> > > "599661",  
> > > "5256322",  
> > > "599666",  
> > > "1753836",  
> > > "599667",  
> > > "599669",  
> > > "5240682",  
> > > "599662",  
> > > "5240666",  
> > > "599668",  
> > > "5240672",  
> > > "5326625",  
> > > "5256324",  
> > > "599663",  
> > > "5240669",  
> > > "599665",  
> > > "1753835"  
> > > ]  
> > > }  
> > > },  
> > > {  
> > > "term": {  
> > > "id\_country": 1  
> > > }  
> > > },  
> > > {  
> > > "range": {  
> > > "date": {  
> > > "from": 1376938800  
> > > }  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > },  
> > > {  
> > > "or": [  
> > > {  
> > > "geo\_distance": {  
> > > "distance": "100km",  
> > > "location": {  
> > > "lat": 40.6234,  
> > > "lon": -74.0288  
> > > }  
> > > }  
> > > },  
> > > {  
> > > "missing": {  
> > > "field": "location"  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > },  
> > > "filters": ,  
> > > "score\_mode": "total"  
> > > }  
> > > }  
> > > }  
> > > },  
> > > "from": 0,  
> > > "size": 60  
> > > }
> > > 
> > > On Thursday, September 5, 2013 8:52:51 AM UTC-4, Martijn v Groningen  
> > > wrote:
> > > 
> > > > The query in the email doesn't use the has\_child query, so I don't know  
> > > > what you mean with sorting for childs. So in this case you're searching for  
> > > > product documents, and from the script you send I see that this document  
> > > > type has a location field, so that will just sort any product document that  
> > > > matches.
> > > > 
> > > > Sorting parent document by fields that are in child documents isn't  
> > > > supported. There is a work around that uses the custom\_score query in the  
> > > > has\_child query, that allows you to psuedo sort parent document by fields  
> > > > in child docs:  
> > > > [http://www.slideshare.net/\*\*mart\*\*ijnvg/document-\*\*relationsbbuz20\*\*13](http://www.slideshare.net/ **mart** ijnvg/document- **relationsbbuz20** 13)[http://www.slideshare.net/martijnvg/document-relationsbbuz2013](http://www.slideshare.net/martijnvg/document-relationsbbuz2013)(last slide)
> > > > 
> > > > On 4 September 2013 23:06, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > > 
> > > > > can you help me, how i can use correct sort for childs  
> > > > > i have query but sort does not work, I know that the field is not  
> > > > > correctly specified in the sorting, but how to specify the correctly for  
> > > > > sorting for childs
> > > > > 
> > > > > {  
> > > > > "sort": [  
> > > > > {  
> > > > > "\_geo\_distance": {  
> > > > > "location": {  
> > > > > "lat": 40.6234,  
> > > > > "lon": -74.0288  
> > > > > },  
> > > > > "order": "asc",  
> > > > > "unit": "km"  
> > > > > }  
> > > > > }  
> > > > > ],  
> > > > > "fields": ,  
> > > > > "query": {  
> > > > > "custom\_filters\_score": {  
> > > > > "query": {  
> > > > > "filtered": {  
> > > > > "query": {  
> > > > > "bool": {  
> > > > > "must": [  
> > > > > {  
> > > > > "multi\_match": {  
> > > > > "query": "Infiniti",  
> > > > > "fields": [  
> > > > > "title",  
> > > > > "description",  
> > > > > "facets",  
> > > > > "keywords",  
> > > > > "categories",  
> > > > > "hidden"  
> > > > > ],  
> > > > > "operator": "and",  
> > > > > "use\_dis\_max": false  
> > > > > }  
> > > > > }  
> > > > > ],  
> > > > > "should":   
> > > > > }  
> > > > > },  
> > > > > "filter": {  
> > > > > "and": [  
> > > > > {  
> > > > > "bool": {  
> > > > > "must": [  
> > > > > {  
> > > > > "terms": {  
> > > > > "base\_topic": [  
> > > > > "1",  
> > > > > "2",  
> > > > > "597820",  
> > > > > "599660",  
> > > > > "599661",  
> > > > > "5256322",  
> > > > > "599666",  
> > > > > "1753836",  
> > > > > "599667",  
> > > > > "599669",  
> > > > > "5240682",  
> > > > > "599662",  
> > > > > "5240666",  
> > > > > "599668",  
> > > > > "5240672",  
> > > > > "5326625",  
> > > > > "5256324",  
> > > > > "599663",  
> > > > > "5240669",  
> > > > > "599665",  
> > > > > "1753835"  
> > > > > ]  
> > > > > }  
> > > > > },  
> > > > > {  
> > > > > "term": {  
> > > > > "id\_country": 1  
> > > > > }  
> > > > > },  
> > > > > {  
> > > > > "range": {  
> > > > > "date": {  
> > > > > "from": 1376938800  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > },  
> > > > > {  
> > > > > "or": [  
> > > > > {  
> > > > > "geo\_distance": {  
> > > > > "distance": "100km",  
> > > > > "location": {  
> > > > > "lat": 40.6234,  
> > > > > "lon": -74.0288  
> > > > > }  
> > > > > }  
> > > > > },  
> > > > > {  
> > > > > "missing": {  
> > > > > "field": "location"  
> > > > > }  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > }  
> > > > > },  
> > > > > "filters": ,  
> > > > > "score\_mode": "total"  
> > > > > }  
> > > > > },  
> > > > > "from": 0,  
> > > > > "size": 60  
> > > > > }
> > > > > 
> > > > > --  
> > > > > 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 elasticsearc...@\*\*googlegroups.\*\*com.
> > > > > 
> > > > > For more options, visit [https://groups.google.com/\*\*grou\*\*ps/opt\_out](https://groups.google.com/ **grou** ps/opt_out)[https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out)  
> > > > > .
> > > > 
> > > > --  
> > > > Met vriendelijke groet,
> > > > 
> > > > Martijn van Groningen
> > 
> > --  
> > Met vriendelijke groet,
> > 
> > Martijn van Groningen

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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:** ![Max\_Seleznev](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Max\_Seleznev](https://discuss.elastic.co/u/Max_Seleznev)\
**Post date:** [September 6, 2013, 11:40am UTC](https://discuss.elastic.co/t/parent-child-sort/13466/7 "2013-09-06T11:40:03Z")

</div>

"RemoteTransportException[[Noble,  
Peter][inet[/10.0.10.225:9300]][search/phase/query]]; nested:  
QueryPhaseExecutionException[[testparentfacets][0]:  
query[ChildrenQuery[product/group](custom score  
(ConstantScore(_:_),function=script[doc[location].distance(40.6234,  
-74.0288)], params [null]))],from[0],size[10]: Query Failed [failed to  
execute context rewrite]]; nested: CompileException[[Error: No field found  
for [org.elasticsearch.index.fielddata.ScriptDocValues$GeoPoints@34187f94]  
in mapping with types ]\n[Near : {... doc[location].distance(40.6234  
....}]\n ^\n[Line: 1, Column: 1]]; nested:  
ElasticSearchIllegalArgumentException[No field found for  
[org.elasticsearch.index.fielddata.ScriptDocValues$GeoPoints@34187f94] in  
mapping with types ]; "

that does not work too

i attached file with my mapping and search query (look last query)

_Are you sure it's supposed to work?_

On Friday, September 6, 2013 3:23:57 AM UTC-4, Martijn v Groningen wrote:

> You need to substitute the lat and long variables in the script with the  
> actual values:  
> "script": "doc['location'].distance(40.6234, -74.0288)"
> 
> On 5 September 2013 21:16, Max Seleznev \<[m...@inetgiant.com](mailto:m...@inetgiant.com) \<javascript:\>\>wrote:
> 
> > Thanks for idea, but i run your example and have error
> > 
> > - error: SearchPhaseExecutionException[Failed to execute phase  
> > [query\_fetch], total failure; shardFailures  
> > {[8xa0yPbjREmk8DuC\_jlipA][testparentfacets][0]:  
> > RemoteTransportException[[T-Ray][inet[/10.0.10.223:9301]][search/phase/query+fetch]];  
> > nested: QueryPhaseExecutionException[[testparentfacets][0]:  
> > query[ChildrenQuery[product/group](custom score  
> > (ConstantScore(_:_),function=script[doc['location'].distance(lat, lon)],  
> > params [null]))],from[0],size[10]: _Query Failed [failed to execute  
> > context rewrite]]; nested: PropertyAccessException[[Error: unresolvable  
> > property or identifier: lat] [Near : {... doc['location'].distance(lat,  
> > lon) ....}] ^ [Line: 1, Column: 26]]; }]_
> > - status: 500
> > 
> > On Thursday, September 5, 2013 1:21:16 PM UTC-4, Martijn v Groningen  
> > wrote:
> > 
> > > Did you see the last slide of the presentation I mentioned in my  
> > > previous reply?  
> > > What you need to do is move the sorting part to a custom\_score query and  
> > > put that in the has\_child query as inner query. Something like this:  
> > > {  
> > > "query": {  
> > > "has\_child": {  
> > > "score\_type": "max",  
> > > "type": "product",  
> > > "query": {  
> > > "custom\_score": {  
> > > "query": {  
> > > "match\_all": {}  
> > > },  
> > > "script": "doc['location'].distance(lat, lon)"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > In the above example the sorting by distance has been replaced by the  
> > > distance function in the script.
> > > 
> > > On 5 September 2013 16:29, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > 
> > > > sorry, I mixed queries.  
> > > > I attached file with the request, if you started it then you will see  
> > > > two requests  
> > > > can you run attached file  
> > > > {  
> > > > "sort": [  
> > > > {  
> > > > "\_geo\_distance": {  
> > > > "location": {  
> > > > "lat": 40.6234,  
> > > > "lon": -74.0288  
> > > > },  
> > > > "order": "asc",  
> > > > "unit": "km"  
> > > > }  
> > > > }  
> > > > ],  
> > > > "fields": ,  
> > > > "query": {
> > > > 
> > > > - "has\_child": {\*  
> > > > "score\_type": "max",  
> > > > "type": "product",  
> > > > "query": {  
> > > > "custom\_filters\_score": {  
> > > > "query": {  
> > > > "filtered": {  
> > > > "query": {  
> > > > "bool": {  
> > > > "must": [  
> > > > {  
> > > > "multi\_match": {  
> > > > "query": "Infiniti",  
> > > > "fields": [  
> > > > "title",  
> > > > "description",  
> > > > "facets",  
> > > > "keywords",  
> > > > "categories",  
> > > > "hidden"  
> > > > ],  
> > > > "operator": "and",  
> > > > "use\_dis\_max": false  
> > > > }  
> > > > }  
> > > > ],  
> > > > "should":   
> > > > }  
> > > > },  
> > > > "filter": {  
> > > > "and": [  
> > > > {  
> > > > "bool": {  
> > > > "must": [  
> > > > {  
> > > > "terms": {  
> > > > "base\_topic": [  
> > > > "1",  
> > > > "2",  
> > > > "597820",  
> > > > "599660",  
> > > > "599661",  
> > > > "5256322",  
> > > > "599666",  
> > > > "1753836",  
> > > > "599667",  
> > > > "599669",  
> > > > "5240682",  
> > > > "599662",  
> > > > "5240666",  
> > > > "599668",  
> > > > "5240672",  
> > > > "5326625",  
> > > > "5256324",  
> > > > "599663",  
> > > > "5240669",  
> > > > "599665",  
> > > > "1753835"  
> > > > ]  
> > > > }  
> > > > },  
> > > > {  
> > > > "term": {  
> > > > "id\_country": 1  
> > > > }  
> > > > },  
> > > > {  
> > > > "range": {  
> > > > "date": {  
> > > > "from": 1376938800  
> > > > }  
> > > > }  
> > > > }  
> > > > ]  
> > > > }  
> > > > },  
> > > > {  
> > > > "or": [  
> > > > {  
> > > > "geo\_distance": {  
> > > > "distance": "100km",  
> > > > "location": {  
> > > > "lat": 40.6234,  
> > > > "lon": -74.0288  
> > > > }  
> > > > }  
> > > > },  
> > > > {  
> > > > "missing": {  
> > > > "field": "location"  
> > > > }  
> > > > }  
> > > > ]  
> > > > }  
> > > > ]  
> > > > }  
> > > > }  
> > > > },  
> > > > "filters": ,  
> > > > "score\_mode": "total"  
> > > > }  
> > > > }  
> > > > }  
> > > > },  
> > > > "from": 0,  
> > > > "size": 60  
> > > > }
> > > > 
> > > > On Thursday, September 5, 2013 8:52:51 AM UTC-4, Martijn v Groningen  
> > > > wrote:
> > > > 
> > > > > The query in the email doesn't use the has\_child query, so I don't  
> > > > > know what you mean with sorting for childs. So in this case you're  
> > > > > searching for product documents, and from the script you send I see that  
> > > > > this document type has a location field, so that will just sort any product  
> > > > > document that matches.
> > > > > 
> > > > > Sorting parent document by fields that are in child documents isn't  
> > > > > supported. There is a work around that uses the custom\_score query in the  
> > > > > has\_child query, that allows you to psuedo sort parent document by fields  
> > > > > in child docs:  
> > > > > [http://www.slideshare.net/\*\*mart\*\*ijnvg/document-\*\*relationsbbuz20\*\*13](http://www.slideshare.net/ **mart** ijnvg/document- **relationsbbuz20** 13)[http://www.slideshare.net/martijnvg/document-relationsbbuz2013](http://www.slideshare.net/martijnvg/document-relationsbbuz2013)(last slide)
> > > > > 
> > > > > On 4 September 2013 23:06, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > > > 
> > > > > > can you help me, how i can use correct sort for childs  
> > > > > > i have query but sort does not work, I know that the field is not  
> > > > > > correctly specified in the sorting, but how to specify the correctly for  
> > > > > > sorting for childs
> > > > > > 
> > > > > > {  
> > > > > > "sort": [  
> > > > > > {  
> > > > > > "\_geo\_distance": {  
> > > > > > "location": {  
> > > > > > "lat": 40.6234,  
> > > > > > "lon": -74.0288  
> > > > > > },  
> > > > > > "order": "asc",  
> > > > > > "unit": "km"  
> > > > > > }  
> > > > > > }  
> > > > > > ],  
> > > > > > "fields": ,  
> > > > > > "query": {  
> > > > > > "custom\_filters\_score": {  
> > > > > > "query": {  
> > > > > > "filtered": {  
> > > > > > "query": {  
> > > > > > "bool": {  
> > > > > > "must": [  
> > > > > > {  
> > > > > > "multi\_match": {  
> > > > > > "query": "Infiniti",  
> > > > > > "fields": [  
> > > > > > "title",  
> > > > > > "description",  
> > > > > > "facets",  
> > > > > > "keywords",  
> > > > > > "categories",  
> > > > > > "hidden"  
> > > > > > ],  
> > > > > > "operator": "and",  
> > > > > > "use\_dis\_max": false  
> > > > > > }  
> > > > > > }  
> > > > > > ],  
> > > > > > "should":   
> > > > > > }  
> > > > > > },  
> > > > > > "filter": {  
> > > > > > "and": [  
> > > > > > {  
> > > > > > "bool": {  
> > > > > > "must": [  
> > > > > > {  
> > > > > > "terms": {  
> > > > > > "base\_topic": [  
> > > > > > "1",  
> > > > > > "2",  
> > > > > > "597820",  
> > > > > > "599660",  
> > > > > > "599661",  
> > > > > > "5256322",  
> > > > > > "599666",  
> > > > > > "1753836",  
> > > > > > "599667",  
> > > > > > "599669",  
> > > > > > "5240682",  
> > > > > > "599662",  
> > > > > > "5240666",  
> > > > > > "599668",  
> > > > > > "5240672",  
> > > > > > "5326625",  
> > > > > > "5256324",  
> > > > > > "599663",  
> > > > > > "5240669",  
> > > > > > "599665",  
> > > > > > "1753835"  
> > > > > > ]  
> > > > > > }  
> > > > > > },  
> > > > > > {  
> > > > > > "term": {  
> > > > > > "id\_country": 1  
> > > > > > }  
> > > > > > },  
> > > > > > {  
> > > > > > "range": {  
> > > > > > "date": {  
> > > > > > "from": 1376938800  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > ]  
> > > > > > }  
> > > > > > },  
> > > > > > {  
> > > > > > "or": [  
> > > > > > {  
> > > > > > "geo\_distance": {  
> > > > > > "distance": "100km",  
> > > > > > "location": {  
> > > > > > "lat": 40.6234,  
> > > > > > "lon": -74.0288  
> > > > > > }  
> > > > > > }  
> > > > > > },  
> > > > > > {  
> > > > > > "missing": {  
> > > > > > "field": "location"  
> > > > > > }  
> > > > > > }  
> > > > > > ]  
> > > > > > }  
> > > > > > ]  
> > > > > > }  
> > > > > > }  
> > > > > > },  
> > > > > > "filters": ,  
> > > > > > "score\_mode": "total"  
> > > > > > }  
> > > > > > },  
> > > > > > "from": 0,  
> > > > > > "size": 60  
> > > > > > }
> > > > > > 
> > > > > > --  
> > > > > > 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 elasticsearc...@\*\*googlegroups.\*\*com.
> > > > > > 
> > > > > > For more options, visit [https://groups.google.com/\*\*grou\*\*ps/opt\_out](https://groups.google.com/ **grou** ps/opt_out)[https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out)  
> > > > > > .
> > > > > 
> > > > > --  
> > > > > Met vriendelijke groet,
> > > > > 
> > > > > Martijn van Groningen
> > > 
> > > --  
> > > Met vriendelijke groet,
> > > 
> > > Martijn van Groningen
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--  
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:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 6, 2013, 12:29pm UTC](https://discuss.elastic.co/t/parent-child-sort/13466/8 "2013-09-06T12:29:10Z")

</div>

The reason this error occurred was because of quoting issue. This shouldn't  
cause an error:  
curl -XPOST 'localhost:9203/testparentfacets/\_search?pretty=true' -d '{  
"query": {  
"has\_child": {  
"score\_type": "max",  
"type": "product",  
"query": {  
"custom\_score": {  
"query": {  
"match\_all": {}  
},  
"script": "doc["location"].distance(40.6234, -74.0288)"  
}  
}  
}  
}  
}'

On 6 September 2013 13:40, Max Seleznev [max@inetgiant.com](mailto:max@inetgiant.com) wrote:

> "RemoteTransportException[[Noble, Peter][inet[/10.0.10.225:9300]][search/phase/query]];  
> nested: QueryPhaseExecutionException[[testparentfacets][0]:  
> query[ChildrenQuery[product/group](custom score  
> (ConstantScore(_:_),function=script[doc[location].distance(40.6234,  
> -74.0288)], params [null]))],from[0],size[10]: Query Failed [failed to  
> execute context rewrite]]; nested: CompileException[[Error: No field found  
> for [org.elasticsearch.index.fielddata.ScriptDocValues$GeoPoints@34187f94]  
> in mapping with types ]\n[Near : {... doc[location].distance(40.6234  
> ....}]\n ^\n[Line: 1, Column: 1]]; nested:  
> ElasticSearchIllegalArgumentException[No field found for  
> [org.elasticsearch.index.fielddata.ScriptDocValues$GeoPoints@34187f94] in  
> mapping with types ]; "
> 
> that does not work too
> 
> i attached file with my mapping and search query (look last query)
> 
> _Are you sure it's supposed to work?_
> 
> On Friday, September 6, 2013 3:23:57 AM UTC-4, Martijn v Groningen wrote:
> 
> > You need to substitute the lat and long variables in the script with the  
> > actual values:  
> > "script": "doc['location'].distance(40.\*\*6234, -74.0288)"
> > 
> > On 5 September 2013 21:16, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > 
> > > Thanks for idea, but i run your example and have error
> > > 
> > > - error: SearchPhaseExecutionException[\*\*Failed to execute phase  
> > > [query\_fetch], total failure; shardFailures {[8xa0yPbjREmk8DuC\_jlipA][  
> > > **testparentfacets][0]: RemoteTransportException[[T-**  
> > > Ray][inet[/10.0.10.223:9301]][\*\*search/phase/query+fetch]]; nested:  
> > > QueryPhaseExecutionException[[\*\*testparentfacets][0]:  
> > > query[ChildrenQuery[product/\*\*group](custom score  
> > > (ConstantScore(_:_),function=\*\*script[doc['location'].\*\*distance(lat,  
> > > lon)], params [null]))],from[0],size[10]: \*Query Failed [failed to  
> > > execute context rewrite]]; nested: PropertyAccessException[[Error:  
> > > unresolvable property or identifier: lat] [Near : {...  
> > > doc['location'].distance(lat, lon) ....}] ^ [Line: 1, Column: 26]]; }]
> > > 
> > > - 
> > > 
> > > - status: 500
> > > 
> > > On Thursday, September 5, 2013 1:21:16 PM UTC-4, Martijn v Groningen  
> > > wrote:
> > > 
> > > > Did you see the last slide of the presentation I mentioned in my  
> > > > previous reply?  
> > > > What you need to do is move the sorting part to a custom\_score query  
> > > > and put that in the has\_child query as inner query. Something like this:  
> > > > {  
> > > > "query": {  
> > > > "has\_child": {  
> > > > "score\_type": "max",  
> > > > "type": "product",  
> > > > "query": {  
> > > > "custom\_score": {  
> > > > "query": {  
> > > > "match\_all": {}  
> > > > },  
> > > > "script": "doc['location'].distance(lat, lon)"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > In the above example the sorting by distance has been replaced by the  
> > > > distance function in the script.
> > > > 
> > > > On 5 September 2013 16:29, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > > 
> > > > > sorry, I mixed queries.  
> > > > > I attached file with the request, if you started it then you will see  
> > > > > two requests  
> > > > > can you run attached file  
> > > > > {  
> > > > > "sort": [  
> > > > > {  
> > > > > "\_geo\_distance": {  
> > > > > "location": {  
> > > > > "lat": 40.6234,  
> > > > > "lon": -74.0288  
> > > > > },  
> > > > > "order": "asc",  
> > > > > "unit": "km"  
> > > > > }  
> > > > > }  
> > > > > ],  
> > > > > "fields": ,  
> > > > > "query": {
> > > > > 
> > > > > - "has\_child": {\*  
> > > > > "score\_type": "max",  
> > > > > "type": "product",  
> > > > > "query": {  
> > > > > "custom\_filters\_score": {  
> > > > > "query": {  
> > > > > "filtered": {  
> > > > > "query": {  
> > > > > "bool": {  
> > > > > "must": [  
> > > > > {  
> > > > > "multi\_match": {  
> > > > > "query": "Infiniti",  
> > > > > "fields": [  
> > > > > "title",  
> > > > > "description",  
> > > > > "facets",  
> > > > > "keywords",  
> > > > > "categories",  
> > > > > "hidden"  
> > > > > ],  
> > > > > "operator": "and",  
> > > > > "use\_dis\_max": false  
> > > > > }  
> > > > > }  
> > > > > ],  
> > > > > "should":   
> > > > > }  
> > > > > },  
> > > > > "filter": {  
> > > > > "and": [  
> > > > > {  
> > > > > "bool": {  
> > > > > "must": [  
> > > > > {  
> > > > > "terms": {  
> > > > > "base\_topic": [  
> > > > > "1",  
> > > > > "2",  
> > > > > "597820",  
> > > > > "599660",  
> > > > > "599661",  
> > > > > "5256322",  
> > > > > "599666",  
> > > > > "1753836",  
> > > > > "599667",  
> > > > > "599669",  
> > > > > "5240682",  
> > > > > "599662",  
> > > > > "5240666",  
> > > > > "599668",  
> > > > > "5240672",  
> > > > > "5326625",  
> > > > > "5256324",  
> > > > > "599663",  
> > > > > "5240669",  
> > > > > "599665",  
> > > > > "1753835"  
> > > > > ]  
> > > > > }  
> > > > > },  
> > > > > {  
> > > > > "term": {  
> > > > > "id\_country": 1  
> > > > > }  
> > > > > },  
> > > > > {  
> > > > > "range": {  
> > > > > "date": {  
> > > > > "from": 1376938800  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > },  
> > > > > {  
> > > > > "or": [  
> > > > > {  
> > > > > "geo\_distance": {  
> > > > > "distance": "100km",  
> > > > > "location": {  
> > > > > "lat": 40.6234,  
> > > > > "lon": -74.0288  
> > > > > }  
> > > > > }  
> > > > > },  
> > > > > {  
> > > > > "missing": {  
> > > > > "field": "location"  
> > > > > }  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > }  
> > > > > },  
> > > > > "filters": ,  
> > > > > "score\_mode": "total"  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > },  
> > > > > "from": 0,  
> > > > > "size": 60  
> > > > > }
> > > > > 
> > > > > On Thursday, September 5, 2013 8:52:51 AM UTC-4, Martijn v Groningen  
> > > > > wrote:
> > > > > 
> > > > > > The query in the email doesn't use the has\_child query, so I don't  
> > > > > > know what you mean with sorting for childs. So in this case you're  
> > > > > > searching for product documents, and from the script you send I see that  
> > > > > > this document type has a location field, so that will just sort any product  
> > > > > > document that matches.
> > > > > > 
> > > > > > Sorting parent document by fields that are in child documents isn't  
> > > > > > supported. There is a work around that uses the custom\_score query in the  
> > > > > > has\_child query, that allows you to psuedo sort parent document by fields  
> > > > > > in child docs:  
> > > > > > [http://www.slideshare.net/\*\*mart\*\*\*\*ijnvg/document-\*\*relationsbbuz20](http://www.slideshare.net/ **mart**** ijnvg/document-**relationsbbuz20)\*  
> > > > > > \*\*\*13[http://www.slideshare.net/martijnvg/document-relationsbbuz2013](http://www.slideshare.net/martijnvg/document-relationsbbuz2013)(last slide)
> > > > > > 
> > > > > > On 4 September 2013 23:06, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > > > > 
> > > > > > > can you help me, how i can use correct sort for childs  
> > > > > > > i have query but sort does not work, I know that the field is not  
> > > > > > > correctly specified in the sorting, but how to specify the correctly for  
> > > > > > > sorting for childs
> > > > > > > 
> > > > > > > {  
> > > > > > > "sort": [  
> > > > > > > {  
> > > > > > > "\_geo\_distance": {  
> > > > > > > "location": {  
> > > > > > > "lat": 40.6234,  
> > > > > > > "lon": -74.0288  
> > > > > > > },  
> > > > > > > "order": "asc",  
> > > > > > > "unit": "km"  
> > > > > > > }  
> > > > > > > }  
> > > > > > > ],  
> > > > > > > "fields": ,  
> > > > > > > "query": {  
> > > > > > > "custom\_filters\_score": {  
> > > > > > > "query": {  
> > > > > > > "filtered": {  
> > > > > > > "query": {  
> > > > > > > "bool": {  
> > > > > > > "must": [  
> > > > > > > {  
> > > > > > > "multi\_match": {  
> > > > > > > "query": "Infiniti",  
> > > > > > > "fields": [  
> > > > > > > "title",  
> > > > > > > "description",  
> > > > > > > "facets",  
> > > > > > > "keywords",  
> > > > > > > "categories",  
> > > > > > > "hidden"  
> > > > > > > ],  
> > > > > > > "operator": "and",  
> > > > > > > "use\_dis\_max": false  
> > > > > > > }  
> > > > > > > }  
> > > > > > > ],  
> > > > > > > "should":   
> > > > > > > }  
> > > > > > > },  
> > > > > > > "filter": {  
> > > > > > > "and": [  
> > > > > > > {  
> > > > > > > "bool": {  
> > > > > > > "must": [  
> > > > > > > {  
> > > > > > > "terms": {  
> > > > > > > "base\_topic": [  
> > > > > > > "1",  
> > > > > > > "2",  
> > > > > > > "597820",  
> > > > > > > "599660",  
> > > > > > > "599661",  
> > > > > > > "5256322",  
> > > > > > > "599666",  
> > > > > > > "1753836",  
> > > > > > > "599667",  
> > > > > > > "599669",  
> > > > > > > "5240682",  
> > > > > > > "599662",  
> > > > > > > "5240666",  
> > > > > > > "599668",  
> > > > > > > "5240672",  
> > > > > > > "5326625",  
> > > > > > > "5256324",  
> > > > > > > "599663",  
> > > > > > > "5240669",  
> > > > > > > "599665",  
> > > > > > > "1753835"  
> > > > > > > ]  
> > > > > > > }  
> > > > > > > },  
> > > > > > > {  
> > > > > > > "term": {  
> > > > > > > "id\_country": 1  
> > > > > > > }  
> > > > > > > },  
> > > > > > > {  
> > > > > > > "range": {  
> > > > > > > "date": {  
> > > > > > > "from": 1376938800  
> > > > > > > }  
> > > > > > > }  
> > > > > > > }  
> > > > > > > ]  
> > > > > > > }  
> > > > > > > },  
> > > > > > > {  
> > > > > > > "or": [  
> > > > > > > {  
> > > > > > > "geo\_distance": {  
> > > > > > > "distance": "100km",  
> > > > > > > "location": {  
> > > > > > > "lat": 40.6234,  
> > > > > > > "lon": -74.0288  
> > > > > > > }  
> > > > > > > }  
> > > > > > > },  
> > > > > > > {  
> > > > > > > "missing": {  
> > > > > > > "field": "location"  
> > > > > > > }  
> > > > > > > }  
> > > > > > > ]  
> > > > > > > }  
> > > > > > > ]  
> > > > > > > }  
> > > > > > > }  
> > > > > > > },  
> > > > > > > "filters": ,  
> > > > > > > "score\_mode": "total"  
> > > > > > > }  
> > > > > > > },  
> > > > > > > "from": 0,  
> > > > > > > "size": 60  
> > > > > > > }
> > > > > > > 
> > > > > > > --  
> > > > > > > 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 elasticsearc...@\*\*googlegroups. **c** om.
> > > > > > > 
> > > > > > > For more options, visit [https://groups.google.com/\*\*grou](https://groups.google.com/**grou)\*\*\*\*  
> > > > > > > ps/opt\_out [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > > > 
> > > > > > --  
> > > > > > Met vriendelijke groet,
> > > > > > 
> > > > > > Martijn van Groningen
> > > > 
> > > > --  
> > > > Met vriendelijke groet,
> > > > 
> > > > Martijn van Groningen
> > 
> > --  
> > Met vriendelijke groet,
> > 
> > Martijn van Groningen

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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:** ![Max\_Seleznev](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Max\_Seleznev](https://discuss.elastic.co/u/Max_Seleznev)\
**Post date:** [September 6, 2013, 12:38pm UTC](https://discuss.elastic.co/t/parent-child-sort/13466/9 "2013-09-06T12:38:53Z")

</div>

thanks, but sort does not work

{  
"sort": [  
{  
"\_geo\_distance": {  
"location": {  
"lat": 40.6234,  
"lon": -74.0288  
},  
"order": "asc",  
"unit": "km"  
}  
}  
],  
"query": {  
"has\_child": {  
"score\_type": "max",  
"type": "product",  
"query": {  
"custom\_score": {  
"query": {  
"match\_all": {}  
},  
"script": "doc["location"].distance(40.6234, -74.0288)"  
}  
}  
}  
}  
}

_result_

"hits" : {  
"total" : 2,  
"max\_score" : null,  
"hits" : [ {  
"\_index" : "testparentfacets",  
"\_type" : "group",  
"\_id" : "64a4c4d0cf3ab7bb17e0ba62e7e201cd",  
"\_score" : null, "\_source" :  
{"hash":"64a4c4d0cf3ab7bb17e0ba62e7e201cd"},  
"sort" : [1.7976931348623157E308]  
}, {  
"\_index" : "testparentfacets",  
"\_type" : "group",  
"\_id" : "24237bb1791edbfdf349f9ffcbcaea13",  
"\_score" : null, "\_source" :  
{"hash":"24237bb1791edbfdf349f9ffcbcaea13"},  
"sort" : [1.7976931348623157E308]  
} ]  
}

On Friday, September 6, 2013 8:29:10 AM UTC-4, Martijn v Groningen wrote:

> The reason this error occurred was because of quoting issue. This  
> shouldn't cause an error:  
> curl -XPOST 'localhost:9203/testparentfacets/\_search?pretty=true' -d '{  
> "query": {  
> "has\_child": {  
> "score\_type": "max",  
> "type": "product",  
> "query": {  
> "custom\_score": {  
> "query": {  
> "match\_all": {}  
> },  
> "script": "doc["location"].distance(40.6234,  
> -74.0288)"  
> }  
> }  
> }  
> }  
> }'
> 
> On 6 September 2013 13:40, Max Seleznev \<[m...@inetgiant.com](mailto:m...@inetgiant.com) \<javascript:\>\>wrote:
> 
> > "RemoteTransportException[[Noble,  
> > Peter][inet[/10.0.10.225:9300]][search/phase/query]]; nested:  
> > QueryPhaseExecutionException[[testparentfacets][0]:  
> > query[ChildrenQuery[product/group](custom score  
> > (ConstantScore(_:_),function=script[doc[location].distance(40.6234,  
> > -74.0288)], params [null]))],from[0],size[10]: Query Failed [failed to  
> > execute context rewrite]]; nested: CompileException[[Error: No field found  
> > for [org.elasticsearch.index.fielddata.ScriptDocValues$GeoPoints@34187f94]  
> > in mapping with types ]\n[Near : {... doc[location].distance(40.6234  
> > ....}]\n ^\n[Line: 1, Column: 1]]; nested:  
> > ElasticSearchIllegalArgumentException[No field found for  
> > [org.elasticsearch.index.fielddata.ScriptDocValues$GeoPoints@34187f94] in  
> > mapping with types ]; "
> > 
> > that does not work too
> > 
> > i attached file with my mapping and search query (look last query)
> > 
> > _Are you sure it's supposed to work?_
> > 
> > On Friday, September 6, 2013 3:23:57 AM UTC-4, Martijn v Groningen wrote:
> > 
> > > You need to substitute the lat and long variables in the script with the  
> > > actual values:  
> > > "script": "doc['location'].distance(40.\*\*6234, -74.0288)"
> > > 
> > > On 5 September 2013 21:16, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > 
> > > > Thanks for idea, but i run your example and have error
> > > > 
> > > > - error: SearchPhaseExecutionException[\*\*Failed to execute phase  
> > > > [query\_fetch], total failure; shardFailures {[8xa0yPbjREmk8DuC\_jlipA][  
> > > > **testparentfacets][0]: RemoteTransportException[[T-**  
> > > > Ray][inet[/10.0.10.223:9301]][\*\*search/phase/query+fetch]]; nested:  
> > > > QueryPhaseExecutionException[[\*\*testparentfacets][0]:  
> > > > query[ChildrenQuery[product/\*\*group](custom score  
> > > > (ConstantScore(_:_),function=\*\*script[doc['location'].\*\*distance(lat,  
> > > > lon)], params [null]))],from[0],size[10]: \*Query Failed [failed to  
> > > > execute context rewrite]]; nested: PropertyAccessException[[Error:  
> > > > unresolvable property or identifier: lat] [Near : {...  
> > > > doc['location'].distance(lat, lon) ....}] ^ [Line: 1, Column: 26]]; }]
> > > > 
> > > > - 
> > > > 
> > > > - status: 500
> > > > 
> > > > On Thursday, September 5, 2013 1:21:16 PM UTC-4, Martijn v Groningen  
> > > > wrote:
> > > > 
> > > > > Did you see the last slide of the presentation I mentioned in my  
> > > > > previous reply?  
> > > > > What you need to do is move the sorting part to a custom\_score query  
> > > > > and put that in the has\_child query as inner query. Something like this:  
> > > > > {  
> > > > > "query": {  
> > > > > "has\_child": {  
> > > > > "score\_type": "max",  
> > > > > "type": "product",  
> > > > > "query": {  
> > > > > "custom\_score": {  
> > > > > "query": {  
> > > > > "match\_all": {}  
> > > > > },  
> > > > > "script": "doc['location'].distance(lat, lon)"  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }
> > > > > 
> > > > > In the above example the sorting by distance has been replaced by the  
> > > > > distance function in the script.
> > > > > 
> > > > > On 5 September 2013 16:29, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > > > 
> > > > > > sorry, I mixed queries.  
> > > > > > I attached file with the request, if you started it then you will see  
> > > > > > two requests  
> > > > > > can you run attached file  
> > > > > > {  
> > > > > > "sort": [  
> > > > > > {  
> > > > > > "\_geo\_distance": {  
> > > > > > "location": {  
> > > > > > "lat": 40.6234,  
> > > > > > "lon": -74.0288  
> > > > > > },  
> > > > > > "order": "asc",  
> > > > > > "unit": "km"  
> > > > > > }  
> > > > > > }  
> > > > > > ],  
> > > > > > "fields": ,  
> > > > > > "query": {
> > > > > > 
> > > > > > - "has\_child": {\*  
> > > > > > "score\_type": "max",  
> > > > > > "type": "product",  
> > > > > > "query": {  
> > > > > > "custom\_filters\_score": {  
> > > > > > "query": {  
> > > > > > "filtered": {  
> > > > > > "query": {  
> > > > > > "bool": {  
> > > > > > "must": [  
> > > > > > {  
> > > > > > "multi\_match": {  
> > > > > > "query": "Infiniti",  
> > > > > > "fields": [  
> > > > > > "title",  
> > > > > > "description",  
> > > > > > "facets",  
> > > > > > "keywords",  
> > > > > > "categories",  
> > > > > > "hidden"  
> > > > > > ],  
> > > > > > "operator": "and",  
> > > > > > "use\_dis\_max": false  
> > > > > > }  
> > > > > > }  
> > > > > > ],  
> > > > > > "should":   
> > > > > > }  
> > > > > > },  
> > > > > > "filter": {  
> > > > > > "and": [  
> > > > > > {  
> > > > > > "bool": {  
> > > > > > "must": [  
> > > > > > {  
> > > > > > "terms": {  
> > > > > > "base\_topic": [  
> > > > > > "1",  
> > > > > > "2",  
> > > > > > "597820",  
> > > > > > "599660",  
> > > > > > "599661",  
> > > > > > "5256322",  
> > > > > > "599666",  
> > > > > > "1753836",  
> > > > > > "599667",  
> > > > > > "599669",  
> > > > > > "5240682",  
> > > > > > "599662",  
> > > > > > "5240666",  
> > > > > > "599668",  
> > > > > > "5240672",  
> > > > > > "5326625",  
> > > > > > "5256324",  
> > > > > > "599663",  
> > > > > > "5240669",  
> > > > > > "599665",  
> > > > > > "1753835"  
> > > > > > ]  
> > > > > > }  
> > > > > > },  
> > > > > > {  
> > > > > > "term": {  
> > > > > > "id\_country": 1  
> > > > > > }  
> > > > > > },  
> > > > > > {  
> > > > > > "range": {  
> > > > > > "date": {  
> > > > > > "from": 1376938800  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > ]  
> > > > > > }  
> > > > > > },  
> > > > > > {  
> > > > > > "or": [  
> > > > > > {  
> > > > > > "geo\_distance": {  
> > > > > > "distance": "100km",  
> > > > > > "location": {  
> > > > > > "lat": 40.6234,  
> > > > > > "lon": -74.0288  
> > > > > > }  
> > > > > > }  
> > > > > > },  
> > > > > > {  
> > > > > > "missing": {  
> > > > > > "field": "location"  
> > > > > > }  
> > > > > > }  
> > > > > > ]  
> > > > > > }  
> > > > > > ]  
> > > > > > }  
> > > > > > }  
> > > > > > },  
> > > > > > "filters": ,  
> > > > > > "score\_mode": "total"  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > },  
> > > > > > "from": 0,  
> > > > > > "size": 60  
> > > > > > }
> > > > > > 
> > > > > > On Thursday, September 5, 2013 8:52:51 AM UTC-4, Martijn v Groningen  
> > > > > > wrote:
> > > > > > 
> > > > > > > The query in the email doesn't use the has\_child query, so I don't  
> > > > > > > know what you mean with sorting for childs. So in this case you're  
> > > > > > > searching for product documents, and from the script you send I see that  
> > > > > > > this document type has a location field, so that will just sort any product  
> > > > > > > document that matches.
> > > > > > > 
> > > > > > > Sorting parent document by fields that are in child documents isn't  
> > > > > > > supported. There is a work around that uses the custom\_score query in the  
> > > > > > > has\_child query, that allows you to psuedo sort parent document by fields  
> > > > > > > in child docs:  
> > > > > > > [http://www.slideshare.net/\*\*mart\*\*\*\*ijnvg/document-\*\*relationsbbuz20](http://www.slideshare.net/ **mart**** ijnvg/document-**relationsbbuz20)  
> > > > > > > \*\*\*\*13[http://www.slideshare.net/martijnvg/document-relationsbbuz2013](http://www.slideshare.net/martijnvg/document-relationsbbuz2013)(last slide)
> > > > > > > 
> > > > > > > On 4 September 2013 23:06, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > > > > > 
> > > > > > > > can you help me, how i can use correct sort for childs  
> > > > > > > > i have query but sort does not work, I know that the field is not  
> > > > > > > > correctly specified in the sorting, but how to specify the correctly for  
> > > > > > > > sorting for childs
> > > > > > > > 
> > > > > > > > {  
> > > > > > > > "sort": [  
> > > > > > > > {  
> > > > > > > > "\_geo\_distance": {  
> > > > > > > > "location": {  
> > > > > > > > "lat": 40.6234,  
> > > > > > > > "lon": -74.0288  
> > > > > > > > },  
> > > > > > > > "order": "asc",  
> > > > > > > > "unit": "km"  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > ],  
> > > > > > > > "fields": ,  
> > > > > > > > "query": {  
> > > > > > > > "custom\_filters\_score": {  
> > > > > > > > "query": {  
> > > > > > > > "filtered": {  
> > > > > > > > "query": {  
> > > > > > > > "bool": {  
> > > > > > > > "must": [  
> > > > > > > > {  
> > > > > > > > "multi\_match": {  
> > > > > > > > "query": "Infiniti",  
> > > > > > > > "fields": [  
> > > > > > > > "title",  
> > > > > > > > "description",  
> > > > > > > > "facets",  
> > > > > > > > "keywords",  
> > > > > > > > "categories",  
> > > > > > > > "hidden"  
> > > > > > > > ],  
> > > > > > > > "operator": "and",  
> > > > > > > > "use\_dis\_max": false  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > ],  
> > > > > > > > "should":   
> > > > > > > > }  
> > > > > > > > },  
> > > > > > > > "filter": {  
> > > > > > > > "and": [  
> > > > > > > > {  
> > > > > > > > "bool": {  
> > > > > > > > "must": [  
> > > > > > > > {  
> > > > > > > > "terms": {  
> > > > > > > > "base\_topic": [  
> > > > > > > > "1",  
> > > > > > > > "2",  
> > > > > > > > "597820",  
> > > > > > > > "599660",  
> > > > > > > > "599661",  
> > > > > > > > "5256322",  
> > > > > > > > "599666",  
> > > > > > > > "1753836",  
> > > > > > > > "599667",  
> > > > > > > > "599669",  
> > > > > > > > "5240682",  
> > > > > > > > "599662",  
> > > > > > > > "5240666",  
> > > > > > > > "599668",  
> > > > > > > > "5240672",  
> > > > > > > > "5326625",  
> > > > > > > > "5256324",  
> > > > > > > > "599663",  
> > > > > > > > "5240669",  
> > > > > > > > "599665",  
> > > > > > > > "1753835"  
> > > > > > > > ]  
> > > > > > > > }  
> > > > > > > > },  
> > > > > > > > {  
> > > > > > > > "term": {  
> > > > > > > > "id\_country": 1  
> > > > > > > > }  
> > > > > > > > },  
> > > > > > > > {  
> > > > > > > > "range": {  
> > > > > > > > "date": {  
> > > > > > > > "from": 1376938800  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > ]  
> > > > > > > > }  
> > > > > > > > },  
> > > > > > > > {  
> > > > > > > > "or": [  
> > > > > > > > {  
> > > > > > > > "geo\_distance": {  
> > > > > > > > "distance": "100km",  
> > > > > > > > "location": {  
> > > > > > > > "lat": 40.6234,  
> > > > > > > > "lon": -74.0288  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > },  
> > > > > > > > {  
> > > > > > > > "missing": {  
> > > > > > > > "field": "location"  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > ]  
> > > > > > > > }  
> > > > > > > > ]  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > },  
> > > > > > > > "filters": ,  
> > > > > > > > "score\_mode": "total"  
> > > > > > > > }  
> > > > > > > > },  
> > > > > > > > "from": 0,  
> > > > > > > > "size": 60  
> > > > > > > > }
> > > > > > > > 
> > > > > > > > --  
> > > > > > > > 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 elasticsearc...@\*\*googlegroups. **c** om.
> > > > > > > > 
> > > > > > > > For more options, visit [https://groups.google.com/\*\*grou](https://groups.google.com/**grou)\*\*\*\*  
> > > > > > > > ps/opt\_out [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > > > > 
> > > > > > > --  
> > > > > > > Met vriendelijke groet,
> > > > > > > 
> > > > > > > Martijn van Groningen
> > > > > 
> > > > > --  
> > > > > Met vriendelijke groet,
> > > > > 
> > > > > Martijn van Groningen
> > > 
> > > --  
> > > Met vriendelijke groet,
> > > 
> > > Martijn van Groningen
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--  
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:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 6, 2013, 1:06pm UTC](https://discuss.elastic.co/t/parent-child-sort/13466/10 "2013-09-06T13:06:46Z")

</div>

The sorting by distance is done inside the script, you should remove the  
other sort. The script will output the distance in the \_score field for  
each hit.

On 6 September 2013 14:38, Max Seleznev [max@inetgiant.com](mailto:max@inetgiant.com) wrote:

> thanks, but sort does not work
> 
> {  
> "sort": [  
> {  
> "\_geo\_distance": {  
> "location": {  
> "lat": 40.6234,  
> "lon": -74.0288  
> },  
> "order": "asc",  
> "unit": "km"  
> }  
> }  
> ],  
> "query": {  
> "has\_child": {  
> "score\_type": "max",  
> "type": "product",  
> "query": {  
> "custom\_score": {  
> "query": {  
> "match\_all": {}  
> },  
> "script": "doc["location"].distance(40.6234,  
> -74.0288)"  
> }  
> }  
> }  
> }  
> }
> 
> _result_
> 
> "hits" : {  
> "total" : 2,  
> "max\_score" : null,  
> "hits" : [ {  
> "\_index" : "testparentfacets",  
> "\_type" : "group",  
> "\_id" : "64a4c4d0cf3ab7bb17e0ba62e7e201cd",  
> "\_score" : null, "\_source" :  
> {"hash":"64a4c4d0cf3ab7bb17e0ba62e7e201cd"},  
> "sort" : [1.7976931348623157E308]  
> }, {  
> "\_index" : "testparentfacets",  
> "\_type" : "group",  
> "\_id" : "24237bb1791edbfdf349f9ffcbcaea13",  
> "\_score" : null, "\_source" :  
> {"hash":"24237bb1791edbfdf349f9ffcbcaea13"},  
> "sort" : [1.7976931348623157E308]  
> } ]  
> }
> 
> On Friday, September 6, 2013 8:29:10 AM UTC-4, Martijn v Groningen wrote:
> 
> > The reason this error occurred was because of quoting issue. This  
> > shouldn't cause an error:  
> > curl -XPOST 'localhost:9203/\*\*testparentfacets/\_search?\*\*pretty=true' -d  
> > '{  
> > "query": {  
> > "has\_child": {  
> > "score\_type": "max",  
> > "type": "product",  
> > "query": {  
> > "custom\_score": {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "script": "doc["location"].distance(\*\*40.6234,  
> > -74.0288)"  
> > }  
> > }  
> > }  
> > }  
> > }'
> > 
> > On 6 September 2013 13:40, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > 
> > > "RemoteTransportException[[**Noble, Peter][inet[/10.0.10.225:9300]**][search/phase/query]];  
> > > nested: QueryPhaseExecutionException[[\*\*testparentfacets][0]:  
> > > query[ChildrenQuery[product/\*\*group](custom score  
> > > (ConstantScore(_:_),function=\*\*script[doc[location].distance(**40.6234,  
> > > -74.0288)], params [null]))],from[0],size[10]: Query Failed [failed to  
> > > execute context rewrite]]; nested: CompileException[[Error: No field found  
> > > for [org.elasticsearch.index.fielddata.ScriptDocValues$  
> > > GeoPoints@34187f94] in mapping with types []]\n[Near : {...  
> > > doc[location].distance(40.6234 ....}]\n ^\n[Line: 1, Column:  
> > > 1]]; nested: ElasticSearchIllegalArgumentEx**ception[No field found for  
> > > [org.elasticsearch.index. **fielddata.ScriptDocValues$**  
> > > GeoPoints@34187f94] in mapping with types ]; "
> > > 
> > > that does not work too
> > > 
> > > i attached file with my mapping and search query (look last query)
> > > 
> > > _Are you sure it's supposed to work?_
> > > 
> > > On Friday, September 6, 2013 3:23:57 AM UTC-4, Martijn v Groningen wrote:
> > > 
> > > > You need to substitute the lat and long variables in the script with  
> > > > the actual values:  
> > > > "script": "doc['location'].distance(40. **6** 234, -74.0288)"
> > > > 
> > > > On 5 September 2013 21:16, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > > 
> > > > > Thanks for idea, but i run your example and have error
> > > > > 
> > > > > - error: SearchPhaseExecutionException[\*\*\*\*Failed to execute phase  
> > > > > [query\_fetch], total failure; shardFailures {[8xa0yPbjREmk8DuC\_jlipA][  
> > > > > **test** parentfacets][0]: RemoteTransportException[[T- **Ra**  
> > > > > y][inet[/10.0.10.223:9301]][**se** arch/phase/query+fetch]];  
> > > > > nested: QueryPhaseExecutionException[[\*\*_testparentfacets][0]:  
> > > > > query[ChildrenQuery[product/ **gr** oup](custom score  
> > > > > (ConstantScore(:_),function= **s** cript[doc['location'].\*_distance_  
> > > > > \*(lat, lon)], params [null]))],from[0],size[10]: \*Query Failed  
> > > > > [failed to execute context rewrite]]; nested: PropertyAccessException[[  
> > > > > Error: unresolvable property or identifier: lat] [Near : {...  
> > > > > doc['location'].distance(lat, lon) ....}] ^ [Line: 1, Column: 26]]; }]
> > > > > 
> > > > > - 
> > > > > 
> > > > > - status: 500
> > > > > 
> > > > > On Thursday, September 5, 2013 1:21:16 PM UTC-4, Martijn v Groningen  
> > > > > wrote:
> > > > > 
> > > > > > Did you see the last slide of the presentation I mentioned in my  
> > > > > > previous reply?  
> > > > > > What you need to do is move the sorting part to a custom\_score query  
> > > > > > and put that in the has\_child query as inner query. Something like this:  
> > > > > > {  
> > > > > > "query": {  
> > > > > > "has\_child": {  
> > > > > > "score\_type": "max",  
> > > > > > "type": "product",  
> > > > > > "query": {  
> > > > > > "custom\_score": {  
> > > > > > "query": {  
> > > > > > "match\_all": {}  
> > > > > > },  
> > > > > > "script": "doc['location'].distance(lat, lon)"  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }
> > > > > > 
> > > > > > In the above example the sorting by distance has been replaced by the  
> > > > > > distance function in the script.
> > > > > > 
> > > > > > On 5 September 2013 16:29, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > > > > 
> > > > > > > sorry, I mixed queries.  
> > > > > > > I attached file with the request, if you started it then you will  
> > > > > > > see two requests  
> > > > > > > can you run attached file  
> > > > > > > {  
> > > > > > > "sort": [  
> > > > > > > {  
> > > > > > > "\_geo\_distance": {  
> > > > > > > "location": {  
> > > > > > > "lat": 40.6234,  
> > > > > > > "lon": -74.0288  
> > > > > > > },  
> > > > > > > "order": "asc",  
> > > > > > > "unit": "km"  
> > > > > > > }  
> > > > > > > }  
> > > > > > > ],  
> > > > > > > "fields": ,  
> > > > > > > "query": {
> > > > > > > 
> > > > > > > - "has\_child": {\*  
> > > > > > > "score\_type": "max",  
> > > > > > > "type": "product",  
> > > > > > > "query": {  
> > > > > > > "custom\_filters\_score": {  
> > > > > > > "query": {  
> > > > > > > "filtered": {  
> > > > > > > "query": {  
> > > > > > > "bool": {  
> > > > > > > "must": [  
> > > > > > > {  
> > > > > > > "multi\_match": {  
> > > > > > > "query": "Infiniti",  
> > > > > > > "fields": [  
> > > > > > > "title",  
> > > > > > > "description",  
> > > > > > > "facets",  
> > > > > > > "keywords",  
> > > > > > > "categories",  
> > > > > > > "hidden"  
> > > > > > > ],  
> > > > > > > "operator": "and",  
> > > > > > > "use\_dis\_max": false  
> > > > > > > }  
> > > > > > > }  
> > > > > > > ],  
> > > > > > > "should":   
> > > > > > > }  
> > > > > > > },  
> > > > > > > "filter": {  
> > > > > > > "and": [  
> > > > > > > {  
> > > > > > > "bool": {  
> > > > > > > "must": [  
> > > > > > > {  
> > > > > > > "terms": {  
> > > > > > > "base\_topic": [  
> > > > > > > "1",  
> > > > > > > "2",  
> > > > > > > "597820",  
> > > > > > > "599660",  
> > > > > > > "599661",  
> > > > > > > "5256322",  
> > > > > > > "599666",  
> > > > > > > "1753836",  
> > > > > > > "599667",  
> > > > > > > "599669",  
> > > > > > > "5240682",  
> > > > > > > "599662",  
> > > > > > > "5240666",  
> > > > > > > "599668",  
> > > > > > > "5240672",  
> > > > > > > "5326625",  
> > > > > > > "5256324",  
> > > > > > > "599663",  
> > > > > > > "5240669",  
> > > > > > > "599665",  
> > > > > > > "1753835"  
> > > > > > > ]  
> > > > > > > }  
> > > > > > > },  
> > > > > > > {  
> > > > > > > "term": {  
> > > > > > > "id\_country": 1  
> > > > > > > }  
> > > > > > > },  
> > > > > > > {  
> > > > > > > "range": {  
> > > > > > > "date": {  
> > > > > > > "from": 1376938800  
> > > > > > > }  
> > > > > > > }  
> > > > > > > }  
> > > > > > > ]  
> > > > > > > }  
> > > > > > > },  
> > > > > > > {  
> > > > > > > "or": [  
> > > > > > > {  
> > > > > > > "geo\_distance": {  
> > > > > > > "distance": "100km",  
> > > > > > > "location": {  
> > > > > > > "lat": 40.6234,  
> > > > > > > "lon": -74.0288  
> > > > > > > }  
> > > > > > > }  
> > > > > > > },  
> > > > > > > {  
> > > > > > > "missing": {  
> > > > > > > "field": "location"  
> > > > > > > }  
> > > > > > > }  
> > > > > > > ]  
> > > > > > > }  
> > > > > > > ]  
> > > > > > > }  
> > > > > > > }  
> > > > > > > },  
> > > > > > > "filters": ,  
> > > > > > > "score\_mode": "total"  
> > > > > > > }  
> > > > > > > }  
> > > > > > > }  
> > > > > > > },  
> > > > > > > "from": 0,  
> > > > > > > "size": 60  
> > > > > > > }
> > > > > > > 
> > > > > > > On Thursday, September 5, 2013 8:52:51 AM UTC-4, Martijn v Groningen  
> > > > > > > wrote:
> > > > > > > 
> > > > > > > > The query in the email doesn't use the has\_child query, so I don't  
> > > > > > > > know what you mean with sorting for childs. So in this case you're  
> > > > > > > > searching for product documents, and from the script you send I see that  
> > > > > > > > this document type has a location field, so that will just sort any product  
> > > > > > > > document that matches.
> > > > > > > > 
> > > > > > > > Sorting parent document by fields that are in child documents isn't  
> > > > > > > > supported. There is a work around that uses the custom\_score query in the  
> > > > > > > > has\_child query, that allows you to psuedo sort parent document by fields  
> > > > > > > > in child docs:  
> > > > > > > > [http://www.slideshare.net/\*\*mart\*\*\*\*\*\*ijnvg/document-](http://www.slideshare.net/ **mart****** ijnvg/document-)\*\*  
> > > > > > > > relationsbbuz20\*\*\*\*\*\*13[http://www.slideshare.net/martijnvg/document-relationsbbuz2013](http://www.slideshare.net/martijnvg/document-relationsbbuz2013)(last slide)
> > > > > > > > 
> > > > > > > > On 4 September 2013 23:06, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com)wrote:
> > > > > > > > 
> > > > > > > > > can you help me, how i can use correct sort for childs  
> > > > > > > > > i have query but sort does not work, I know that the field is not  
> > > > > > > > > correctly specified in the sorting, but how to specify the correctly for  
> > > > > > > > > sorting for childs
> > > > > > > > > 
> > > > > > > > > {  
> > > > > > > > > "sort": [  
> > > > > > > > > {  
> > > > > > > > > "\_geo\_distance": {  
> > > > > > > > > "location": {  
> > > > > > > > > "lat": 40.6234,  
> > > > > > > > > "lon": -74.0288  
> > > > > > > > > },  
> > > > > > > > > "order": "asc",  
> > > > > > > > > "unit": "km"  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > ],  
> > > > > > > > > "fields": ,  
> > > > > > > > > "query": {  
> > > > > > > > > "custom\_filters\_score": {  
> > > > > > > > > "query": {  
> > > > > > > > > "filtered": {  
> > > > > > > > > "query": {  
> > > > > > > > > "bool": {  
> > > > > > > > > "must": [  
> > > > > > > > > {  
> > > > > > > > > "multi\_match": {  
> > > > > > > > > "query": "Infiniti",  
> > > > > > > > > "fields": [  
> > > > > > > > > "title",  
> > > > > > > > > "description",  
> > > > > > > > > "facets",  
> > > > > > > > > "keywords",  
> > > > > > > > > "categories",  
> > > > > > > > > "hidden"  
> > > > > > > > > ],  
> > > > > > > > > "operator": "and",  
> > > > > > > > > "use\_dis\_max": false  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > ],  
> > > > > > > > > "should":   
> > > > > > > > > }  
> > > > > > > > > },  
> > > > > > > > > "filter": {  
> > > > > > > > > "and": [  
> > > > > > > > > {  
> > > > > > > > > "bool": {  
> > > > > > > > > "must": [  
> > > > > > > > > {  
> > > > > > > > > "terms": {  
> > > > > > > > > "base\_topic": [  
> > > > > > > > > "1",  
> > > > > > > > > "2",  
> > > > > > > > > "597820",  
> > > > > > > > > "599660",  
> > > > > > > > > "599661",  
> > > > > > > > > "5256322",  
> > > > > > > > > "599666",  
> > > > > > > > > "1753836",  
> > > > > > > > > "599667",  
> > > > > > > > > "599669",  
> > > > > > > > > "5240682",  
> > > > > > > > > "599662",  
> > > > > > > > > "5240666",  
> > > > > > > > > "599668",  
> > > > > > > > > "5240672",  
> > > > > > > > > "5326625",  
> > > > > > > > > "5256324",  
> > > > > > > > > "599663",  
> > > > > > > > > "5240669",  
> > > > > > > > > "599665",  
> > > > > > > > > "1753835"  
> > > > > > > > > ]  
> > > > > > > > > }  
> > > > > > > > > },  
> > > > > > > > > {  
> > > > > > > > > "term": {  
> > > > > > > > > "id\_country": 1  
> > > > > > > > > }  
> > > > > > > > > },  
> > > > > > > > > {  
> > > > > > > > > "range": {  
> > > > > > > > > "date": {  
> > > > > > > > > "from": 1376938800  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > ]  
> > > > > > > > > }  
> > > > > > > > > },  
> > > > > > > > > {  
> > > > > > > > > "or": [  
> > > > > > > > > {  
> > > > > > > > > "geo\_distance": {  
> > > > > > > > > "distance": "100km",  
> > > > > > > > > "location": {  
> > > > > > > > > "lat": 40.6234,  
> > > > > > > > > "lon": -74.0288  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > },  
> > > > > > > > > {  
> > > > > > > > > "missing": {  
> > > > > > > > > "field": "location"  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > ]  
> > > > > > > > > }  
> > > > > > > > > ]  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > },  
> > > > > > > > > "filters": ,  
> > > > > > > > > "score\_mode": "total"  
> > > > > > > > > }  
> > > > > > > > > },  
> > > > > > > > > "from": 0,  
> > > > > > > > > "size": 60  
> > > > > > > > > }
> > > > > > > > > 
> > > > > > > > > --  
> > > > > > > > > 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 elasticsearc...@\*\*googlegroups.\*\*c\*\*\*\*om.
> > > > > > > > > 
> > > > > > > > > For more options, visit [https://groups.google.com/\*\*grou](https://groups.google.com/**grou)\*\*\*\*\*\*  
> > > > > > > > > ps/opt\_out [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > > > > > 
> > > > > > > > --  
> > > > > > > > Met vriendelijke groet,
> > > > > > > > 
> > > > > > > > Martijn van Groningen
> > > > > > 
> > > > > > --  
> > > > > > Met vriendelijke groet,
> > > > > > 
> > > > > > Martijn van Groningen
> > > > 
> > > > --  
> > > > Met vriendelijke groet,
> > > > 
> > > > Martijn van Groningen
> > 
> > --  
> > Met vriendelijke groet,
> > 
> > Martijn van Groningen

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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:** ![Max\_Seleznev](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Max\_Seleznev](https://discuss.elastic.co/u/Max_Seleznev)\
**Post date:** [September 6, 2013, 3:07pm UTC](https://discuss.elastic.co/t/parent-child-sort/13466/11 "2013-09-06T15:07:48Z")

</div>

That does not work,  
full script [es.geo.sort.sh · GitHub](https://gist.github.com/max-inetgiant/9fb4789935b2bc73e772)

_item 1_

# _parent_

{"ok":true,"\_index":"testparentfacets","\_type":"group","\_id":"\*  
64a4c4d0cf3ab7bb17e0ba62e7e201cd\*","\_version":1}

# _child_

{"ok":true,"\_index":"testparentfacets","\_type":"product","\_id":"\*  
bca1efe0cd89dc42bfc913f098b67bf3\*","\_version":1}  
_item 2_

# _parent_

{"ok":true,"\_index":"testparentfacets","\_type":"group","\_id":"\*  
24237bb1791edbfdf349f9ffcbcaea13\*","\_version":1}

# _child_

{"ok":true,"\_index":"testparentfacets","\_type":"product","\_id":"\*  
f047b7225daa8dd2ab6258f9fed9a3e1\*","\_version":1}

_this is result with not correct sort, i run search query by parect and  
sort by location (first query)_ _not good results_

# {

# "took" : 2,

# "timed\_out" : false,

# "\_shards" : {

# "total" : 2,

# "successful" : 2,

# "failed" : 0

# },

# "hits" : {

# "total" : 2,

# "max\_score" : null,

# "hits" : [ {

# "\_index" : "testparentfacets",

# "\_type" : "group",

# "\_id" : "_64a4c4d0cf3ab7bb17e0ba62e7e201cd_",

# "\_score" : null,

# "sort" : [1.7976931348623157E308]

# }, {

# "\_index" : "testparentfacets",

# "\_type" : "group",

# "\_id" : "_24237bb1791edbfdf349f9ffcbcaea13_",

# "\_score" : null,

# "sort" : [1.7976931348623157E308]

# } ]

# }

# }

_this is result with correct sort, i run search query by childs only and  
sort by location (second query)_ _perfect results_

# {

# "took" : 1,

# "timed\_out" : false,

# "\_shards" : {

# "total" : 2,

# "successful" : 2,

# "failed" : 0

# },

# "hits" : {

# "total" : 2,

# "max\_score" : null,

# "hits" : [ {

# "\_index" : "testparentfacets",

# "\_type" : "product",

# "\_id" : "_f047b7225daa8dd2ab6258f9fed9a3e1_",

```
                                    *parent* *

```

24237bb1791edbfdf349f9ffcbcaea13\*

# "\_score" : null,

# "sort" : [50.236949738919485]

# }, {

# "\_index" : "testparentfacets",

# "\_type" : "product",

# "\_id" : "_bca1efe0cd89dc42bfc913f098b67bf3_",

```
                                     *parent* *

```

64a4c4d0cf3ab7bb17e0ba62e7e201cd\*

# "\_score" : null,

# "sort" : [56.1165820505235]

# } ]

# }

# }

_and your example, result is not correct_

curl -XPOST '[http://10.0.10.223:9200/testparentfacets/\_search?pretty=true](http://10.0.10.223:9200/testparentfacets/_search?pretty=true)'  
-d '  
{  
"fields": ,  
"query": {  
"has\_child": {  
"score\_type": "max",  
"type": "product",  
"query": {  
"custom\_score": {  
"query": {  
"match\_all": {}  
},  
"script": "doc["location"].distance(40.6234, -74.0288)"  
}  
}  
}  
}  
}

' && echo

# {

# "took" : 2,

# "timed\_out" : false,

# "\_shards" : {

# "total" : 5,

# "successful" : 5,

# "failed" : 0

# },

# "hits" : {

# "total" : 2,

# "max\_score" : 39.39948,

# "hits" : [ {

# "\_index" : "testparentfacets",

# "\_type" : "group",

# "\_id" : "_64a4c4d0cf3ab7bb17e0ba62e7e201cd_",

# "\_score" : 39.39948

# }, {

# "\_index" : "testparentfacets",

# "\_type" : "group",

# "\_id" : "_24237bb1791edbfdf349f9ffcbcaea13_",

# "\_score" : 33.126003

# } ]

# }

# }

On Friday, September 6, 2013 9:06:46 AM UTC-4, Martijn v Groningen wrote:

> The sorting by distance is done inside the script, you should remove the  
> other sort. The script will output the distance in the \_score field for  
> each hit.
> 
> On 6 September 2013 14:38, Max Seleznev \<[m...@inetgiant.com](mailto:m...@inetgiant.com) \<javascript:\>\>wrote:
> 
> > thanks, but sort does not work
> > 
> > {  
> > "sort": [  
> > {  
> > "\_geo\_distance": {  
> > "location": {  
> > "lat": 40.6234,  
> > "lon": -74.0288  
> > },  
> > "order": "asc",  
> > "unit": "km"  
> > }  
> > }  
> > ],  
> > "query": {  
> > "has\_child": {  
> > "score\_type": "max",  
> > "type": "product",  
> > "query": {  
> > "custom\_score": {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "script": "doc["location"].distance(40.6234,  
> > -74.0288)"  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > _result_
> > 
> > "hits" : {  
> > "total" : 2,  
> > "max\_score" : null,  
> > "hits" : [ {  
> > "\_index" : "testparentfacets",  
> > "\_type" : "group",  
> > "\_id" : "64a4c4d0cf3ab7bb17e0ba62e7e201cd",  
> > "\_score" : null, "\_source" :  
> > {"hash":"64a4c4d0cf3ab7bb17e0ba62e7e201cd"},  
> > "sort" : [1.7976931348623157E308]  
> > }, {  
> > "\_index" : "testparentfacets",  
> > "\_type" : "group",  
> > "\_id" : "24237bb1791edbfdf349f9ffcbcaea13",  
> > "\_score" : null, "\_source" :  
> > {"hash":"24237bb1791edbfdf349f9ffcbcaea13"},  
> > "sort" : [1.7976931348623157E308]  
> > } ]  
> > }
> > 
> > On Friday, September 6, 2013 8:29:10 AM UTC-4, Martijn v Groningen wrote:
> > 
> > > The reason this error occurred was because of quoting issue. This  
> > > shouldn't cause an error:  
> > > curl -XPOST 'localhost:9203/\*\*testparentfacets/\_search?\*\*pretty=true'  
> > > -d '{  
> > > "query": {  
> > > "has\_child": {  
> > > "score\_type": "max",  
> > > "type": "product",  
> > > "query": {  
> > > "custom\_score": {  
> > > "query": {  
> > > "match\_all": {}  
> > > },  
> > > "script": "doc["location"].distance(\*\*40.6234,  
> > > -74.0288)"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }'
> > > 
> > > On 6 September 2013 13:40, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > 
> > > > "RemoteTransportException[[**Noble, Peter][inet[/10.0.10.225:9300]**][search/phase/query]];  
> > > > nested: QueryPhaseExecutionException[[\*\*testparentfacets][0]:  
> > > > query[ChildrenQuery[product/\*\*group](custom score  
> > > > (ConstantScore(_:_),function=\*\*script[doc[location].distance(\*\*40.6234,  
> > > > -74.0288)], params [null]))],from[0],size[10]: Query Failed [failed to  
> > > > execute context rewrite]]; nested: CompileException[[Error: No field found  
> > > > for [org.elasticsearch.index.\*\*fielddata.ScriptDocValues$**GeoPoints@34187f94]  
> > > > in mapping with types []]\n[Near : {... doc[location].distance(40.6234  
> > > > ....}]\n ^\n[Line: 1, Column: 1]]; nested:  
> > > > ElasticSearchIllegalArgumentEx**ception[No field found for  
> > > > [org.elasticsearch.index.\*\*fielddata.ScriptDocValues$\*\*GeoPoints@34187f94]  
> > > > in mapping with types ]; "
> > > > 
> > > > that does not work too
> > > > 
> > > > i attached file with my mapping and search query (look last query)
> > > > 
> > > > _Are you sure it's supposed to work?_
> > > > 
> > > > On Friday, September 6, 2013 3:23:57 AM UTC-4, Martijn v Groningen  
> > > > wrote:
> > > > 
> > > > > You need to substitute the lat and long variables in the script with  
> > > > > the actual values:  
> > > > > "script": "doc['location'].distance(40. **6** 234, -74.0288)"
> > > > > 
> > > > > On 5 September 2013 21:16, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > > > 
> > > > > > Thanks for idea, but i run your example and have error
> > > > > > 
> > > > > > - error: SearchPhaseExecutionException[\*\*\*\*Failed to execute  
> > > > > > phase [query\_fetch], total failure; shardFailures {[8xa0yPbjREmk8DuC\_jlipA][  
> > > > > > **test** parentfacets][0]: RemoteTransportException[[T- **Ra**  
> > > > > > y][inet[/10.0.10.223:9301]][**se** arch/phase/query+fetch]];  
> > > > > > nested: QueryPhaseExecutionException[[\*\*_testparentfacets][0]:  
> > > > > > query[ChildrenQuery[product/ **gr** oup](custom score  
> > > > > > (ConstantScore(:_),function= **s** cript[doc['location'].\*\*distance  
> > > > > > \*\*(lat, lon)], params [null]))],from[0],size[10]: \*Query Failed  
> > > > > > [failed to execute context rewrite]]; nested: PropertyAccessException[[  
> > > > > > Error: unresolvable property or identifier: lat] [Near : {...  
> > > > > > doc['location'].distance(lat, lon) ....}] ^ [Line: 1, Column: 26]]; }]
> > > > > > 
> > > > > > - 
> > > > > > 
> > > > > > - status: 500
> > > > > > 
> > > > > > On Thursday, September 5, 2013 1:21:16 PM UTC-4, Martijn v Groningen  
> > > > > > wrote:
> > > > > > 
> > > > > > > Did you see the last slide of the presentation I mentioned in my  
> > > > > > > previous reply?  
> > > > > > > What you need to do is move the sorting part to a custom\_score query  
> > > > > > > and put that in the has\_child query as inner query. Something like this:  
> > > > > > > {  
> > > > > > > "query": {  
> > > > > > > "has\_child": {  
> > > > > > > "score\_type": "max",  
> > > > > > > "type": "product",  
> > > > > > > "query": {  
> > > > > > > "custom\_score": {  
> > > > > > > "query": {  
> > > > > > > "match\_all": {}  
> > > > > > > },  
> > > > > > > "script": "doc['location'].distance(lat, lon)"  
> > > > > > > }  
> > > > > > > }  
> > > > > > > }  
> > > > > > > }  
> > > > > > > }
> > > > > > > 
> > > > > > > In the above example the sorting by distance has been replaced by  
> > > > > > > the distance function in the script.
> > > > > > > 
> > > > > > > On 5 September 2013 16:29, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > > > > > 
> > > > > > > > sorry, I mixed queries.  
> > > > > > > > I attached file with the request, if you started it then you will  
> > > > > > > > see two requests  
> > > > > > > > can you run attached file  
> > > > > > > > {  
> > > > > > > > "sort": [  
> > > > > > > > {  
> > > > > > > > "\_geo\_distance": {  
> > > > > > > > "location": {  
> > > > > > > > "lat": 40.6234,  
> > > > > > > > "lon": -74.0288  
> > > > > > > > },  
> > > > > > > > "order": "asc",  
> > > > > > > > "unit": "km"  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > ],  
> > > > > > > > "fields": ,  
> > > > > > > > "query": {
> > > > > > > > 
> > > > > > > > - "has\_child": {\*  
> > > > > > > > "score\_type": "max",  
> > > > > > > > "type": "product",  
> > > > > > > > "query": {  
> > > > > > > > "custom\_filters\_score": {  
> > > > > > > > "query": {  
> > > > > > > > "filtered": {  
> > > > > > > > "query": {  
> > > > > > > > "bool": {  
> > > > > > > > "must": [  
> > > > > > > > {  
> > > > > > > > "multi\_match": {  
> > > > > > > > "query": "Infiniti",  
> > > > > > > > "fields": [  
> > > > > > > > "title",  
> > > > > > > > "description",  
> > > > > > > > "facets",  
> > > > > > > > "keywords",  
> > > > > > > > "categories",  
> > > > > > > > "hidden"  
> > > > > > > > ],  
> > > > > > > > "operator": "and",  
> > > > > > > > "use\_dis\_max": false  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > ],  
> > > > > > > > "should":   
> > > > > > > > }  
> > > > > > > > },  
> > > > > > > > "filter": {  
> > > > > > > > "and": [  
> > > > > > > > {  
> > > > > > > > "bool": {  
> > > > > > > > "must": [  
> > > > > > > > {  
> > > > > > > > "terms": {  
> > > > > > > > "base\_topic": [  
> > > > > > > > "1",  
> > > > > > > > "2",  
> > > > > > > > "597820",  
> > > > > > > > "599660",  
> > > > > > > > "599661",  
> > > > > > > > "5256322",  
> > > > > > > > "599666",  
> > > > > > > > "1753836",  
> > > > > > > > "599667",  
> > > > > > > > "599669",  
> > > > > > > > "5240682",  
> > > > > > > > "599662",  
> > > > > > > > "5240666",  
> > > > > > > > "599668",  
> > > > > > > > "5240672",  
> > > > > > > > "5326625",  
> > > > > > > > "5256324",  
> > > > > > > > "599663",  
> > > > > > > > "5240669",  
> > > > > > > > "599665",  
> > > > > > > > "1753835"  
> > > > > > > > ]  
> > > > > > > > }  
> > > > > > > > },  
> > > > > > > > {  
> > > > > > > > "term": {  
> > > > > > > > "id\_country": 1  
> > > > > > > > }  
> > > > > > > > },  
> > > > > > > > {  
> > > > > > > > "range": {  
> > > > > > > > "date": {  
> > > > > > > > "from": 1376938800  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > ]  
> > > > > > > > }  
> > > > > > > > },  
> > > > > > > > {  
> > > > > > > > "or": [  
> > > > > > > > {  
> > > > > > > > "geo\_distance": {  
> > > > > > > > "distance": "100km",  
> > > > > > > > "location": {  
> > > > > > > > "lat": 40.6234,  
> > > > > > > > "lon": -74.0288  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > },  
> > > > > > > > {  
> > > > > > > > "missing": {  
> > > > > > > > "field": "location"  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > ]  
> > > > > > > > }  
> > > > > > > > ]  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > },  
> > > > > > > > "filters": ,  
> > > > > > > > "score\_mode": "total"  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > },  
> > > > > > > > "from": 0,  
> > > > > > > > "size": 60  
> > > > > > > > }
> > > > > > > > 
> > > > > > > > On Thursday, September 5, 2013 8:52:51 AM UTC-4, Martijn v  
> > > > > > > > Groningen wrote:
> > > > > > > > 
> > > > > > > > > The query in the email doesn't use the has\_child query, so I don't  
> > > > > > > > > know what you mean with sorting for childs. So in this case you're  
> > > > > > > > > searching for product documents, and from the script you send I see that  
> > > > > > > > > this document type has a location field, so that will just sort any product  
> > > > > > > > > document that matches.
> > > > > > > > > 
> > > > > > > > > Sorting parent document by fields that are in child documents  
> > > > > > > > > isn't supported. There is a work around that uses the custom\_score query in  
> > > > > > > > > the has\_child query, that allows you to psuedo sort parent document by  
> > > > > > > > > fields in child docs:  
> > > > > > > > > [http://www.slideshare.net/\*\*mart\*\*\*\*\*\*ijnvg/document-](http://www.slideshare.net/ **mart****** ijnvg/document-)\*\*  
> > > > > > > > > relationsbbuz20\*\*\*\*\*\*13[http://www.slideshare.net/martijnvg/document-relationsbbuz2013](http://www.slideshare.net/martijnvg/document-relationsbbuz2013)(last slide)
> > > > > > > > > 
> > > > > > > > > On 4 September 2013 23:06, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com)wrote:
> > > > > > > > > 
> > > > > > > > > > can you help me, how i can use correct sort for childs  
> > > > > > > > > > i have query but sort does not work, I know that the field is not  
> > > > > > > > > > correctly specified in the sorting, but how to specify the correctly for  
> > > > > > > > > > sorting for childs
> > > > > > > > > > 
> > > > > > > > > > {  
> > > > > > > > > > "sort": [  
> > > > > > > > > > {  
> > > > > > > > > > "\_geo\_distance": {  
> > > > > > > > > > "location": {  
> > > > > > > > > > "lat": 40.6234,  
> > > > > > > > > > "lon": -74.0288  
> > > > > > > > > > },  
> > > > > > > > > > "order": "asc",  
> > > > > > > > > > "unit": "km"  
> > > > > > > > > > }  
> > > > > > > > > > }  
> > > > > > > > > > ],  
> > > > > > > > > > "fields": ,  
> > > > > > > > > > "query": {  
> > > > > > > > > > "custom\_filters\_score": {  
> > > > > > > > > > "query": {  
> > > > > > > > > > "filtered": {  
> > > > > > > > > > "query": {  
> > > > > > > > > > "bool": {  
> > > > > > > > > > "must": [  
> > > > > > > > > > {  
> > > > > > > > > > "multi\_match": {  
> > > > > > > > > > "query": "Infiniti",  
> > > > > > > > > > "fields": [  
> > > > > > > > > > "title",  
> > > > > > > > > > "description",  
> > > > > > > > > > "facets",  
> > > > > > > > > > "keywords",  
> > > > > > > > > > "categories",  
> > > > > > > > > > "hidden"  
> > > > > > > > > > ],  
> > > > > > > > > > "operator": "and",  
> > > > > > > > > > "use\_dis\_max": false  
> > > > > > > > > > }  
> > > > > > > > > > }  
> > > > > > > > > > ],  
> > > > > > > > > > "should":   
> > > > > > > > > > }  
> > > > > > > > > > },  
> > > > > > > > > > "filter": {  
> > > > > > > > > > "and": [  
> > > > > > > > > > {  
> > > > > > > > > > "bool": {  
> > > > > > > > > > "must": [  
> > > > > > > > > > {  
> > > > > > > > > > "terms": {  
> > > > > > > > > > "base\_topic": [  
> > > > > > > > > > "1",  
> > > > > > > > > > "2",  
> > > > > > > > > > "597820",  
> > > > > > > > > > "599660",  
> > > > > > > > > > "599661",  
> > > > > > > > > > "5256322",  
> > > > > > > > > > "599666",  
> > > > > > > > > > "1753836",  
> > > > > > > > > > "599667",  
> > > > > > > > > > "599669",  
> > > > > > > > > > "5240682",  
> > > > > > > > > > "599662",  
> > > > > > > > > > "5240666",  
> > > > > > > > > > "599668",  
> > > > > > > > > > "5240672",  
> > > > > > > > > > "5326625",  
> > > > > > > > > > "5256324",  
> > > > > > > > > > "599663",  
> > > > > > > > > > "5240669",  
> > > > > > > > > > "599665",  
> > > > > > > > > > "1753835"  
> > > > > > > > > > ]  
> > > > > > > > > > }  
> > > > > > > > > > },  
> > > > > > > > > > {  
> > > > > > > > > > "term": {  
> > > > > > > > > > "id\_country": 1  
> > > > > > > > > > }  
> > > > > > > > > > },  
> > > > > > > > > > {  
> > > > > > > > > > "range": {  
> > > > > > > > > > "date": {  
> > > > > > > > > > "from": 1376938800  
> > > > > > > > > > }  
> > > > > > > > > > }  
> > > > > > > > > > }  
> > > > > > > > > > ]  
> > > > > > > > > > }  
> > > > > > > > > > },  
> > > > > > > > > > {  
> > > > > > > > > > "or": [  
> > > > > > > > > > {  
> > > > > > > > > > "geo\_distance": {  
> > > > > > > > > > "distance": "100km",  
> > > > > > > > > > "location": {  
> > > > > > > > > > "lat": 40.6234,  
> > > > > > > > > > "lon": -74.0288  
> > > > > > > > > > }  
> > > > > > > > > > }  
> > > > > > > > > > },  
> > > > > > > > > > {  
> > > > > > > > > > "missing": {  
> > > > > > > > > > "field": "location"  
> > > > > > > > > > }  
> > > > > > > > > > }  
> > > > > > > > > > ]  
> > > > > > > > > > }  
> > > > > > > > > > ]  
> > > > > > > > > > }  
> > > > > > > > > > }  
> > > > > > > > > > },  
> > > > > > > > > > "filters": ,  
> > > > > > > > > > "score\_mode": "total"  
> > > > > > > > > > }  
> > > > > > > > > > },  
> > > > > > > > > > "from": 0,  
> > > > > > > > > > "size": 60  
> > > > > > > > > > }
> > > > > > > > > > 
> > > > > > > > > > --  
> > > > > > > > > > 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 elasticsearc...@\*\*googlegroups.\*\*c\*\*\*\*om.
> > > > > > > > > > 
> > > > > > > > > > For more options, visit [https://groups.google.com/\*\*grou](https://groups.google.com/**grou)\*\*\*\*\*\*  
> > > > > > > > > > ps/opt\_out [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > > > > > > 
> > > > > > > > > --  
> > > > > > > > > Met vriendelijke groet,
> > > > > > > > > 
> > > > > > > > > Martijn van Groningen
> > > > > > > 
> > > > > > > --  
> > > > > > > Met vriendelijke groet,
> > > > > > > 
> > > > > > > Martijn van Groningen
> > > > > 
> > > > > --  
> > > > > Met vriendelijke groet,
> > > > > 
> > > > > Martijn van Groningen
> > > 
> > > --  
> > > Met vriendelijke groet,
> > > 
> > > Martijn van Groningen
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--  
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:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 6, 2013, 8:00pm UTC](https://discuss.elastic.co/t/parent-child-sort/13466/12 "2013-09-06T20:00:12Z")

</div>

Like I mentioned in my first reply this is a work around and it isn't  
perfect. You're sorting now by score, so the computed distance gets  
transformed from a double into a float value (so its losses precision). I  
forgot the mention that you're sorting in descending order, if you want to  
sort in ascending order you need to change the script to this:  
"-doc["location"].distance(40.6234, -74.0288)"

Also the distance right now is computed in miles, if you want the use km as  
unit then you need to use the 'distanceInKm' function:  
"-doc["location"]. distanceInKm(40.6234, -74.0288)"

I think it makes sense if you use the 'round' function in order to minimize  
the double to float conversion error. More information about scripts and  
functions:

> **[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.

On 6 September 2013 17:07, Max Seleznev [max@inetgiant.com](mailto:max@inetgiant.com) wrote:

> That does not work,  
> full script [es.geo.sort.sh · GitHub](https://gist.github.com/max-inetgiant/9fb4789935b2bc73e772)
> 
> _item 1_
> 
> # _parent_
> 
> {"ok":true,"\_index":"testparentfacets","\_type":"group","\_id":"\*  
> 64a4c4d0cf3ab7bb17e0ba62e7e201cd\*","\_version":1}
> 
> # _child_
> 
> {"ok":true,"\_index":"testparentfacets","\_type":"product","\_id":"\*  
> bca1efe0cd89dc42bfc913f098b67bf3\*","\_version":1}  
> _item 2_
> 
> # _parent_
> 
> {"ok":true,"\_index":"testparentfacets","\_type":"group","\_id":"\*  
> 24237bb1791edbfdf349f9ffcbcaea13\*","\_version":1}
> 
> # _child_
> 
> {"ok":true,"\_index":"testparentfacets","\_type":"product","\_id":"\*  
> f047b7225daa8dd2ab6258f9fed9a3e1\*","\_version":1}
> 
> _this is result with not correct sort, i run search query by parect and  
> sort by location (first query)_ _not good results_
> 
> # {
> 
> # "took" : 2,
> 
> # "timed\_out" : false,
> 
> # "\_shards" : {
> 
> # "total" : 2,
> 
> # "successful" : 2,
> 
> # "failed" : 0
> 
> # },
> 
> # "hits" : {
> 
> # "total" : 2,
> 
> # "max\_score" : null,
> 
> # "hits" : [ {
> 
> # "\_index" : "testparentfacets",
> 
> # "\_type" : "group",
> 
> # "\_id" : "_64a4c4d0cf3ab7bb17e0ba62e7e201cd_",
> 
> # "\_score" : null,
> 
> # "sort" : [1.7976931348623157E308]
> 
> # }, {
> 
> # "\_index" : "testparentfacets",
> 
> # "\_type" : "group",
> 
> # "\_id" : "_24237bb1791edbfdf349f9ffcbcaea13_",
> 
> # "\_score" : null,
> 
> # "sort" : [1.7976931348623157E308]
> 
> # } ]
> 
> # }
> 
> # }
> 
> _this is result with correct sort, i run search query by childs only and  
> sort by location (second query)_ _perfect results_
> 
> # {
> 
> # "took" : 1,
> 
> # "timed\_out" : false,
> 
> # "\_shards" : {
> 
> # "total" : 2,
> 
> # "successful" : 2,
> 
> # "failed" : 0
> 
> # },
> 
> # "hits" : {
> 
> # "total" : 2,
> 
> # "max\_score" : null,
> 
> # "hits" : [ {
> 
> # "\_index" : "testparentfacets",
> 
> # "\_type" : "product",
> 
> # "\_id" : "_f047b7225daa8dd2ab6258f9fed9a3e1_",
> 
> ```
> *parent* *
> 
> ```
> 
> 24237bb1791edbfdf349f9ffcbcaea13\*
> 
> # "\_score" : null,
> 
> # "sort" : [50.236949738919485]
> 
> # }, {
> 
> # "\_index" : "testparentfacets",
> 
> # "\_type" : "product",
> 
> # "\_id" : "_bca1efe0cd89dc42bfc913f098b67bf3_",
> 
> ```
> *parent* *
> 
> ```
> 
> 64a4c4d0cf3ab7bb17e0ba62e7e201cd\*
> 
> # "\_score" : null,
> 
> # "sort" : [56.1165820505235]
> 
> # } ]
> 
> # }
> 
> # }
> 
> _and your example, result is not correct_
> 
> curl -XPOST '[http://10.0.10.223:9200/testparentfacets/\_search?pretty=true](http://10.0.10.223:9200/testparentfacets/_search?pretty=true)'  
> -d '  
> {  
> "fields": ,  
> "query": {  
> "has\_child": {  
> "score\_type": "max",  
> "type": "product",  
> "query": {  
> "custom\_score": {  
> "query": {  
> "match\_all": {}  
> },  
> "script": "doc["location"].distance(40.6234, -74.0288)"  
> }  
> }  
> }  
> }  
> }
> 
> ' && echo
> 
> # {
> 
> # "took" : 2,
> 
> # "timed\_out" : false,
> 
> # "\_shards" : {
> 
> # "total" : 5,
> 
> # "successful" : 5,
> 
> # "failed" : 0
> 
> # },
> 
> # "hits" : {
> 
> # "total" : 2,
> 
> # "max\_score" : 39.39948,
> 
> # "hits" : [ {
> 
> # "\_index" : "testparentfacets",
> 
> # "\_type" : "group",
> 
> # "\_id" : "_64a4c4d0cf3ab7bb17e0ba62e7e201cd_",
> 
> # "\_score" : 39.39948
> 
> # }, {
> 
> # "\_index" : "testparentfacets",
> 
> # "\_type" : "group",
> 
> # "\_id" : "_24237bb1791edbfdf349f9ffcbcaea13_",
> 
> # "\_score" : 33.126003
> 
> # } ]
> 
> # }
> 
> # }
> 
> On Friday, September 6, 2013 9:06:46 AM UTC-4, Martijn v Groningen wrote:
> 
> > The sorting by distance is done inside the script, you should remove the  
> > other sort. The script will output the distance in the \_score field for  
> > each hit.
> > 
> > On 6 September 2013 14:38, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > 
> > > thanks, but sort does not work
> > > 
> > > {  
> > > "sort": [  
> > > {  
> > > "\_geo\_distance": {  
> > > "location": {  
> > > "lat": 40.6234,  
> > > "lon": -74.0288  
> > > },  
> > > "order": "asc",  
> > > "unit": "km"  
> > > }  
> > > }  
> > > ],  
> > > "query": {  
> > > "has\_child": {  
> > > "score\_type": "max",  
> > > "type": "product",  
> > > "query": {  
> > > "custom\_score": {  
> > > "query": {  
> > > "match\_all": {}  
> > > },  
> > > "script": "doc["location"].distance(\*\*40.6234,  
> > > -74.0288)"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > _result_
> > > 
> > > "hits" : {  
> > > "total" : 2,  
> > > "max\_score" : null,  
> > > "hits" : [ {  
> > > "\_index" : "testparentfacets",  
> > > "\_type" : "group",  
> > > "\_id" : " **64a4c4d0cf3ab7bb17e0ba62e7e201** cd",  
> > > "\_score" : null, "\_source" : {"hash":"\*\*  
> > > 64a4c4d0cf3ab7bb17e0ba62e7e201**cd"},  
> > > "sort" : [1.7976931348623157E308]  
> > > }, {  
> > > "\_index" : "testparentfacets",  
> > > "\_type" : "group",  
> > > "\_id" : "24237bb1791edbfdf349f9ffcbcaea13",  
> > > "\_score" : null, "\_source" : {"hash":"**  
> > > 24237bb1791edbfdf349f9ffcbcaea\*\*13"},  
> > > "sort" : [1.7976931348623157E308]  
> > > } ]  
> > > }
> > > 
> > > On Friday, September 6, 2013 8:29:10 AM UTC-4, Martijn v Groningen wrote:
> > > 
> > > > The reason this error occurred was because of quoting issue. This  
> > > > shouldn't cause an error:  
> > > > curl -XPOST 'localhost:9203/ **testparentface** ts/\_search?\*\*pretty=true'  
> > > > -d '{  
> > > > "query": {  
> > > > "has\_child": {  
> > > > "score\_type": "max",  
> > > > "type": "product",  
> > > > "query": {  
> > > > "custom\_score": {  
> > > > "query": {  
> > > > "match\_all": {}  
> > > > },  
> > > > "script": "doc["location"].distance( **40**.6234,  
> > > > -74.0288)"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }'
> > > > 
> > > > On 6 September 2013 13:40, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > > 
> > > > > "RemoteTransportException[[**Nob** le, Peter][inet[/10.0.10.225:9300]\*\*  
> > > > > \*\*][search/phase/query]]; nested: QueryPhaseExecutionException[[**_testparentfacets][0]:  
> > > > > query[ChildrenQuery[product/group](custom score  
> > > > > (ConstantScore(:_),function=script[doc[location].distance(40.6234,  
> > > > > -74.0288)], params [null]))],from[0],size[10]: Query Failed [failed to  
> > > > > execute context rewrite]]; nested: CompileException[[Error: No field found  
> > > > > for [org.elasticsearch.index.fielddata.ScriptDocValues$\*\*GeoPoints  
> > > > > \*\*@34187f94] in mapping with types []]\n[Near : {...  
> > > > > doc[location].distance(40.6234 ....}]\n ^\n[Line: 1, Column:  
> > > > > 1]]; nested: ElasticSearchIllegalArgumentEx**\*\*ception[No field found  
> > > > > for [org.elasticsearch.index. **field** data.ScriptDocValues$\*\*GeoPoints  
> > > > > \*\*@34187f94] in mapping with types ]; "
> > > > > 
> > > > > that does not work too
> > > > > 
> > > > > i attached file with my mapping and search query (look last query)
> > > > > 
> > > > > _Are you sure it's supposed to work?_
> > > > > 
> > > > > On Friday, September 6, 2013 3:23:57 AM UTC-4, Martijn v Groningen  
> > > > > wrote:
> > > > > 
> > > > > > You need to substitute the lat and long variables in the script with  
> > > > > > the actual values:  
> > > > > > "script": "doc['location'].distance(40.\*\*6\*\*\*\*234, -74.0288)"
> > > > > > 
> > > > > > On 5 September 2013 21:16, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > > > > 
> > > > > > > Thanks for idea, but i run your example and have error
> > > > > > > 
> > > > > > > - error: SearchPhaseExecutionException[**Failed to execute  
> > > > > > > phase [query\_fetch], total failure; shardFailures {[8xa0yPbjREmk8DuC\_jlipA][  
> > > > > > > \*\*testparentfacets][0]: RemoteTransportException[[T-Ra**  
> > > > > > > y][inet[/10.0.10.223:9301]][**search/phase/query+fetch]];  
> > > > > > > nested: QueryPhaseExecutionException[[_testparentfacets][0]:  
> > > > > > > query[ChildrenQuery[product/\*\*group](custom score  
> > > > > > > (ConstantScore(:_),function=\*\*script[doc['location'].**  
> > > > > > > distance\*\*\*\*(lat, lon)], params [null]))],from[0],size[10]: _Query  
> > > > > > > Failed [failed to execute context rewrite]]; nested:  
> > > > > > > PropertyAccessException[[Error: unresolvable property or  
> > > > > > > identifier: lat] [Near : {... doc['location'].distance(lat, lon) ....}] ^  
> > > > > > > [Line: 1, Column: 26]]; }]_
> > > > > > > - status: 500
> > > > > > > 
> > > > > > > On Thursday, September 5, 2013 1:21:16 PM UTC-4, Martijn v Groningen  
> > > > > > > wrote:
> > > > > > > 
> > > > > > > > Did you see the last slide of the presentation I mentioned in my  
> > > > > > > > previous reply?  
> > > > > > > > What you need to do is move the sorting part to a custom\_score  
> > > > > > > > query and put that in the has\_child query as inner query. Something like  
> > > > > > > > this:  
> > > > > > > > {  
> > > > > > > > "query": {  
> > > > > > > > "has\_child": {  
> > > > > > > > "score\_type": "max",  
> > > > > > > > "type": "product",  
> > > > > > > > "query": {  
> > > > > > > > "custom\_score": {  
> > > > > > > > "query": {  
> > > > > > > > "match\_all": {}  
> > > > > > > > },  
> > > > > > > > "script": "doc['location'].distance(lat, lon)"  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > }  
> > > > > > > > }
> > > > > > > > 
> > > > > > > > In the above example the sorting by distance has been replaced by  
> > > > > > > > the distance function in the script.
> > > > > > > > 
> > > > > > > > On 5 September 2013 16:29, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com) wrote:
> > > > > > > > 
> > > > > > > > > sorry, I mixed queries.  
> > > > > > > > > I attached file with the request, if you started it then you will  
> > > > > > > > > see two requests  
> > > > > > > > > can you run attached file  
> > > > > > > > > {  
> > > > > > > > > "sort": [  
> > > > > > > > > {  
> > > > > > > > > "\_geo\_distance": {  
> > > > > > > > > "location": {  
> > > > > > > > > "lat": 40.6234,  
> > > > > > > > > "lon": -74.0288  
> > > > > > > > > },  
> > > > > > > > > "order": "asc",  
> > > > > > > > > "unit": "km"  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > ],  
> > > > > > > > > "fields": ,  
> > > > > > > > > "query": {
> > > > > > > > > 
> > > > > > > > > - "has\_child": {\*  
> > > > > > > > > "score\_type": "max",  
> > > > > > > > > "type": "product",  
> > > > > > > > > "query": {  
> > > > > > > > > "custom\_filters\_score": {  
> > > > > > > > > "query": {  
> > > > > > > > > "filtered": {  
> > > > > > > > > "query": {  
> > > > > > > > > "bool": {  
> > > > > > > > > "must": [  
> > > > > > > > > {  
> > > > > > > > > "multi\_match": {  
> > > > > > > > > "query": "Infiniti",  
> > > > > > > > > "fields": [  
> > > > > > > > > "title",  
> > > > > > > > > "description",  
> > > > > > > > > "facets",  
> > > > > > > > > "keywords",  
> > > > > > > > > "categories",  
> > > > > > > > > "hidden"  
> > > > > > > > > ],  
> > > > > > > > > "operator": "and",  
> > > > > > > > > "use\_dis\_max": false  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > ],  
> > > > > > > > > "should":   
> > > > > > > > > }  
> > > > > > > > > },  
> > > > > > > > > "filter": {  
> > > > > > > > > "and": [  
> > > > > > > > > {  
> > > > > > > > > "bool": {  
> > > > > > > > > "must": [  
> > > > > > > > > {  
> > > > > > > > > "terms": {  
> > > > > > > > > "base\_topic": [  
> > > > > > > > > "1",  
> > > > > > > > > "2",  
> > > > > > > > > "597820",  
> > > > > > > > > "599660",  
> > > > > > > > > "599661",  
> > > > > > > > > "5256322",  
> > > > > > > > > "599666",  
> > > > > > > > > "1753836",  
> > > > > > > > > "599667",  
> > > > > > > > > "599669",  
> > > > > > > > > "5240682",  
> > > > > > > > > "599662",  
> > > > > > > > > "5240666",  
> > > > > > > > > "599668",  
> > > > > > > > > "5240672",  
> > > > > > > > > "5326625",  
> > > > > > > > > "5256324",  
> > > > > > > > > "599663",  
> > > > > > > > > "5240669",  
> > > > > > > > > "599665",  
> > > > > > > > > "1753835"  
> > > > > > > > > ]  
> > > > > > > > > }  
> > > > > > > > > },  
> > > > > > > > > {  
> > > > > > > > > "term": {  
> > > > > > > > > "id\_country": 1  
> > > > > > > > > }  
> > > > > > > > > },  
> > > > > > > > > {  
> > > > > > > > > "range": {  
> > > > > > > > > "date": {  
> > > > > > > > > "from": 1376938800  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > ]  
> > > > > > > > > }  
> > > > > > > > > },  
> > > > > > > > > {  
> > > > > > > > > "or": [  
> > > > > > > > > {  
> > > > > > > > > "geo\_distance": {  
> > > > > > > > > "distance": "100km",  
> > > > > > > > > "location": {  
> > > > > > > > > "lat": 40.6234,  
> > > > > > > > > "lon": -74.0288  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > },  
> > > > > > > > > {  
> > > > > > > > > "missing": {  
> > > > > > > > > "field": "location"  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > ]  
> > > > > > > > > }  
> > > > > > > > > ]  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > },  
> > > > > > > > > "filters": ,  
> > > > > > > > > "score\_mode": "total"  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > }  
> > > > > > > > > },  
> > > > > > > > > "from": 0,  
> > > > > > > > > "size": 60  
> > > > > > > > > }
> > > > > > > > > 
> > > > > > > > > On Thursday, September 5, 2013 8:52:51 AM UTC-4, Martijn v  
> > > > > > > > > Groningen wrote:
> > > > > > > > > 
> > > > > > > > > > The query in the email doesn't use the has\_child query, so I  
> > > > > > > > > > don't know what you mean with sorting for childs. So in this case you're  
> > > > > > > > > > searching for product documents, and from the script you send I see that  
> > > > > > > > > > this document type has a location field, so that will just sort any product  
> > > > > > > > > > document that matches.
> > > > > > > > > > 
> > > > > > > > > > Sorting parent document by fields that are in child documents  
> > > > > > > > > > isn't supported. There is a work around that uses the custom\_score query in  
> > > > > > > > > > the has\_child query, that allows you to psuedo sort parent document by  
> > > > > > > > > > fields in child docs:  
> > > > > > > > > > [http://www.slideshare.net/\*\*mart\*\*\*\*\*\*\*\*ijnvg/document-](http://www.slideshare.net/ **mart******** ijnvg/document-)\*\*  
> > > > > > > > > > relationsbbuz20\*\*\*\*\*\*\*\*13[http://www.slideshare.net/martijnvg/document-relationsbbuz2013](http://www.slideshare.net/martijnvg/document-relationsbbuz2013)(last slide)
> > > > > > > > > > 
> > > > > > > > > > On 4 September 2013 23:06, Max Seleznev [m...@inetgiant.com](mailto:m...@inetgiant.com)wrote:
> > > > > > > > > > 
> > > > > > > > > > > can you help me, how i can use correct sort for childs  
> > > > > > > > > > > i have query but sort does not work, I know that the field is  
> > > > > > > > > > > not correctly specified in the sorting, but how to specify the correctly  
> > > > > > > > > > > for sorting for childs
> > > > > > > > > > > 
> > > > > > > > > > > {  
> > > > > > > > > > > "sort": [  
> > > > > > > > > > > {  
> > > > > > > > > > > "\_geo\_distance": {  
> > > > > > > > > > > "location": {  
> > > > > > > > > > > "lat": 40.6234,  
> > > > > > > > > > > "lon": -74.0288  
> > > > > > > > > > > },  
> > > > > > > > > > > "order": "asc",  
> > > > > > > > > > > "unit": "km"  
> > > > > > > > > > > }  
> > > > > > > > > > > }  
> > > > > > > > > > > ],  
> > > > > > > > > > > "fields": ,  
> > > > > > > > > > > "query": {  
> > > > > > > > > > > "custom\_filters\_score": {  
> > > > > > > > > > > "query": {  
> > > > > > > > > > > "filtered": {  
> > > > > > > > > > > "query": {  
> > > > > > > > > > > "bool": {  
> > > > > > > > > > > "must": [  
> > > > > > > > > > > {  
> > > > > > > > > > > "multi\_match": {  
> > > > > > > > > > > "query": "Infiniti",  
> > > > > > > > > > > "fields": [  
> > > > > > > > > > > "title",  
> > > > > > > > > > > "description",  
> > > > > > > > > > > "facets",  
> > > > > > > > > > > "keywords",  
> > > > > > > > > > > "categories",  
> > > > > > > > > > > "hidden"  
> > > > > > > > > > > ],  
> > > > > > > > > > > "operator": "and",  
> > > > > > > > > > > "use\_dis\_max": false  
> > > > > > > > > > > }  
> > > > > > > > > > > }  
> > > > > > > > > > > ],  
> > > > > > > > > > > "should":   
> > > > > > > > > > > }  
> > > > > > > > > > > },  
> > > > > > > > > > > "filter": {  
> > > > > > > > > > > "and": [  
> > > > > > > > > > > {  
> > > > > > > > > > > "bool": {  
> > > > > > > > > > > "must": [  
> > > > > > > > > > > {  
> > > > > > > > > > > "terms": {  
> > > > > > > > > > > "base\_topic": [  
> > > > > > > > > > > "1",  
> > > > > > > > > > > "2",  
> > > > > > > > > > > "597820",  
> > > > > > > > > > > "599660",  
> > > > > > > > > > > "599661",  
> > > > > > > > > > > "5256322",  
> > > > > > > > > > > "599666",  
> > > > > > > > > > > "1753836",  
> > > > > > > > > > > "599667",  
> > > > > > > > > > > "599669",  
> > > > > > > > > > > "5240682",  
> > > > > > > > > > > "599662",  
> > > > > > > > > > > "5240666",  
> > > > > > > > > > > "599668",  
> > > > > > > > > > > "5240672",  
> > > > > > > > > > > "5326625",  
> > > > > > > > > > > "5256324",  
> > > > > > > > > > > "599663",  
> > > > > > > > > > > "5240669",  
> > > > > > > > > > > "599665",  
> > > > > > > > > > > "1753835"  
> > > > > > > > > > > ]  
> > > > > > > > > > > }  
> > > > > > > > > > > },  
> > > > > > > > > > > {  
> > > > > > > > > > > "term": {  
> > > > > > > > > > > "id\_country": 1  
> > > > > > > > > > > }  
> > > > > > > > > > > },  
> > > > > > > > > > > {  
> > > > > > > > > > > "range": {  
> > > > > > > > > > > "date": {  
> > > > > > > > > > > "from": 1376938800  
> > > > > > > > > > > }  
> > > > > > > > > > > }  
> > > > > > > > > > > }  
> > > > > > > > > > > ]  
> > > > > > > > > > > }  
> > > > > > > > > > > },  
> > > > > > > > > > > {  
> > > > > > > > > > > "or": [  
> > > > > > > > > > > {  
> > > > > > > > > > > "geo\_distance": {  
> > > > > > > > > > > "distance": "100km",  
> > > > > > > > > > > "location": {  
> > > > > > > > > > > "lat": 40.6234,  
> > > > > > > > > > > "lon": -74.0288  
> > > > > > > > > > > }  
> > > > > > > > > > > }  
> > > > > > > > > > > },  
> > > > > > > > > > > {  
> > > > > > > > > > > "missing": {  
> > > > > > > > > > > "field": "location"  
> > > > > > > > > > > }  
> > > > > > > > > > > }  
> > > > > > > > > > > ]  
> > > > > > > > > > > }  
> > > > > > > > > > > ]  
> > > > > > > > > > > }  
> > > > > > > > > > > }  
> > > > > > > > > > > },  
> > > > > > > > > > > "filters": ,  
> > > > > > > > > > > "score\_mode": "total"  
> > > > > > > > > > > }  
> > > > > > > > > > > },  
> > > > > > > > > > > "from": 0,  
> > > > > > > > > > > "size": 60  
> > > > > > > > > > > }
> > > > > > > > > > > 
> > > > > > > > > > > --  
> > > > > > > > > > > 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 elasticsearc...@ **googlegroups.c** \*\*om.
> > > > > > > > > > > 
> > > > > > > > > > > For more options, visit [https://groups.google.com/\*\*grou](https://groups.google.com/**grou)\*\*\*\*\*\*\*\*  
> > > > > > > > > > > ps/opt\_out [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > > > > > > > 
> > > > > > > > > > --  
> > > > > > > > > > Met vriendelijke groet,
> > > > > > > > > > 
> > > > > > > > > > Martijn van Groningen
> > > > > > > > 
> > > > > > > > --  
> > > > > > > > Met vriendelijke groet,
> > > > > > > > 
> > > > > > > > Martijn van Groningen
> > > > > > 
> > > > > > --  
> > > > > > Met vriendelijke groet,
> > > > > > 
> > > > > > Martijn van Groningen
> > > > 
> > > > --  
> > > > Met vriendelijke groet,
> > > > 
> > > > Martijn van Groningen
> > 
> > --  
> > Met vriendelijke groet,
> > 
> > Martijn van Groningen

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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:17am UTC](https://discuss.elastic.co/t/parent-child-sort/13466/13 "2017-07-06T02:17:42Z")

</div>


