# ElasticSearch query for most relevant documents

**URL:** <https://discuss.elastic.co/t/elasticsearch-query-for-most-relevant-documents/142276>\
**Category:** Elasticsearch\
**Created:** [July 31, 2018, 5:36am UTC](https://discuss.elastic.co/t/elasticsearch-query-for-most-relevant-documents/142276 "2018-07-31T05:36:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vidya\_Lalitha\_S\_K](https://avatars.discourse-cdn.com/v4/letter/v/ecae2f/32.png) [@Vidya\_Lalitha\_S\_K](https://discuss.elastic.co/u/Vidya_Lalitha_S_K)\
**Post date:** [July 31, 2018, 5:36am UTC](https://discuss.elastic.co/t/elasticsearch-query-for-most-relevant-documents/142276/1 "2018-07-31T05:36:47Z")

</div>

I have these documents  
{  
"\_id" : "1",  
"name" : "Nopales",  
}  
{  
"\_id" : "2",  
"name" : "ginger ale",  
}  
{  
"\_id" : "3",  
"name" : "Kale",  
}  
{  
"\_id" : "4",  
"name" : "Triticale Flour Whole Grain",  
}

and the request is to find the matching words with the word "ale" is,  
GET /\_search  
{  
"query": {  
"query\_string" : {  
"default\_field" : "name", "query" : "(ale) OR (_ale) OR(ale)OR(ale_)"  
}  
}  
}

In ElasticSearch, I tried to makeit search for a part of a word. My example data is

{  
"\_id" : "1",  
"name" : "Nopales",  
}  
{  
"\_id" : "2",  
"name" : "ginger ale",  
}  
{  
"\_id" : "3",  
"name" : "Kale",  
}  
{  
"\_id" : "4",  
"name" : "Triticale Flour Whole Grain",  
}

and the request is to find the matching words with the word "ale is"

GET /\_search  
{  
"query": {  
"query\_string" : {"default\_field" : "name", "query" : "_ale_"}  
}

}

and it returns the results like

Kale, Triticale Flour Whole Grain, ginger ale, Nopales

but the best match is ginger ale, which contains the exact word, then kale, then nopales based on word counts then Triticale Flour Whole Grain. Anyone have idea how to achieve this?

---

<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 31, 2018, 6:51am UTC](https://discuss.elastic.co/t/elasticsearch-query-for-most-relevant-documents/142276/2 "2018-07-31T06:51:22Z")

</div>

You can combine 2 queries within a should clause of a bool query:

- Full term match
- Partial term match

If needed, boost the full term one.

Full example here

> <https://gist.github.com/dadoonet/5179ee72ecbf08f12f53d4bda1b76bab>

---

<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 28, 2018, 6:51am UTC](https://discuss.elastic.co/t/elasticsearch-query-for-most-relevant-documents/142276/3 "2018-08-28T06:51:30Z")

</div>

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