# \[5.0.1\]\[Geolocation\] Wrong sorting of elements with search\_after clause

**URL:** <https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885>\
**Category:** Elasticsearch\
**Created:** [November 22, 2016, 3:45pm UTC](https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885 "2016-11-22T15:45:56Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![keyball](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@keyball](https://discuss.elastic.co/u/keyball)\
**Post date:** [November 22, 2016, 3:45pm UTC](https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885/1 "2016-11-22T15:45:56Z")

</div>

Hi together,

Background:  
I'm building a list of paginated entries according to the distance to a lat-lon geo point.

Problem:  
I have a problem with search\_after functionality and the geo location.

First my query:

```
{
    "_source":[
        "id"
    ],
    "query":{
        "bool":{
            "must":{
                "match_all":{
                }
            },
            "filter":{
                "geo_distance":{
                    "distance":"100.000000km",
                    "geopoint.location":{
                        "lat":50.931788,
                        "lon":6.940588
                    }
                }
            }
        }
    },
    "search_after":[
        0,
        "activity#0000000000001"
    ],
    "sort":[
        {
            "_geo_distance":{
                "geopoint.location":{
                    "lat":50.9317882,
                    "lon":6.9405879
                },
                "order":"asc",
                "unit":"km",
                "mode":"min",
                "distance_type":"sloppy_arc"
            }
        },
        "_uid"
    ],
    "track_scores":true,
    "from":0,
    "size":5

}

```

- I've got a result with 5 entries sorted by distance and UID. The last element looks so:

- There is many more elements with the distance of 29.663276081577074km in the query.

After a change into "search\_after":[29,"activity#0000000000011"] I should have the elements AFTER the distance of 29 kms and my unique ID (in this case 0000000000011). But the sorting engine used only the first (distance) criteria and the second seemed to be omitted, resulting in the list of elements with UIDs from 0000000000008-0000000000012 - including the search\_after criteria of 0000000000011.

Now come the question 🙂 I know, I'm a lame in Elasticsearch yet, so I'm asking - it is a bug or an unexpected feature? 🙂 - or what can I do with my query to get the results I want? I tried with ranges, other filters and different search\_after criteria (e.g. timestamp) but I'd got always the results only sorted by the first criteria (distance).

Best regards,  
Luke

---

<div class="post-metadata">

**Author:** ![mainec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mainec/32/5557_2.png) [@mainec](https://discuss.elastic.co/u/mainec)\
**Post date:** [November 23, 2016, 10:36am UTC](https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885/2 "2016-11-23T10:36:45Z")

</div>

Thank you for including the query already. Would be great if you could also post some sample documents to make reproducing your issue easier.

---

<div class="post-metadata">

**Author:** ![keyball](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@keyball](https://discuss.elastic.co/u/keyball)\
**Post date:** [November 23, 2016, 11:55am UTC](https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885/3 "2016-11-23T11:55:17Z")

</div>

Ok, here they go:

1. "search\_after": [0, "activity#0000000000001"] - first page of the search

I search for 5 entries after the distance 0 and UID xxx1:

Results:  
"hits": {  
"total": 10,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "activities",  
"\_type": "activity",  
"\_id": "0000000000002",  
"\_score": 1,  
"\_source": {  
"id": 2  
},  
"sort": [  
8.146888754906811,  
"activity#0000000000002"  
]  
},  
{  
"\_index": "activities",  
"\_type": "activity",  
"\_id": "0000000000008",  
"\_score": 1,  
"\_source": {  
"id": 8  
},  
"sort": [  
29.663276081577074,  
"activity#0000000000008"  
]  
},  
{  
"\_index": "activities",  
"\_type": "activity",  
"\_id": "0000000000009",  
"\_score": 1,  
"\_source": {  
"id": 9  
},  
"sort": [  
29.663276081577074,  
"activity#0000000000009"  
]  
},  
{  
"\_index": "activities",  
"\_type": "activity",  
"\_id": "0000000000010",  
"\_score": 1,  
"\_source": {  
"id": 10  
},  
"sort": [  
29.663276081577074,  
"activity#0000000000010"  
]  
},  
{  
"\_index": "activities",  
"\_type": "activity",  
"\_id": "0000000000011",  
"\_score": 1,  
"\_source": {  
"id": 11  
},  
"sort": [  
29.663276081577074,  
"activity#0000000000011"  
]  
}  
]  
}  
As you can see, I've got 5 results here, sorted by the distance and UID. The last element has the distance 29.663276081577074 and the UID: activity#0000000000011

1. "search\_after": [29, "activity#0000000000011"]  
I'll use the values from the last query, convert the distance into an integer, as I have many results with the same distance, and set the UID to activity#0000000000011.  
Results:  
"hits": {  
"total": 10,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "activities",  
"\_type": "activity",  
"\_id": "0000000000008",  
"\_score": 1,  
"\_source": {  
"id": 8  
},  
"sort": [  
29.663276081577074,  
"activity#0000000000008"  
]  
},  
{  
"\_index": "activities",  
"\_type": "activity",  
"\_id": "0000000000009",  
"\_score": 1,  
"\_source": {  
"id": 9  
},  
"sort": [  
29.663276081577074,  
"activity#0000000000009"  
]  
},  
{  
"\_index": "activities",  
"\_type": "activity",  
"\_id": "0000000000010",  
"\_score": 1,  
"\_source": {  
"id": 10  
},  
"sort": [  
29.663276081577074,  
"activity#0000000000010"  
]  
},  
{  
"\_index": "activities",  
"\_type": "activity",  
"\_id": "0000000000011",  
"\_score": 1,  
"\_source": {  
"id": 11  
},  
"sort": [  
29.663276081577074,  
"activity#0000000000011"  
]  
},  
{  
"\_index": "activities",  
"\_type": "activity",  
"\_id": "0000000000012",  
"\_score": 1,  
"\_source": {  
"id": 12  
},  
"sort": [  
29.663276081577074,  
"activity#0000000000012"  
]  
}  
]  
}  
So, I've got the results from xxx8 to xxx12, but I would expect to get entries after the xxx11 - so the xxx12 should be the first element in a new page. The problem exists even if I let Elasticsearch to generate UIDs - so it shouldn't be the name convention I have chosen. I understood the search\_after feature should search elements with an internal cursor, so the name convention of the UIDs does not matter here. Or am I wrong?

