# Trouble with query. Tough calculation

**URL:** <https://discuss.elastic.co/t/trouble-with-query-tough-calculation/105309>\
**Category:** Elasticsearch\
**Created:** [October 25, 2017, 11:03pm UTC](https://discuss.elastic.co/t/trouble-with-query-tough-calculation/105309 "2017-10-25T23:03:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MatthewDiamant](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@MatthewDiamant](https://discuss.elastic.co/u/MatthewDiamant)\
**Post date:** [October 25, 2017, 11:03pm UTC](https://discuss.elastic.co/t/trouble-with-query-tough-calculation/105309/1 "2017-10-25T23:03:38Z")

</div>

This is my current query

```
{
  query: {
    function_score: {
      functions: [
        { filter: { match: { foo: "top" } }, weight: 2.0 },
        { filter: { match: { foo: "mid" } }, weight: 1.0 },
        { field_value_factor: { field: "bar.a", missing: 0 } },
        { field_value_factor: { field: "bar.b", missing: 0 } },
        { field_value_factor: { field: "bar.c", missing: 0 } }
      ],
      score_mode: "sum",
      query: {
        filtered: {
          filter: {
            bool: {
              filter: [{ terms: {"bar": ["a", "b", "c"]} }]
            }
          }
        }
      }
    }
  }
}

```

which currently is creating a score by adding either `2.0` or `1.0` depending on `foo`, and also adding the fields of each of the object children of `bar`. This works fine.

It's essentially `foo + bar.a + bar.b + bar.c`

**What I'd like to do is this calculation instead:**  
`foo * ( bar.a + bar.b + bar.c )`

I'm not sure how to structure the query to achieve this. Maybe there should be a subquery that calculates the children of `bar` with `score_mode: "sum"`, and then wrap that somehow with the filter weighting of `foo` and multiply? What is the syntax to do that?

Also, I know that `filtered` queries are deprecated, but when I change it to `bool`, it comes out with a different calculation.

Any help would be amazing. Thank you.

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [October 27, 2017, 5:03pm UTC](https://discuss.elastic.co/t/trouble-with-query-tough-calculation/105309/2 "2017-10-27T17:03:24Z")

</div>

It's not elegant, but I think you'll have to do a `function_score` inside a `function_score`. Something like:

```json
{
  "query: {
    function_score: {
      functions: [
        { filter: { match: { foo: "top" } }, weight: 2.0 },
        { filter: { match: { foo: "mid" } }, weight: 1.0 },
        { filter: {
          function_score: {
            functions: [
              { field_value_factor: { field: "bar.a", missing: 0 } },
              { field_value_factor: { field: "bar.b", missing: 0 } },
              { field_value_factor: { field: "bar.c", missing: 0 } }
            ],
            score_mode: "sum"
          }
        } }
      ],
      score_mode: "multiply",
      query: {
        bool: {
          filter: {
            bool: {
              filter: [{ terms: {"bar": ["a", "b", "c"]} }]
            }
          }
        }
      }
    }
  }
}

```

Note: haven't tested this, just writing off top of my head 🙂

Basically, sum up a/b/c first, then multiply that score by foo.mid and foo.top.

---

<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:** [November 24, 2017, 5:03pm UTC](https://discuss.elastic.co/t/trouble-with-query-tough-calculation/105309/3 "2017-11-24T17:03:36Z")

</div>

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