Count matched words

I'm doing something with ES Learning to rank. I want to count how many words in a query matched with a document and take it as a feature (number_of_matched_words) for LTR.
Ex: q = "iphone 11", d = "iphone 11 64gb". In this case, assume that we tokenize by space, then number_of_matched_words = 2.

I can write a query like below in ES to get _score=2

    {
        "query": {
            "bool": {
                "should": [
                    {
                        "constant_score": {
                            "filter": {
                                "term": {
                                    "searchable_name": "iphone"
                                }
                            }
                        }
                    },
                    {
                        "constant_score": {
                            "filter": {
                                "term": {
                                    "searchable_name": "11"
                                }
                            }
                        }
                    }
                ],
                "minimum_should_match": 0
            }
        }
    }

I want to create a feature like that for ES LTR.
PUT /_ltr/_featureset/test_featureset

    {
    "featureset": {
        "features": [
            {
                "name": "number_of_matched_words",
                "params": [
                    "q"
                ],
                "template_language": "mustache",
                "template": {
                    "bool": {
                        "should": [
                            {
                                "constant_score": {
                                    "filter": {
                                        "term": {
                                            "searchable_name": "iphone"
                                        }
                                    }
                                }
                            },
                            {
                                "constant_score": {
                                    "filter": {
                                        "term": {
                                            "searchable_name": "11"
                                        }
                                    }
                                }
                            }
                        ],
                        "minimum_should_match": 0
                    }
                }
            }
        ]
    },
    "validation": {
        "params": {
            "q": "iphone 11"
        },
        "index": "test_index"
    }
}

It's a bit hard to parameterize (I want to change 'iphone' and '11' to some params like {{token_1}} and {{token_2}}) because I don't know how many should clause I have (it depends on the query)

Do we have any query which can do the same thing as should clause above (I'm expecting a query with only one param, i.e: {{q}}) ? Or How I can parameterize my query to use it as a feature.

Thank in advance.

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