# How to combine all tokens into one?

**URL:** <https://discuss.elastic.co/t/how-to-combine-all-tokens-into-one/143158>\
**Category:** Elasticsearch\
**Created:** [August 6, 2018, 11:54am UTC](https://discuss.elastic.co/t/how-to-combine-all-tokens-into-one/143158 "2018-08-06T11:54:08Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![zouxiang](https://avatars.discourse-cdn.com/v4/letter/z/a4c791/32.png) [@zouxiang](https://discuss.elastic.co/u/zouxiang)\
**Post date:** [August 6, 2018, 11:54am UTC](https://discuss.elastic.co/t/how-to-combine-all-tokens-into-one/143158/1 "2018-08-06T11:54:08Z")

</div>

Hello, I'm trying to find a token filter that can combine all the tokens into one.

For example:

1. the text "bags and shoes" ==\> 3 tokens: "bags" "and" "shoes" (use StandardTokenizer)
2. "bags" "and" "shoes" ==\> "bag" "and" "shoe" (use Porter Stem Token Filter)

then, is there a token filter which can combine "bag", "and", "shoe" into one token: "bag and shoe"?

Or is there any way to analysis the text "bags and shoes" and get a keyword result "bag and shoe"?

---

<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:** [August 6, 2018, 12:05pm UTC](https://discuss.elastic.co/t/how-to-combine-all-tokens-into-one/143158/2 "2018-08-06T12:05:49Z")

</div>

Have a look at [https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-shingle-tokenfilter.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-shingle-tokenfilter.html)

Might be what you are looking for.

---

<div class="post-metadata">

**Author:** ![zouxiang](https://avatars.discourse-cdn.com/v4/letter/z/a4c791/32.png) [@zouxiang](https://discuss.elastic.co/u/zouxiang)\
**Post date:** [August 6, 2018, 12:43pm UTC](https://discuss.elastic.co/t/how-to-combine-all-tokens-into-one/143158/3 "2018-08-06T12:43:06Z")

</div>

Thanks.  
I try to use the shingle filter like this

```auto
GET _analyze
{
  "text": ["bags and shoes"],
  "tokenizer": "standard",
  "filter": [
    "porter_stem",
    {
      "type": "shingle",
      "output_unigrams": false,
      "min_shingle_size": 3,
      "max_shingle_size": 3
    }
  ]
}

```

and get the result :

```auto
{
  "tokens": [
    {
      "token": "bag and shoe",
      "start_offset": 0,
      "end_offset": 14,
      "type": "shingle",
      "position": 0
    }
  ]
}

```

This is the result I want. But this works only when

```auto
min_shingle_size == max_shingle_size == token size 

```

The token size is indeterminate and may differs from each other, so I can't determine the value of min\_shingle\_size and max\_shingle\_size

---

<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:** [August 6, 2018, 12:45pm UTC](https://discuss.elastic.co/t/how-to-combine-all-tokens-into-one/143158/4 "2018-08-06T12:45:03Z")

</div>

Why not indexing then the full content as one single token with a "keyword" type for example and add a subfield which index every single term alone?

See [https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html)

---

<div class="post-metadata">

**Author:** ![zouxiang](https://avatars.discourse-cdn.com/v4/letter/z/a4c791/32.png) [@zouxiang](https://discuss.elastic.co/u/zouxiang)\
**Post date:** [August 6, 2018, 12:54pm UTC](https://discuss.elastic.co/t/how-to-combine-all-tokens-into-one/143158/5 "2018-08-06T12:54:50Z")

</div>

If index with a "keyword" type, the porter\_stem filter may not work correctly

```auto
GET _analyze
{
  "text": ["bags and shoes"],
  "tokenizer": "keyword",
  "filter": [
    "porter_stem"
  ]
}

```

the result:

```auto
{
  "tokens": [
    {
      "token": "bags and sho",
      "start_offset": 0,
      "end_offset": 14,
      "type": "word",
      "position": 0
    }
  ]
} 

```

the token is "bags and sho" , not "bag and shoe"

---

<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:** [August 6, 2018, 1:38pm UTC](https://discuss.elastic.co/t/how-to-combine-all-tokens-into-one/143158/6 "2018-08-06T13:38:34Z")

</div>

> [@zouxiang](#):
>
> If index with a "keyword" type, the porter\_stem filter may not work correctly

Yes. That's true.

BTW why do you want to do this?

> I'm trying to find a token filter that can combine all the tokens into one.

Why not using a phrase search?

---

<div class="post-metadata">

**Author:** ![zouxiang](https://avatars.discourse-cdn.com/v4/letter/z/a4c791/32.png) [@zouxiang](https://discuss.elastic.co/u/zouxiang)\
**Post date:** [August 6, 2018, 2:21pm UTC](https://discuss.elastic.co/t/how-to-combine-all-tokens-into-one/143158/7 "2018-08-06T14:21:06Z")

</div>

Because I want to match all words in this field, but not part of it.  
The document "bags and shoes" should return only when I search **"bags and shoes"** or **"bag and shoe"** ,  
and not return when I search **"bag"** or **"bag and"** or **"and shoe"**

---

<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:** [August 6, 2018, 2:50pm UTC](https://discuss.elastic.co/t/how-to-combine-all-tokens-into-one/143158/8 "2018-08-06T14:50:27Z")

</div>

And if there is a document like "bags and shoes and whatever" it should not be returned either when you search for "bags and shoes", right?

---

<div class="post-metadata">

**Author:** ![zouxiang](https://avatars.discourse-cdn.com/v4/letter/z/a4c791/32.png) [@zouxiang](https://discuss.elastic.co/u/zouxiang)\
**Post date:** [August 6, 2018, 2:51pm UTC](https://discuss.elastic.co/t/how-to-combine-all-tokens-into-one/143158/9 "2018-08-06T14:51:59Z")

</div>

Yes.

---

<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:** [August 6, 2018, 3:06pm UTC](https://discuss.elastic.co/t/how-to-combine-all-tokens-into-one/143158/10 "2018-08-06T15:06:54Z")

</div>

So I don't know. The problem is that you also want to apply a stemmer to all terms.

May be @jpountz has an idea?

---

<div class="post-metadata">

**Author:** ![zouxiang](https://avatars.discourse-cdn.com/v4/letter/z/a4c791/32.png) [@zouxiang](https://discuss.elastic.co/u/zouxiang)\
**Post date:** [August 6, 2018, 3:17pm UTC](https://discuss.elastic.co/t/how-to-combine-all-tokens-into-one/143158/11 "2018-08-06T15:17:46Z")

</div>

Thank you anyway. Maybe I should add a plugin and implement a custom token filter.

---

<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:** [September 3, 2018, 3:18pm UTC](https://discuss.elastic.co/t/how-to-combine-all-tokens-into-one/143158/12 "2018-09-03T15:18:01Z")

</div>

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