# Sort by dynamic fields and use search\_after

**URL:** <https://discuss.elastic.co/t/sort-by-dynamic-fields-and-use-search-after/86275>\
**Category:** Elasticsearch\
**Created:** [May 18, 2017, 1:53pm UTC](https://discuss.elastic.co/t/sort-by-dynamic-fields-and-use-search-after/86275 "2017-05-18T13:53:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hello](https://avatars.discourse-cdn.com/v4/letter/h/7993a0/32.png) [@hello](https://discuss.elastic.co/u/hello)\
**Post date:** [May 18, 2017, 1:53pm UTC](https://discuss.elastic.co/t/sort-by-dynamic-fields-and-use-search-after/86275/1 "2017-05-18T13:53:52Z")

</div>

Version: 5.2.2

I'm currently sorting by dynamic fields and a timestamp and want to utilize the search\_after property. Unfortunately, for certain fields that are missing the dynamic field, they return "Infinity" or "-Infinity" in the sort property result set. When passing back those Infinity values in any position as a String how ES originally returned it, ES seems to not return any hits.

[Example sort against docs that don't have the dynamic field.](http://imgur.com/a/MLi3P)

Has anyone had to overcome this before?

---

<div class="post-metadata">

**Author:** ![raymyers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raymyers/32/12057_2.png) [@raymyers](https://discuss.elastic.co/u/raymyers)\
**Post date:** [May 19, 2017, 11:16pm UTC](https://discuss.elastic.co/t/sort-by-dynamic-fields-and-use-search-after/86275/2 "2017-05-19T23:16:24Z")

</div>

I believe I'm having a related issue using ES 5.4.0. I have a date field which comes back with sort value 9223372036854776000 when missing. Using that value in search\_after gives the error: No matching token for number\_type [BIG\_INTEGER]

I haven't found a workaround yet. It seems like search\_after currently can't be used when sorting by an optional date field.

Hopefully someone can help determine if there is a fix or documentation update in order.

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [May 31, 2017, 12:17pm UTC](https://discuss.elastic.co/t/sort-by-dynamic-fields-and-use-search-after/86275/3 "2017-05-31T12:17:37Z")

</div>

I answered on the github issue directly:

> <https://github.com/elastic/elasticsearch/issues/24774>

  
@raymyers your problem is different. `9223372036854776000` cannot be encoded as a long (the internal encoding for dates). It seems that your json library messed up ES result because a missing date would return either Long.MAX\_VALUE or Long.MIN\_VALUE. What library are you using to communicate with ES ?

---

<div class="post-metadata">

**Author:** ![raymyers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raymyers/32/12057_2.png) [@raymyers](https://discuss.elastic.co/u/raymyers)\
**Post date:** [May 31, 2017, 7:52pm UTC](https://discuss.elastic.co/t/sort-by-dynamic-fields-and-use-search-after/86275/4 "2017-05-31T19:52:16Z")

</div>

@jimczi, thanks for looking into this. I'm using elasticsearch-ruby 5.0.4. The underlying JSON library is multi\_json 1.12.1. By turning on logging of the HTTP responses, I've confirmed that the Ruby library is changing the value. I can work around it by replacing values greater than 9223372036854775807 when the search\_after array is assembled.

I'll file an issue with the client library. Thanks again!

---

<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:** [June 28, 2017, 8:02pm UTC](https://discuss.elastic.co/t/sort-by-dynamic-fields-and-use-search-after/86275/5 "2017-06-28T20:02:09Z")

</div>

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