# Access the matched analysed term in script\_score?

**URL:** <https://discuss.elastic.co/t/access-the-matched-analysed-term-in-script-score/133448>\
**Category:** Elasticsearch\
**Created:** [May 28, 2018, 3:55am UTC](https://discuss.elastic.co/t/access-the-matched-analysed-term-in-script-score/133448 "2018-05-28T03:55:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![NewHere](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@NewHere](https://discuss.elastic.co/u/NewHere)\
**Post date:** [May 28, 2018, 3:55am UTC](https://discuss.elastic.co/t/access-the-matched-analysed-term-in-script-score/133448/1 "2018-05-28T03:55:12Z")

</div>

I am using function\_score to supply my custom script to compute the score, I know I can access the field value via "doc['field'].value", but how do you access the actual matched term?

In fact, what I wanted to achieve is to calculate the score based on the number of characters matched in an analysed field, e.g. for a field with value "Pete Alex", when querying for "Peter" with an edge\_ngram analyzer (i.e. querying "P", "Pe", "Pet", "Pete", "Peter"), I want to return a score of len(matched\_term)/len(search\_term) = len("Pete")/len("Peter") = 4/5 = 0.8.

I can create a script\_score like this:

```
"script_score": {
    "script": {
        "source": "doc['field'].value.length()*1.0/params.term.length()",
        "params": {
            "term": "Peter"
        }
    }
}

```

But it only works if "doc['field'].value" does not have spaces or other delimiters (e.g. if the field is just "Pete" instead of "Pete Alex"). How do I access the matched analysed term (i.e. "Pete" in this case) in script\_score?

---

<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:** [June 25, 2018, 3:55am UTC](https://discuss.elastic.co/t/access-the-matched-analysed-term-in-script-score/133448/2 "2018-06-25T03:55:19Z")

</div>

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