# New field as N-gram of another field

**URL:** <https://discuss.elastic.co/t/new-field-as-n-gram-of-another-field/147382>\
**Category:** Elasticsearch\
**Created:** [September 5, 2018, 11:08am UTC](https://discuss.elastic.co/t/new-field-as-n-gram-of-another-field/147382 "2018-09-05T11:08:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![programagor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/programagor/32/40183_2.png) [@programagor](https://discuss.elastic.co/u/programagor)\
**Post date:** [September 5, 2018, 11:08am UTC](https://discuss.elastic.co/t/new-field-as-n-gram-of-another-field/147382/1 "2018-09-05T11:08:11Z")

</div>

Greetings

I have a field called `hdr_subject` and I would like to create a new field such as `hdr_subject_ngram`, which I would like to be an array of all word N-grams of the field `hdr_subject`, up to certain size (e.g. 4).  
For example, if

```
"hdr_subject" : "Discount: RX Viagra Pills"

```

then I would like

```
"hdr_subject_ngram" : [
  "discount",
  "rx",
  "viagra",
  "pills",
  "discount rx",
  "rx viagra",
  "viagra pills",
  "discount rx viagra",
  "rx viagra pills",
  "discount rx viagra pills"
]

```

This N-gram field will be indexed as a keyword, and used as an influencer for a Machine Learning job.

So far, I found [https://stackoverflow.com/questions/27387231](https://stackoverflow.com/questions/27387231) to be of help, but I'm not sure how to create a new field out of the analyzer. Do you have any pointers?

Thanks

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [September 7, 2018, 9:17am UTC](https://discuss.elastic.co/t/new-field-as-n-gram-of-another-field/147382/2 "2018-09-07T09:17:29Z")

</div>

I think what you are looking for might be [multi fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html). You continue to index the existing `"hdr_subject"`field but set up an additional custom analyzer that uses the [ngram tokenizer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-ngram-tokenizer.html) and use that one on a multi field of the original one.

---

<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:** [October 5, 2018, 9:17am UTC](https://discuss.elastic.co/t/new-field-as-n-gram-of-another-field/147382/3 "2018-10-05T09:17:40Z")

</div>

Closed on October 5, 2018, 9:17am UTC

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