Constant Score - Multiple matches

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

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:

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:

"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

1 Like

Thats great.

Thank you cbuescher

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