# Get documents that have a field with a value prefixing a specified string

**URL:** <https://discuss.elastic.co/t/get-documents-that-have-a-field-with-a-value-prefixing-a-specified-string/92789>\
**Category:** Elasticsearch\
**Created:** [July 12, 2017, 9:34am UTC](https://discuss.elastic.co/t/get-documents-that-have-a-field-with-a-value-prefixing-a-specified-string/92789 "2017-07-12T09:34:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![igbayilola](https://avatars.discourse-cdn.com/v4/letter/i/76d3ee/32.png) [@igbayilola](https://discuss.elastic.co/u/igbayilola)\
**Post date:** [July 12, 2017, 9:34am UTC](https://discuss.elastic.co/t/get-documents-that-have-a-field-with-a-value-prefixing-a-specified-string/92789/1 "2017-07-12T09:34:41Z")

</div>

Hello Everybody,  
I'm looking for a way to get document that have a field which value prefixing a specified string. For a better comprehension, see the following example:

**Elasticsearch Document content**  
CountryCode:33, CountryName:France  
CountryCode:34, CountryName:Spain  
CountryCode:39, CountryName:France  
CountryCode:216, CountryName:Tunisia  
CountryCode:212, CountryName:Morocco

A query with " **33** 652729381" will give the line with France as CountryName.

Regards,

---

<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:** [July 12, 2017, 11:12am UTC](https://discuss.elastic.co/t/get-documents-that-have-a-field-with-a-value-prefixing-a-specified-string/92789/2 "2017-07-12T11:12:25Z")

</div>

It's a question of analysis.

When you search, you want to apply a pattern which transform `33652729381` to:

- `3`
- `33`
- `336`
- `3365`
- `33652`
- ...

Then you want to compare that with what has been indexed in the `CountryCode` field and get back that document.

Which means to me:

- Using a `keyword` analyzer at index time: `"analyzer": "keyword"`
- Using a [edge ngram](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/analysis-edgengram-tokenizer.html) based analyzer at search time: `"search_analyzer":"my_ngram_analyzer`"

HTH

---

<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:** [August 9, 2017, 11:12am UTC](https://discuss.elastic.co/t/get-documents-that-have-a-field-with-a-value-prefixing-a-specified-string/92789/3 "2017-08-09T11:12:43Z")

</div>

Closed on August 9, 2017, 11:12am UTC

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