# Using value from the returned documents and recalculating the score of the documents

**URL:** <https://discuss.elastic.co/t/using-value-from-the-returned-documents-and-recalculating-the-score-of-the-documents/334154>\
**Category:** Elasticsearch\
**Created:** [May 23, 2023, 8:31pm UTC](https://discuss.elastic.co/t/using-value-from-the-returned-documents-and-recalculating-the-score-of-the-documents/334154 "2023-05-23T20:31:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![akhil\_reddy](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@akhil\_reddy](https://discuss.elastic.co/u/akhil_reddy)\
**Post date:** [May 23, 2023, 8:31pm UTC](https://discuss.elastic.co/t/using-value-from-the-returned-documents-and-recalculating-the-score-of-the-documents/334154/1 "2023-05-23T20:31:58Z")

</div>

Hi,  
I have a use case where I need to perform a search request, then use a value from the returned documents in recalculating the score. Below is the example of returned documents for my search request

```auto
{
_score: 1.7,
_source: {
  "affinity": {
    "visitorSegments": [
      "TPRO",
      "LPRO"
    ],
    "category": [
      "kitchen",
      "corridor"
    ]
  },
 "affinities": ["visitorSegments", "category"],
  "sets": [
    {
      "visitorSegments": "TPRO",
      "category" : "kitchen",
      "engagementScore": "0.5"
    },
    {
      "visitorSegments": "LPRO",
      "category" : "kitchen",
      "engagementScore": "0.4"
    },
    {
      "visitorSegments": "LPRO",
      "category" : "corridor",
      "engagementScore": "0.1"
    }
  ],
  "distinctaffinities": 2
}
}

```

I need to get the correct set from the array 'sets' and add the engagementScore of the this set to the elastic score.

Ex: if the above document is returned when I searched for visitorSegments: LPRO and category:"kitchen", then my total score should be 1.7 + 0.4

if the document is returned when I searched for visitorSegments: LPRO and category:"corridor", then my total score should be 1.7 + 0.1

if the document is returned when I searched for visitorSegments: RTYU and category:"corr", then my total score should be 1.7 + 0 as there is no matching se

How do I filter the sets array to get the correct set and add the score the elastic score in a search request

Below is my query

```auto
GET test-index-2/_search/?size=40
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "filter": [
              {
                "bool": {
                  "should": [
                    {
                      "bool": {
                        "must": [
                          {
                            "term": {
                              "affinity.category": "kitchen"
                            }
                          }
                        ]
                      }
                    },
                    {
                      "bool": {
                        "must_not": [
                          {
                            "exists": {
                              "field": "affinity.category"
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "bool": {
                        "must": [
                          {
                            "term": {
                              "affinity.visitorSegments": "LPRO"
                            }
                          }
                        ]
                      }
                    },
                    {
                      "bool": {
                        "must_not": [
                          {
                            "exists": {
                              "field": "affinity.visitorSegments"
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ],
            "must": [
              {
                "bool": {
                  "boost": 2,
                  "minimum_should_match": 1,
                  "should": [
                    {
                      "bool": {
                        "must": [
                          {
                            "constant_score": {
                              "filter": {
                                "match_phrase": {
                                  "affinity.category": "kitchen"
                                }
                              },
                              "boost": 3.2
                            }
                          }
                        ]
                      }
                    },
                    {
                      "bool": {
                        "must": [
                          {
                            "constant_score": {
                              "filter": {
                                 "match_phrase": {
                              "affinity.visitorSegments": "LPRO"
                            }
                              },
                              "boost": 9.2
                            }
                           
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 1,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "lastUpdated": {
        "order": "desc"
      }
    }
  ]
}

```

Thanks

---

<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 24, 2023, 2:11am UTC](https://discuss.elastic.co/t/using-value-from-the-returned-documents-and-recalculating-the-score-of-the-documents/334154/2 "2023-05-24T02:11:03Z")

</div>

Hi @akhil_reddy.

Your question is similar to [Referencing a weight value from array of match under score function](https://discuss.elastic.co/t/referencing-a-weight-value-from-array-of-match-under-score-function/333909).

---

<div class="post-metadata">

**Author:** ![akhil\_reddy](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@akhil\_reddy](https://discuss.elastic.co/u/akhil_reddy)\
**Post date:** [May 24, 2023, 4:10am UTC](https://discuss.elastic.co/t/using-value-from-the-returned-documents-and-recalculating-the-score-of-the-documents/334154/3 "2023-05-24T04:10:01Z")

</div>

> [@RabBit\_BR](#):
>
> Referencing a weight value from array of match under score function

Thanks @RabBit_BR . I will try implementing this solution

---

<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 21, 2023, 4:10am UTC](https://discuss.elastic.co/t/using-value-from-the-returned-documents-and-recalculating-the-score-of-the-documents/334154/4 "2023-06-21T04:10:02Z")

</div>

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