# Substring match in search term order using Elasticsearch

**URL:** <https://discuss.elastic.co/t/substring-match-in-search-term-order-using-elasticsearch/17282>\
**Category:** Elasticsearch\
**Created:** [April 30, 2014, 2:15pm UTC](https://discuss.elastic.co/t/substring-match-in-search-term-order-using-elasticsearch/17282 "2014-04-30T14:15:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kruti\_Shukla](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@Kruti\_Shukla](https://discuss.elastic.co/u/Kruti_Shukla)\
**Post date:** [April 30, 2014, 2:15pm UTC](https://discuss.elastic.co/t/substring-match-in-search-term-order-using-elasticsearch/17282/1 "2014-04-30T14:15:35Z")

</div>

Posted same question on "stackover flow"  
"[http://stackoverflow.com/questions/23244796/substring-match-in-search-term-order-using-elasticsearch](http://stackoverflow.com/questions/23244796/substring-match-in-search-term-order-using-elasticsearch)" but still looking for Answer.

I'm new to elasticsearch

I want to perform substring/partial word match using elastic search. I want  
results to be returned in the perticular order. In order to explain my  
problem I will show you how I create my index, mappings and what are the  
records I use.

_Creating Index and mappings:_

PUT /my\_index1  
{  
"settings": {  
"analysis": {  
"filter": {  
"trigrams\_filter": {  
"type": "ngram",  
"min\_gram": 3,  
"max\_gram": 3  
}  
},  
"analyzer": {  
"trigrams": {  
"type": "custom",  
"tokenizer": "standard",  
"filter": [  
"lowercase",  
"trigrams\_filter"  
]  
}  
}  
}  
},  
"mappings": {  
"my\_type1": {  
"properties": {  
"text": {  
"type": "string",  
"analyzer": "trigrams"  
}  
}  
}  
}  
}

_Bulk record insert:_

POST /my\_index1/my\_type1/\_bulk  
{ "index": { "\_id": 1 }}  
{ "text": "men's shaver" }  
{ "index": { "\_id": 2 }}  
{ "text": "men's foil shaver" }  
{ "index": { "\_id": 3 }}  
{ "text": "men's foil advanced shaver" }  
{ "index": { "\_id": 4 }}  
{ "text": "norelco men's foil advanced shaver" }  
{ "index": { "\_id": 5 }}  
{ "text": "men's shavers" }  
{ "index": { "\_id": 6 }}  
{ "text": "women's shaver" }  
{ "index": { "\_id": 7 }}  
{ "text": "women's foil shaver" }  
{ "index": { "\_id": 8 }}  
{ "text": "women's foil advanced shaver" }  
{ "index": { "\_id": 9 }}  
{ "text": "norelco women's foil advanced shaver" }  
{ "index": { "\_id": 10 }}  
{ "text": "women's shavers" }

_Now, I want to perform search for "en's shaver". I'm searching using  
follwing query:_

POST /my\_index1/my\_type1/\_search  
{  
"query": {  
"match": {  
"text":  
{ "query": "en's shaver",

```
        "minimum_should_match": "100%"

      }
   }

}

```

}

I want results to be in following sequence:

1. men's shaver --\> closest match with following same search keyword  
order "en's shaver
2. women's shaver --\> closest match with following same search keyword  
order "en's shaver
3. men's foil shaver --\> increased distance by 1
4. women's foil shaver --\> increased distance by 1
5. men's foil advanced shaver --\> increased distance by 2
6. women's foil advanced shaver --\> increased distance by 2
7. men's shavers --\> substring match for "shavers"
8. women's shavers --\> substring match for "shavers"

I'm performing following query. It is not giving me result in the order I  
want:

POST /my\_index1/my\_type1/\_search  
{  
"query": {  
"query\_string": {  
"default\_field": "text",  
"query": "men's shaver",  
"minimum\_should\_match": "90%"  
}  
}  
}

--  
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/b7d43a2d-be99-45a5-a2a3-4151dbc52292%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b7d43a2d-be99-45a5-a2a3-4151dbc52292%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ramdev\_Wudali](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ramdev_wudali/32/7894_2.png) [@Ramdev\_Wudali](https://discuss.elastic.co/u/Ramdev_Wudali)\
**Post date:** [April 30, 2014, 7:04pm UTC](https://discuss.elastic.co/t/substring-match-in-search-term-order-using-elasticsearch/17282/2 "2014-04-30T19:04:03Z")

</div>

what happens when you query as you indicated ?

did you try and wildchar query ? Also perhaps an analyzer with the shingle  
token filter  
([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-shingle-tokenfilter.html#analysis-shingle-tokenfilter))  
will work better for your purposes ?

Ramdev

On Wednesday, 30 April 2014 09:15:35 UTC-5, Kruti Shukla wrote:

> Posted same question on "stackover flow"  
> "[Substring match in search term order using Elasticsearch - Stack Overflow](http://stackoverflow.com/questions/23244796/substring-match-in-search-term-order-using-elasticsearch)" but still looking for Answer.
> 
> I'm new to elasticsearch
> 
> I want to perform substring/partial word match using Elasticsearch. I  
> want results to be returned in the perticular order. In order to explain my  
> problem I will show you how I create my index, mappings and what are the  
> records I use.
> 
> _Creating Index and mappings:_
> 
> PUT /my\_index1  
> {  
> "settings": {  
> "analysis": {  
> "filter": {  
> "trigrams\_filter": {  
> "type": "ngram",  
> "min\_gram": 3,  
> "max\_gram": 3  
> }  
> },  
> "analyzer": {  
> "trigrams": {  
> "type": "custom",  
> "tokenizer": "standard",  
> "filter": [  
> "lowercase",  
> "trigrams\_filter"  
> ]  
> }  
> }  
> }  
> },  
> "mappings": {  
> "my\_type1": {  
> "properties": {  
> "text": {  
> "type": "string",  
> "analyzer": "trigrams"  
> }  
> }  
> }  
> }  
> }
> 
> _Bulk record insert:_
> 
> POST /my\_index1/my\_type1/\_bulk  
> { "index": { "\_id": 1 }}  
> { "text": "men's shaver" }  
> { "index": { "\_id": 2 }}  
> { "text": "men's foil shaver" }  
> { "index": { "\_id": 3 }}  
> { "text": "men's foil advanced shaver" }  
> { "index": { "\_id": 4 }}  
> { "text": "norelco men's foil advanced shaver" }  
> { "index": { "\_id": 5 }}  
> { "text": "men's shavers" }  
> { "index": { "\_id": 6 }}  
> { "text": "women's shaver" }  
> { "index": { "\_id": 7 }}  
> { "text": "women's foil shaver" }  
> { "index": { "\_id": 8 }}  
> { "text": "women's foil advanced shaver" }  
> { "index": { "\_id": 9 }}  
> { "text": "norelco women's foil advanced shaver" }  
> { "index": { "\_id": 10 }}  
> { "text": "women's shavers" }
> 
> _Now, I want to perform search for "en's shaver". I'm searching using  
> follwing query:_
> 
> POST /my\_index1/my\_type1/\_search  
> {  
> "query": {  
> "match": {  
> "text":  
> { "query": "en's shaver",
> 
> ```
> "minimum_should_match": "100%"
> 
> }
> }
> 
> }
> 
> ```
> 
> }
> 
> I want results to be in following sequence:
> 
> 1. men's shaver --\> closest match with following same search keyword  
> order "en's shaver
> 2. women's shaver --\> closest match with following same search keyword  
> order "en's shaver
> 3. men's foil shaver --\> increased distance by 1
> 4. women's foil shaver --\> increased distance by 1
> 5. men's foil advanced shaver --\> increased distance by 2
> 6. women's foil advanced shaver --\> increased distance by 2
> 7. men's shavers --\> substring match for "shavers"
> 8. women's shavers --\> substring match for "shavers"
> 
> I'm performing following query. It is not giving me result in the order I  
> want:
> 
> POST /my\_index1/my\_type1/\_search  
> {  
> "query": {  
> "query\_string": {  
> "default\_field": "text",  
> "query": "men's shaver",  
> "minimum\_should\_match": "90%"  
> }  
> }  
> }

--  
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/df570460-9e71-4c4b-9208-c5a7f467cde5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/df570460-9e71-4c4b-9208-c5a7f467cde5%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Kruti\_Shukla](https://avatars.discourse-cdn.com/v4/letter/k/45deac/32.png) [@Kruti\_Shukla](https://discuss.elastic.co/u/Kruti_Shukla)\
**Post date:** [May 1, 2014, 7:36am UTC](https://discuss.elastic.co/t/substring-match-in-search-term-order-using-elasticsearch/17282/3 "2014-05-01T07:36:06Z")

</div>

For following query:  
POST /my\_index1/my\_type1/\_search

{  
"query": {  
"match": {  
"text":  
{ "query": "en's shaver",

```
        "minimum_should_match": "100%"

      }
   }

}

```

}

I'm getting this result set --\>  
women's shaver  
men's foil advanced shaver  
women's foil advanced shaver  
men's foil shaver  
women's foil shaver  
norelco men's foil advanced shaver  
norelco women's foil advanced shaver  
men's shavers

And when I search using below query:

POST /my\_index1/my\_type1/\_search  
{  
"query": {  
"query\_string": {  
"default\_field": "text",  
"query": "men's shaver",  
"minimum\_should\_match": "90%"  
}  
}  
}

I'm getting this result set --\>  
women's shavers  
men's shaver  
women's shaver  
men's foil advanced shaver  
women's foil advanced shaver  
men's foil shaver  
women's foil shaver  
norelco men's foil advanced shaver  
norelco women's foil advanced shaver  
men's shavers

Which is not correct either.

I think shigles will not help when I want to search by substring for  
example "en's" shaver.  
If you think it can help then can you please post some code that I can give  
me above result using shigle?

On Wednesday, April 30, 2014 3:04:03 PM UTC-4, Ramdev Wudali wrote:

> what happens when you query as you indicated ?
> 
> did you try and wildchar query ? Also perhaps an analyzer with the  
> shingle token filter (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-shingle-tokenfilter.html#analysis-shingle-tokenfilter)[http://www.google.com/url?q=http%3A%2F%2Fwww.elasticsearch.org%2Fguide%2Fen%2Felasticsearch%2Freference%2Fcurrent%2Fanalysis-shingle-tokenfilter.html%23analysis-shingle-tokenfilter&sa=D&sntz=1&usg=AFQjCNGpdJVjC70Brm5eG5zM9E3UNELkJQ](http://www.google.com/url?q=http%3A%2F%2Fwww.elasticsearch.org%2Fguide%2Fen%2Felasticsearch%2Freference%2Fcurrent%2Fanalysis-shingle-tokenfilter.html%23analysis-shingle-tokenfilter&sa=D&sntz=1&usg=AFQjCNGpdJVjC70Brm5eG5zM9E3UNELkJQ))  
> will work better for your purposes ?
> 
> Ramdev
> 
> On Wednesday, 30 April 2014 09:15:35 UTC-5, Kruti Shukla wrote:
> 
> > Posted same question on "stackover flow"  
> > "[Substring match in search term order using Elasticsearch - Stack Overflow](http://stackoverflow.com/questions/23244796/substring-match-in-search-term-order-using-elasticsearch)" but still looking for Answer.
> > 
> > I'm new to elasticsearch
> > 
> > I want to perform substring/partial word match using Elasticsearch. I  
> > want results to be returned in the perticular order. In order to explain my  
> > problem I will show you how I create my index, mappings and what are the  
> > records I use.
> > 
> > _Creating Index and mappings:_
> > 
> > PUT /my\_index1  
> > {  
> > "settings": {  
> > "analysis": {  
> > "filter": {  
> > "trigrams\_filter": {  
> > "type": "ngram",  
> > "min\_gram": 3,  
> > "max\_gram": 3  
> > }  
> > },  
> > "analyzer": {  
> > "trigrams": {  
> > "type": "custom",  
> > "tokenizer": "standard",  
> > "filter": [  
> > "lowercase",  
> > "trigrams\_filter"  
> > ]  
> > }  
> > }  
> > }  
> > },  
> > "mappings": {  
> > "my\_type1": {  
> > "properties": {  
> > "text": {  
> > "type": "string",  
> > "analyzer": "trigrams"  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > _Bulk record insert:_
> > 
> > POST /my\_index1/my\_type1/\_bulk  
> > { "index": { "\_id": 1 }}  
> > { "text": "men's shaver" }  
> > { "index": { "\_id": 2 }}  
> > { "text": "men's foil shaver" }  
> > { "index": { "\_id": 3 }}  
> > { "text": "men's foil advanced shaver" }  
> > { "index": { "\_id": 4 }}  
> > { "text": "norelco men's foil advanced shaver" }  
> > { "index": { "\_id": 5 }}  
> > { "text": "men's shavers" }  
> > { "index": { "\_id": 6 }}  
> > { "text": "women's shaver" }  
> > { "index": { "\_id": 7 }}  
> > { "text": "women's foil shaver" }  
> > { "index": { "\_id": 8 }}  
> > { "text": "women's foil advanced shaver" }  
> > { "index": { "\_id": 9 }}  
> > { "text": "norelco women's foil advanced shaver" }  
> > { "index": { "\_id": 10 }}  
> > { "text": "women's shavers" }
> > 
> > _Now, I want to perform search for "en's shaver". I'm searching using  
> > follwing query:_
> > 
> > POST /my\_index1/my\_type1/\_search  
> > {  
> > "query": {  
> > "match": {  
> > "text":  
> > { "query": "en's shaver",
> > 
> > ```
> > "minimum_should_match": "100%"
> > 
> > }
> > }
> > 
> > }
> > 
> > ```
> > 
> > }
> > 
> > I want results to be in following sequence:
> > 
> > 1. men's shaver --\> closest match with following same search keyword  
> > order "en's shaver
> > 2. women's shaver --\> closest match with following same search  
> > keyword order "en's shaver
> > 3. men's foil shaver --\> increased distance by 1
> > 4. women's foil shaver --\> increased distance by 1
> > 5. men's foil advanced shaver --\> increased distance by 2
> > 6. women's foil advanced shaver --\> increased distance by 2
> > 7. men's shavers --\> substring match for "shavers"
> > 8. women's shavers --\> substring match for "shavers"
> > 
> > I'm performing following query. It is not giving me result in the order I  
> > want:
> > 
> > POST /my\_index1/my\_type1/\_search  
> > {  
> > "query": {  
> > "query\_string": {  
> > "default\_field": "text",  
> > "query": "men's shaver",  
> > "minimum\_should\_match": "90%"  
> > }  
> > }  
> > }

--  
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/d0520076-31ec-4e98-97b4-c4d12cb94172%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d0520076-31ec-4e98-97b4-c4d12cb94172%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:32am UTC](https://discuss.elastic.co/t/substring-match-in-search-term-order-using-elasticsearch/17282/4 "2017-07-06T01:32:20Z")

</div>


