# Obtaining matching tokens with \_search

**URL:** <https://discuss.elastic.co/t/obtaining-matching-tokens-with-search/174647>\
**Category:** Elasticsearch\
**Created:** [March 30, 2019, 6:12pm UTC](https://discuss.elastic.co/t/obtaining-matching-tokens-with-search/174647 "2019-03-30T18:12:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fivo](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@fivo](https://discuss.elastic.co/u/fivo)\
**Post date:** [March 30, 2019, 6:12pm UTC](https://discuss.elastic.co/t/obtaining-matching-tokens-with-search/174647/1 "2019-03-30T18:12:40Z")

</div>

Suppose I have the following index:

```auto
PUT test_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "code_tokenizer"
        }
      },
      "tokenizer": {
        "code_tokenizer": {
          "type": "ngram",
          "min_gram": 2,
          "max_gram": 10,
          "token_chars": []
        }
      }
    }
  },
  "mappings": {
    "codeline": {
      "properties" : {
        "line" : {
          "type": "text",
          "fields": {
            "ngram" : {
              "type" : "text",
              "analyzer" : "my_analyzer"
            }
          }
        }
      }
    }
  }
}

```

and added the following line

```auto
POST test_index/codeline/1
{
  "line" : "coding is fun"
}

```

When now searching the index, is it somehow possible to get the tokens that match the search query? For example:

```auto
GET test_index/codeline/_search 
{
  "query": {
    "multi_match": {
      "query": "coding fun",
      "fields": [
        "line",
        "line.ngram"
      ],
      "type": "most_fields"
    }
  }
}

```

would return the document added above plus something along the lines of

```auto
{"tokens":[{"token":"coding","start_offset":0,"end_offset":6,"type":"word","position":0}, {"token":"fun","start_offset":10,"end_offset":13,"type":"word","position":0}]}

```

It does not necessarily need to be the largest matching tokens (although that would be nice), but could also just be the tokens that made up the score of the query.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 30, 2019, 6:46pm UTC](https://discuss.elastic.co/t/obtaining-matching-tokens-with-search/174647/2 "2019-03-30T18:46:09Z")

</div>

Would highlighting feature work for you?

---

<div class="post-metadata">

**Author:** ![fivo](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@fivo](https://discuss.elastic.co/u/fivo)\
**Post date:** [April 2, 2019, 7:14am UTC](https://discuss.elastic.co/t/obtaining-matching-tokens-with-search/174647/3 "2019-04-02T07:14:25Z")

</div>

Yes that kind of works. One more question though, is it possible to keep the spaces at the beginning of the line? So if the original document is something like:

```auto
POST test_index/codeline/1
{
  "line" : " coding is fun"
}

```

then the highlighting feature will return something like:

```auto
"<em>coding</em> is <em>fun</em>" 

```

where I would prefer something like:

```auto
" <em>coding</em> is <em>fun</em>" 

```

Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 2, 2019, 7:50am UTC](https://discuss.elastic.co/t/obtaining-matching-tokens-with-search/174647/4 "2019-04-02T07:50:52Z")

</div>

I don't know.

---

<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 30, 2019, 7:50am UTC](https://discuss.elastic.co/t/obtaining-matching-tokens-with-search/174647/5 "2019-04-30T07:50:55Z")

</div>

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