# Referencing a weight value from array of match under score function

**URL:** <https://discuss.elastic.co/t/referencing-a-weight-value-from-array-of-match-under-score-function/333909>\
**Category:** Elasticsearch\
**Created:** [May 20, 2023, 1:14pm UTC](https://discuss.elastic.co/t/referencing-a-weight-value-from-array-of-match-under-score-function/333909 "2023-05-20T13:14:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harinder\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harinder_singh/32/106262_2.png) [@Harinder\_Singh](https://discuss.elastic.co/u/Harinder_Singh)\
**Post date:** [May 20, 2023, 1:14pm UTC](https://discuss.elastic.co/t/referencing-a-weight-value-from-array-of-match-under-score-function/333909/1 "2023-05-20T13:14:07Z")

</div>

We have a requirement to override default elastic score mechanism and score two documents equally if they have equal number of matches ignoring tf and idf.

Below is my sample index data

```auto
PUT my_index/_doc/5
{
  "affinity": {
    "visitorSegments": [
      "TPRO",
      "LPRO"
    ],
    "category": [
      "kitchen",
      "corridor"
    ]
  },
  "sets": [
    {
      "visitorSegments": "TPRO",
      "category" : "kitchen",
      "engagementScore": "0.5"
    },
    {
      "visitorSegments": "LPRO",
      "category" : "kitchen",
      "engagementScore": "0.4"
    },
    {
      "visitorSegments": "LPRO",
      "category" : "corridor",
      "engagementScore": "0.1"
    }
  ],
  "distinctaffinities": 2
}

PUT my_index/_doc/6
{
  "affinity": {
    "visitorSegments": [
      "TPRO"
    ],
    "category": [
      "kitchen"
    ]
  },
  "distinctaffinities": 2
}

```

Each document is tagged to some visitorSegments and categories. And also an array with possible combinations of values from visitorSegments and category with an enagagementScore.

I am trying the below Query

```auto
GET my_index/_search
{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "function_score": {
            "query": {
              "bool": {
                "minimum_should_match": 1,
                "should": [
                  {
                    "bool": {
                      "must": [
                        {
                          "match_phrase": {
                            "affinity.visitorSegments": "TPRO"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "must": [
                        {
                          "match_phrase": {
                            "affinity.category": "kitchen"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            },
            "functions": [
              {
                "filter": {
                  "match": {
                    "affinity.category": "kitchen"
                  }
                },
                "weight": 3
              },
              {
                "filter": {
                  "match": {
                    "affinity.visitorSegments": "TPRO"
                  }
                },
                "weight": 1
              },
              {
                "filter": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "sets.visitorSegments": "TPRO"
                        }
                      },
                      {
                        "match": {
                          "sets.category": "kitchen"
                        }
                      }
                    ]
                  }
                },
                "weight": //dynamic value for engagementScore for matching entry from sets[]
              }
            ],
            "score_mode": "sum",
            "boost_mode": "replace"
          }
        }
      ]
    }
  }
}

```

I need to get the value engagementScore from the array entry under sets and set that as value to weight under the third filter statement. So my score should be addition of all three weight statement but third weight value needs to be referred from inside the document only something like  
weight : "doc['sets.engagementScore]" and overall expecting score of 3 + 1 + 0.5 = 4.5  
Able to get score for 4, but unable to get the additional 0.5

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [May 20, 2023, 2:41pm UTC](https://discuss.elastic.co/t/referencing-a-weight-value-from-array-of-match-under-score-function/333909/2 "2023-05-20T14:41:25Z")

</div>

Hi @Harinder_Singh

Try use [script\_score](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-script-score). I recommend avoid string to field like "engagementScore": "0.1", is better to be double for avoid cast in script.

```
    {
      "script_score": {
        "script": """ 
        for (item in params._source.sets)
        {
          if(item.visitorSegments == 'TPRO' && item.category == 'kitchen') {
              return Double.parseDouble(item.engagementScore);
          }
        }
        return 0;
        """
      }
    }

```

Full Query:

```auto
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "minimum_should_match": 1,
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "match_phrase": {
                      "affinity.visitorSegments": "TPRO"
                    }
                  }
                  ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "match_phrase": {
                      "affinity.category": "kitchen"
                    }
                  }
                  ]
              }
            }
            ]
        }
      },
      "functions": [
        {
          "filter": {
            "match": {
              "affinity.category": "kitchen"
            }
          },
          "weight": 3
        },
        {
          "filter": {
            "match": {
              "affinity.visitorSegments": "TPRO"
            }
          },
          "weight": 1
        },
        {
          "script_score": {
            "script": """ 
            def sets = params._source.sets;
            for (item in params._source.sets)
            {
              if(item.visitorSegments == 'TPRO' && item.category == 'kitchen') {
                  return Double.parseDouble(item.engagementScore);
              }
            }
            return 0;
            """
          }
        }
        ],
        "score_mode": "sum",
        "boost_mode": "replace"
    }
  }
}

```

Response:

```auto
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 4.5,
    "hits": [
      {
        "_index": "index",
        "_id": "HI-cOYgBkClv0TmBqXNF",
        "_score": 4.5,
        "_source": {
          "affinity": {
            "visitorSegments": [
              "TPRO",
              "LPRO"
            ],
           .....
    ]
  }

```

---

<div class="post-metadata">

**Author:** ![Harinder\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harinder_singh/32/106262_2.png) [@Harinder\_Singh](https://discuss.elastic.co/u/Harinder_Singh)\
**Post date:** [May 20, 2023, 4:37pm UTC](https://discuss.elastic.co/t/referencing-a-weight-value-from-array-of-match-under-score-function/333909/3 "2023-05-20T16:37:44Z")

</div>

Hi @RabBit_BR

Thanks for the prompt reply. I will have documents like below as well

```auto
PUT my_index/_doc/6

{
  "affinity": {
    "visitorSegments": [
      "TPRO"
    ]
  },
“sets”:[
{
“visitorSegments”: “TPRO”,
“engagementScore”: 0.8
}
]
  "distinctaffinities": 1
}

```

We will be going for partial match as well , so this document should also be returned with score of 1.8 because it is missing category.But don’t want to add extra conditions in the if, else under script\_score.

If we modify the lookup under sets array instead of affinity array, can we still get the engagementScore added to final score per document where each set will be tagged differently.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [May 20, 2023, 5:31pm UTC](https://discuss.elastic.co/t/referencing-a-weight-value-from-array-of-match-under-score-function/333909/4 "2023-05-20T17:31:16Z")

</div>

> [@Harinder\_Singh](#):
>
> If we modify the lookup under sets array instead of affinity array, can we still get the engagementScore added to final score per document where each set will be tagged differently.

does this mean that you got the result you expected without script\_score?

> [@Harinder\_Singh](#):
>
> We will be going for partial match as well , so this document should also be returned with score of 1.8 because it is missing category.But don’t want to add extra conditions in the if, else under script\_score.

does this mean that you got the result you expected without script\_score?

Well... if it is not the condition for adding 0.5, you can change the return 0 to 1.8 (which would be the default). I don't know any other way without adding other conditions in the script\_score.

---

<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:** [June 17, 2023, 5:31pm UTC](https://discuss.elastic.co/t/referencing-a-weight-value-from-array-of-match-under-score-function/333909/5 "2023-06-17T17:31:57Z")

</div>

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