# Query by the sum of two or more fields

**URL:** https://discuss.elastic.co/t/query-by-the-sum-of-two-or-more-fields/169682
**Category:** Elasticsearch
**Created:** [February 23, 2019, 8:29pm UTC](https://discuss.elastic.co/t/query-by-the-sum-of-two-or-more-fields/169682 "2019-02-23T20:29:55Z")
**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: [February 23, 2019, 8:29pm UTC](https://discuss.elastic.co/t/query-by-the-sum-of-two-or-more-fields/169682/1 "2019-02-23T20:29:55Z")

</div>

Let's say I have the following mapping with hundreds of millions of docs currently populating the index:

```auto
{
  "properties": {
    "a": { "type": "integer" },
    "b": { "type": "integer" },
    "c": { "type": "integer" }
  }
}

```

I would like to query by the sum of two or more of these fields. It seems that [script query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html) is the way to go, so I put together the following query:

```auto
{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": {
            "lang": "painless",
            "source": "(doc['a'].value + doc['b'].value + doc['c'].value) > params.sum",
            "params": {
              "sum": 20
            }
          }
        }
      }
    }
  }
}

```

However, this is incredibly slow, taking several seconds to complete. Is there a more efficient way to accomplish this?

Thanks

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [February 23, 2019, 8:51pm UTC](https://discuss.elastic.co/t/query-by-the-sum-of-two-or-more-fields/169682/2 "2019-02-23T20:51:52Z")

</div>

You could store the sum in a separate field when you index the document, which will be a lot faster. This way you pay the cost of the calculation once per document at indexing time rather once per document per query.

---

<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: [February 23, 2019, 9:47pm UTC](https://discuss.elastic.co/t/query-by-the-sum-of-two-or-more-fields/169682/3 "2019-02-23T21:47:33Z")

</div>

Thanks for the response. Any other solutions? Fields aren't just limited to a, b, and c - and sum queries could be any combination of those fields.

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [February 23, 2019, 11:32pm UTC](https://discuss.elastic.co/t/query-by-the-sum-of-two-or-more-fields/169682/4 "2019-02-23T23:32:11Z")

</div>

Then it sounds like a script is the best solution even though it, as you have noted, comes with a performance penalty.

---

<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: [March 23, 2019, 11:32pm UTC](https://discuss.elastic.co/t/query-by-the-sum-of-two-or-more-fields/169682/5 "2019-03-23T23:32:18Z")

</div>

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