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

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

{
_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

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

Hi @akhil_reddy.

Your question is similar to Referencing a weight value from array of match under score function.

Thanks @RabBit_BR . I will try implementing this solution

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