---

<div class="post-metadata">

**Author:** ![keyball](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@keyball](https://discuss.elastic.co/u/keyball)\
**Post date:** [November 23, 2016, 11:55am UTC](https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885/4 "2016-11-23T11:55:51Z")

</div>

I used PHP7 + Elasticsearch-PHP 5.0 in my backend, and Sense-Plugin for Chrome for testing purposes - both provided the same results.  
Regards,  
Luke

---

<div class="post-metadata">

**Author:** ![mainec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mainec/32/5557_2.png) [@mainec](https://discuss.elastic.co/u/mainec)\
**Post date:** [November 23, 2016, 2:09pm UTC](https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885/5 "2016-11-23T14:09:33Z")

</div>

Would you mind posting to full Sense reproduction (including indexing commands)?

---

<div class="post-metadata">

**Author:** ![keyball](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@keyball](https://discuss.elastic.co/u/keyball)\
**Post date:** [November 23, 2016, 2:24pm UTC](https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885/6 "2016-11-23T14:24:18Z")

</div>

This is the full query I'm using:

```
POST activities/activity/_search
{
   "_source": [
      "id"
   ],
   "query": {
      "bool": {
         "must": {
            "match_all": {}
         },
         "filter": {
            "geo_distance": {
               "distance": "100.000000km",
               "geopoint.location": {
                  "lat": 50.931788,
                  "lon": 6.940588
               }
            }
         }
      }
   },
   "search_after": [
      29,
      "activity#0000000000011"
   ],
   "sort": [
      {
         "_geo_distance": {
            "geopoint.location": {
               "lat": 50.9317882,
               "lon": 6.9405879
            },
            "order": "asc",
            "unit": "km",
            "mode": "min",
            "distance_type": "sloppy_arc"
         }
      },
      "_uid"
   ],
   "track_scores": true,
   "from": 0,
   "size": 5
}

```

Should I provide the full response(s) also?

---

<div class="post-metadata">

**Author:** ![keyball](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@keyball](https://discuss.elastic.co/u/keyball)\
**Post date:** [November 23, 2016, 2:26pm UTC](https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885/7 "2016-11-23T14:26:22Z")

</div>

Or should I send the mappings of the index?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 24, 2016, 8:37am UTC](https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885/8 "2016-11-24T08:37:39Z")

</div>

if you could provide a full reproducible example (mapping, indexing documents, query and results), that would be super helpful; if it's more than can fit on a discuss post, please open a [gist](https://gist.github.com/) and paste the link.

---

<div class="post-metadata">

**Author:** ![keyball](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@keyball](https://discuss.elastic.co/u/keyball)\
**Post date:** [November 24, 2016, 9:56am UTC](https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885/9 "2016-11-24T09:56:40Z")

</div>

Ok, here is the gist: [https://gist.github.com/keyball/80672e965e205752a282d3212c3517ff](https://gist.github.com/keyball/80672e965e205752a282d3212c3517ff)

I splitted the data into files. You'll get the mapping, the datasource as SQL dump, 2 queries with results and an PHP array I pass to the bulk method of the Elasticsearch-PHP API

Regards,  
Luke

---

<div class="post-metadata">

**Author:** ![keyball](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@keyball](https://discuss.elastic.co/u/keyball)\
**Post date:** [November 25, 2016, 1:05pm UTC](https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885/10 "2016-11-25T13:05:43Z")

</div>

Hi, could you read the gist?

---

<div class="post-metadata">

**Author:** ![keyball](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@keyball](https://discuss.elastic.co/u/keyball)\
**Post date:** [November 28, 2016, 1:36pm UTC](https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885/11 "2016-11-28T13:36:23Z")

</div>

Hi there, any news?

---

<div class="post-metadata">

**Author:** ![keyball](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@keyball](https://discuss.elastic.co/u/keyball)\
**Post date:** [December 5, 2016, 7:37am UTC](https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885/12 "2016-12-05T07:37:31Z")

</div>

Hi, could you tell me if this issue will be processed someday or should I build a workaround?

---

<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:** [January 2, 2017, 7:37am UTC](https://discuss.elastic.co/t/5-0-1-geolocation-wrong-sorting-of-elements-with-search-after-clause/66885/13 "2017-01-02T07:37:35Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
