# Prioritize exact match using nGram

**URL:** <https://discuss.elastic.co/t/prioritize-exact-match-using-ngram/15372>\
**Category:** Elasticsearch\
**Created:** [January 23, 2014, 4:07am UTC](https://discuss.elastic.co/t/prioritize-exact-match-using-ngram/15372 "2014-01-23T04:07:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikhil\_Podduturi](https://avatars.discourse-cdn.com/v4/letter/n/d07c76/32.png) [@Nikhil\_Podduturi](https://discuss.elastic.co/u/Nikhil_Podduturi)\
**Post date:** [January 23, 2014, 4:07am UTC](https://discuss.elastic.co/t/prioritize-exact-match-using-ngram/15372/1 "2014-01-23T04:07:18Z")

</div>

Hello All,

I am wondering how to rank exact match higher than the nGram matches.

For example:

search string: _abcd_  
results - abcd, abcde, abcdegl

not in a particular order.

But I want to rank 'abcd' higher than the rest. Is it possible??  
I am adding my mappings, analyzers and query below.

mapping for a particular field:

{  
'type': 'multi\_field',  
'fields': {  
'name': {  
'type': 'string',  
'search\_analyzer': 'search\_analyzer',  
'index\_analyzer': 'index\_analyzer',  
'include\_in\_all': True  
},  
'untouched': {  
'type': 'string',  
'index': 'not\_analyzed',  
'include\_in\_all': False,  
},  
'standard': {  
'type': 'string',  
'analyzer': 'search\_analyzer',  
'include\_in\_all': False  
}  
},  
}

My analyzers:

{  
'settings': {  
'analysis': {  
'filter': {  
'substring': {  
'type': 'nGram',  
'min\_gram': 1,  
'max\_gram': 25  
}  
},  
'analyzer': {  
'index\_analyzer': {  
'type': 'custom',  
'tokenizer': 'standard',  
'filter': [  
'standard',  
'lowercase',  
'asciifolding',  
'substring'  
]  
},  
'search\_analyzer': {  
'type': 'custom',  
'tokenizer': 'standard',  
'filter': [  
'standard',  
'lowercase',  
'asciifolding'  
]  
}  
}  
}  
}  
}

query (I am searching in all fields'\_all')

{  
'query': {  
'query\_string': {  
'query': 'abcd',  
'analyze\_wildcard': True,  
'analyzer': 'search\_analyzer',  
'default\_operator': 'AND'  
}  
},  
}

Thanks,  
Nikhil

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/3bd861ff-d854-45ab-bb4f-471a7d34b8cc%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3bd861ff-d854-45ab-bb4f-471a7d34b8cc%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://discuss.elastic.co/u/Binh_Ly)\
**Post date:** [January 23, 2014, 6:24pm UTC](https://discuss.elastic.co/t/prioritize-exact-match-using-ngram/15372/2 "2014-01-23T18:24:35Z")

</div>

Nikhil,

Try to take a look at the function\_score query. You should be able to  
inject a function with say a term filter and provide an override  
(script\_score) or boost (boost\_factor) to perform the exact match boosting.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On Wednesday, January 22, 2014 11:07:18 PM UTC-5, Nikhil Podduturi wrote:

> Hello All,
> 
> I am wondering how to rank exact match higher than the nGram matches.
> 
> For example:
> 
> search string: _abcd_  
> results - abcd, abcde, abcdegl
> 
> not in a particular order.
> 
> But I want to rank 'abcd' higher than the rest. Is it possible??  
> I am adding my mappings, analyzers and query below.
> 
> mapping for a particular field:
> 
> {  
> 'type': 'multi\_field',  
> 'fields': {  
> 'name': {  
> 'type': 'string',  
> 'search\_analyzer': 'search\_analyzer',  
> 'index\_analyzer': 'index\_analyzer',  
> 'include\_in\_all': True  
> },  
> 'untouched': {  
> 'type': 'string',  
> 'index': 'not\_analyzed',  
> 'include\_in\_all': False,  
> },  
> 'standard': {  
> 'type': 'string',  
> 'analyzer': 'search\_analyzer',  
> 'include\_in\_all': False  
> }  
> },  
> }
> 
> My analyzers:
> 
> {  
> 'settings': {  
> 'analysis': {  
> 'filter': {  
> 'substring': {  
> 'type': 'nGram',  
> 'min\_gram': 1,  
> 'max\_gram': 25  
> }  
> },  
> 'analyzer': {  
> 'index\_analyzer': {  
> 'type': 'custom',  
> 'tokenizer': 'standard',  
> 'filter': [  
> 'standard',  
> 'lowercase',  
> 'asciifolding',  
> 'substring'  
> ]  
> },  
> 'search\_analyzer': {  
> 'type': 'custom',  
> 'tokenizer': 'standard',  
> 'filter': [  
> 'standard',  
> 'lowercase',  
> 'asciifolding'  
> ]  
> }  
> }  
> }  
> }  
> }
> 
> query (I am searching in all fields'\_all')
> 
> {  
> 'query': {  
> 'query\_string': {  
> 'query': 'abcd',  
> 'analyze\_wildcard': True,  
> 'analyzer': 'search\_analyzer',  
> 'default\_operator': 'AND'  
> }  
> },  
> }
> 
> Thanks,  
> Nikhil

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5cdcf8ca-6688-46f5-b897-29a258accc45%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5cdcf8ca-6688-46f5-b897-29a258accc45%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [July 6, 2017, 1:55am UTC](https://discuss.elastic.co/t/prioritize-exact-match-using-ngram/15372/3 "2017-07-06T01:55:07Z")

</div>


