# Find out how many given terms are found in the document

**URL:** <https://discuss.elastic.co/t/find-out-how-many-given-terms-are-found-in-the-document/31590>\
**Category:** Elasticsearch\
**Created:** [October 4, 2015, 8:07am UTC](https://discuss.elastic.co/t/find-out-how-many-given-terms-are-found-in-the-document/31590 "2015-10-04T08:07:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![beowulfenator](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/beowulfenator/32/4905_2.png) [@beowulfenator](https://discuss.elastic.co/u/beowulfenator)\
**Post date:** [October 4, 2015, 8:07am UTC](https://discuss.elastic.co/t/find-out-how-many-given-terms-are-found-in-the-document/31590/1 "2015-10-04T08:07:32Z")

</div>

Hi!

I'm working on a system that's looking for geographical places. Here's the problem I'm having. I'm matching all terms in a query to fields like country and city. Let's say I'm looking for "Cancun, Mexico". I can't use multiterm query rewrite (there are reasons for that), so my query looks like this:

```
{"bool" : {
    "must" : {
        "dis_max" : {
            "queries" : [
                {"term" : { "city" : "Cancun" }},
                {"term" : { "city" : "Mexico" }}
            ]
        }
    },
    "should" : {
        "dis_max" : {
            "queries" : [
                {"term" : { "country" : "Cancun" }},
                {"term" : { "country" : "Mexico" }}
            ]
        }
    }
}}

```

Among my results are two cities: `{"country" : "Mexico", "city" : "Mexico"}` (the capital) and `{"country" : "Mexico", "city" : "Cancun"}`. Term frequencies aside, both locations essentially have the same score, because one in both cases there's a country and a city match.

The actual queries I'm using are a bit more complicated, but the problem is the same: how do I reward documents where more (or all) of the original terms matched? I can pass all terms to the scoring script if necessary, I just don't know how to find out which of those terms belong to the document.

---

<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:** [July 5, 2017, 11:46pm UTC](https://discuss.elastic.co/t/find-out-how-many-given-terms-are-found-in-the-document/31590/2 "2017-07-05T23:46:51Z")

</div>


