# Count matched words

**URL:** https://discuss.elastic.co/t/count-matched-words/269294
**Category:** Elasticsearch
**Created:** [April 6, 2021, 7:51am UTC](https://discuss.elastic.co/t/count-matched-words/269294 "2021-04-06T07:51:33Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ndkmath1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndkmath1/32/86601_2.png) [@ndkmath1](https://discuss.elastic.co/u/ndkmath1)
#### Post date: [April 6, 2021, 7:51am UTC](https://discuss.elastic.co/t/count-matched-words/269294/1 "2021-04-06T07:51:33Z")

</div>

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

```auto
    {
        "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`

```auto
    {
    "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.

---

<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: [May 4, 2021, 7:52am UTC](https://discuss.elastic.co/t/count-matched-words/269294/2 "2021-05-04T07:52:27Z")

</div>

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