Use _score as tiebreaker only for function_score query

I would like to use the score from full text search as a tie-breaker only for my calculated score.
In other words, I would like to sort by a custom script, and if the score returned by that script is equal for two documents, sort those two documents by full text relevancy.
Currently i'm noramlizing _score between 0 and 1 and using this query:

{
    "_source": false,
    "from": 0,
    "query": {
        "function_score": {
            "boost_mode": "replace",
            "functions": [{
                "script_score": {
                    "script": "if ('val1' == doc['my_field'].value) { return 2; } else if ('val2' == doc['my_field'].value) { return 1; } return 0;"
                },
                "weight": 10
            }, {
                "script_score": {
                    "script": " return 1 - (1 / (_score + 1)) ; "
                }
            }],
            "query": {
                "bool": {
                    "must": {
                        "match": {
                            "_all": {
                                "fuzziness": "AUTO",
                                "prefix_length": 2,
                                "query": "eum ducimus"
                            }
                        }
                    }
                }
            },
            "score_mode": "sum"
        }
    },
    "size": 100
}

Is there a better way to achieve this?

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