# Is it possible to count tokens which matched documents?

**URL:** https://discuss.elastic.co/t/is-it-possible-to-count-tokens-which-matched-documents/267338
**Category:** Elasticsearch
**Created:** [March 16, 2021, 9:15am UTC](https://discuss.elastic.co/t/is-it-possible-to-count-tokens-which-matched-documents/267338 "2021-03-16T09:15:01Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kwang\_hee\_Han](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kwang_hee_han/32/34412_2.png) [@kwang\_hee\_Han](https://discuss.elastic.co/u/kwang_hee_Han)
#### Post date: [March 16, 2021, 9:15am UTC](https://discuss.elastic.co/t/is-it-possible-to-count-tokens-which-matched-documents/267338/1 "2021-03-16T09:15:01Z")

</div>

Hello,

I want to find which term matched on documents that results of query. I tried to using "highlight" but it does responsed with poor results.(I need more digging time to with them)

I want to know alternative way to find which term matched on document result.

Below example my codes, After request , I expect that getting ["Protein-A","Protein-C"] or ["Protein-A"] or ["Protein-C"] matched from document result.

```
GET papers/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "title": [
              "Genetic",
              "Phenotype",
              "Crispher"
            ]

          }
        },
        {
          "terms": {
            "main_content": [
              "Protien-A",
              "Protein-B",
              "Protien-C"
            ]
          }
        }
      ]
    }
  },
  "highlight": {
    "type": "unified",
    "fields": {
      "main_content": {
        "boundary_scanner": "word",
        "fragment_size": 20,
        "number_of_fragments": 10,
        "pre_tags": [
          "<em>"
        ],
        "post_tags": [
          "</em>"
        ]
      }
    }
  }
}
```

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [March 16, 2021, 9:46am UTC](https://discuss.elastic.co/t/is-it-possible-to-count-tokens-which-matched-documents/267338/2 "2021-03-16T09:46:00Z")

</div>

Do you want to count the matches in _all_ documents or just be more specific about where they matched in the top hits returned?

---

<div class="post-metadata">

### Author: ![kwang\_hee\_Han](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kwang_hee_han/32/34412_2.png) [@kwang\_hee\_Han](https://discuss.elastic.co/u/kwang_hee_Han)
#### Post date: [March 17, 2021, 1:27am UTC](https://discuss.elastic.co/t/is-it-possible-to-count-tokens-which-matched-documents/267338/3 "2021-03-17T01:27:24Z")

</div>

I want which term exactly matched on each result documents.

I mean that If search multiple term list("terms query") in document, generally elasticsearch result document didn't include which terms matched on documents.

Specifically, I looking for more result about document which term exactly matched.(Not count information, each document have matched specific term list a must.)

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [March 17, 2021, 8:18am UTC](https://discuss.elastic.co/t/is-it-possible-to-count-tokens-which-matched-documents/267338/4 "2021-03-17T08:18:34Z")

</div>

[This discussion](https://discuss.elastic.co/t/mapping-the-query-to-the-highlight-using-fvh/257836/2) may be relevant then.

---

<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: [April 14, 2021, 8:18am UTC](https://discuss.elastic.co/t/is-it-possible-to-count-tokens-which-matched-documents/267338/5 "2021-04-14T08:18:40Z")

</div>

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