opensearch-project / opensearch-py

Python Client for OpenSearch
https://opensearch.org/docs/latest/clients/python/
Apache License 2.0
359 stars 178 forks source link

How to normalize the relevance score #736

Closed eswarthammana closed 6 months ago

eswarthammana commented 6 months ago

Hi, could you please share, if there is a possible approach to normalize the relevance scores., i see an approach for hybrid search, Is there a similar approach for normal match case or match all case.

i tired the following approach but aggregator values are available during the script score of functions.

payload = {
    "size": str(num_records),
    "aggs": {
        "max_score": {
            "max": {
                "script": {
                    "source": "_score"
                }
            }
        },
        "min_score": {
            "min": {
                "script": {
                    "source": "_score"
                }
            }
        }
    },
    "query": {
        "function_score": {
            "query": {
                "match": {
                    "title": query_text,
                }
            },
            "functions": [
                {
                    "script_score": {
                        "script": {
                            "source": "def minScore = params.min_score.value; def maxScore = params.max_score.value; if (maxScore == minScore) {return 0.0;} else {return (_score - minScore) / (maxScore - minScore);}",
                            "params": {
                                "min_score": {
                                    "field": "min_score"  
                                },
                                "max_score": {
                                    "field": "max_score"
                                }
                            }
                        }
                    }
                }
            ],
            "boost_mode": "replace"
        }
    }
}

Thank you

dblock commented 6 months ago

Looks like this is a question about general OpenSearch query and not specifically about the opensearch-py client. I think the best place to ask how to do this (I don't know) is either the #seaerch channel on the public slack or forums.

I am going to close this,

eswarthammana commented 6 months ago

Thank you so much @dblock for taking the time to respond to my query.

I'm currently limited in my access to other sites and don't have additional devices to post on other platforms. As a token of appreciation, if you're interested in quantum computing, I'd like to share my blog with you.