# Constant Score - Multiple matches

**URL:** <https://discuss.elastic.co/t/constant-score-multiple-matches/214085>\
**Category:** Elasticsearch\
**Created:** [January 7, 2020, 3:32pm UTC](https://discuss.elastic.co/t/constant-score-multiple-matches/214085 "2020-01-07T15:32:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Gabb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ben_gabb/32/60395_2.png) [@Ben\_Gabb](https://discuss.elastic.co/u/Ben_Gabb)\
**Post date:** [January 7, 2020, 3:32pm UTC](https://discuss.elastic.co/t/constant-score-multiple-matches/214085/1 "2020-01-07T15:32:46Z")

</div>

I have a constant score which looks into a nested field:

```
{
  "from": 0,
  "size": 30,
  "query": {
    "constant_score": {
      "boost": 100,
      "filter": {
        "nested": {
          "query": {
            "term": {
              "field-prop": {
                "value": "xyz"
              }
            }
          },
          "path": "children"
        }
      }
    }
  }
}

```

This works as expected and boosts by 100.

What I'm trying to achieve is boosting by 100 for each of the matching children. so if there are 3 children with field-prop of xyz I want the score to be 300.

is this possible? or can it be achieved another way?

Thanks  
Ben

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [January 7, 2020, 4:53pm UTC](https://discuss.elastic.co/t/constant-score-multiple-matches/214085/2 "2020-01-07T16:53:56Z")

</div>

Hi Ben,

if I understand your ask correctly, I believe you can use `"score_mode" : "sum"` in the nested query and wrap the inner `term` queries on the sub-fields in a constant score. Here's a short example:

```auto
PUT my_index
{
  "mappings": {
    "properties": {
      "user": {
        "type": "nested" 
      }
    }
  }
}

PUT my_index/_doc/1
{
  "group" : "fans",
  "user" : [
    {
      "first" : "john",
      "last" : "smith"
    },
    {
      "first" : "john",
      "last" : "white"
    },
    {
      "first" : "robert",
      "last" : "white"
    }
  ]
}

PUT my_index/_doc/2
{
  "group" : "fans",
  "user" : [
    {
      "first" : "john",
      "last" : "smith"
    },
    {
      "first" : "john",
      "last" : "white"
    },
    {
      "first" : "john",
      "last" : "snow"
    }
  ]
}

POST my_index/_search
{
  "from": 0,
  "size": 30,
  "query": {
    "nested": {
      "score_mode": "sum",
      "query": {
        "constant_score": {
          "boost": 100,
          "filter": { 
          "term": {
            "user.first": {
              "value": "john"
            }
          }}
        }
      },
      "path": "user"
    }
  }
}

```

When I try the above on Elasticsearch 7.5 I get:

```auto
"hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 300.0,
        "_source" : {
          "group" : "fans",
          "user" : [
            {
              "first" : "john",
              "last" : "smith"
            },
            {
              "first" : "john",
              "last" : "white"
            },
            {
              "first" : "john",
              "last" : "snow"
            }
          ]
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 200.0,
        "_source" : {
          "group" : "fans",
          "user" : [
            {
              "first" : "john",
              "last" : "smith"
            },
            {
              "first" : "john",
              "last" : "white"
            },
            {
              "first" : "robert",
              "last" : "white"
            }
          ]
        }
      }
    ]
  }

```

Maybe this is not exactly what you need but you can hopefully work from there...

Cheers

---

<div class="post-metadata">

**Author:** ![Ben\_Gabb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ben_gabb/32/60395_2.png) [@Ben\_Gabb](https://discuss.elastic.co/u/Ben_Gabb)\
**Post date:** [January 8, 2020, 10:32am UTC](https://discuss.elastic.co/t/constant-score-multiple-matches/214085/3 "2020-01-08T10:32:48Z")

</div>

Thats great.

Thank you [cbuescher](https://discuss.elastic.co/u/cbuescher)

---

<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:** [February 5, 2020, 10:32am UTC](https://discuss.elastic.co/t/constant-score-multiple-matches/214085/4 "2020-02-05T10:32:49Z")

</div>

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