# Compare two fields in a query

**URL:** <https://discuss.elastic.co/t/compare-two-fields-in-a-query/69575>\
**Category:** Elasticsearch\
**Created:** [December 20, 2016, 1:32pm UTC](https://discuss.elastic.co/t/compare-two-fields-in-a-query/69575 "2016-12-20T13:32:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stylabs](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@stylabs](https://discuss.elastic.co/u/stylabs)\
**Post date:** [December 20, 2016, 1:32pm UTC](https://discuss.elastic.co/t/compare-two-fields-in-a-query/69575/1 "2016-12-20T13:32:21Z")

</div>

I'm trying to compare two fields and ensure one is greater than other. Something like this:  
"doc['key1'].value \> doc['key2'].value"

But this isn't working as scripting is disabled with my provider. Is there any other alternative to this?

---

<div class="post-metadata">

**Author:** ![Glen\_Smith](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glen_smith/32/111656_2.png) [@Glen\_Smith](https://discuss.elastic.co/u/Glen_Smith)\
**Post date:** [December 20, 2016, 4:13pm UTC](https://discuss.elastic.co/t/compare-two-fields-in-a-query/69575/2 "2016-12-20T16:13:24Z")

</div>

There isn't another way to compare two values of the same document as part of your search, no.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [December 20, 2016, 6:29pm UTC](https://discuss.elastic.co/t/compare-two-fields-in-a-query/69575/3 "2016-12-20T18:29:24Z")

</div>

One work-around might be to index the difference of these two fields, and store the result in another field that you can perform range queries against. However that would require reindexing.

---

<div class="post-metadata">

**Author:** ![stylabs](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@stylabs](https://discuss.elastic.co/u/stylabs)\
**Post date:** [December 21, 2016, 6:36am UTC](https://discuss.elastic.co/t/compare-two-fields-in-a-query/69575/4 "2016-12-21T06:36:21Z")

</div>

Why didn't I think of this... Thank you for pointing it 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:** [January 18, 2017, 6:36am UTC](https://discuss.elastic.co/t/compare-two-fields-in-a-query/69575/5 "2017-01-18T06:36:24Z")

</div>

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