# Workaround for using wild cards in phrases and proximity searches(Elastic Search)

**URL:** https://discuss.elastic.co/t/workaround-for-using-wild-cards-in-phrases-and-proximity-searches-elastic-search/18373
**Category:** Elasticsearch
**Created:** [June 27, 2014, 1:48pm UTC](https://discuss.elastic.co/t/workaround-for-using-wild-cards-in-phrases-and-proximity-searches-elastic-search/18373 "2014-06-27T13:48:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![saud\_ur\_rehman](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@saud\_ur\_rehman](https://discuss.elastic.co/u/saud_ur_rehman)
#### Post date: [June 27, 2014, 1:48pm UTC](https://discuss.elastic.co/t/workaround-for-using-wild-cards-in-phrases-and-proximity-searches-elastic-search/18373/1 "2014-06-27T13:48:33Z")

</div>

Problem:

Recently I wanted to do a proximity search on elastic search index. I  
wanted to search all docs where ‘measles’ and ‘vaccin\*’ were with 25  
characters to each other. Plus I wanted both of them to be in order.

The elastic search proximity search wasn’t an option because of two  
reasons.

1. Proximity search doesn’t support wildcards. e.g (“measles vaccine”)~25  
is supported but (“measles vacci\*”)~25 or (“measle\* vacci\*”) is not  
supported.

2. Proximity search doesn’t check the respect the order of words in phrase  
e.g (“measles vaccine”)~25 and (“vaccine measles”)~25 will give same  
results.

Solution:  
Few examples to resolve this issue using span\_near

1. (“measles vacci\*”)~25  
{  
"query": {  
"span\_near": {  
"clauses": [  
{  
"span\_or": {  
"clauses": [  
{  
"span\_term": {  
"text": "measles"  
}  
}  
]  
}  
},  
{  
"span\_or": {  
"clauses": [  
{  
"span\_multi": {  
"match": {  
"prefix": {  
"text": {  
"value": "vacci"  
}  
}  
}  
}  
}  
]  
}  
}  
],  
"slop": 25,  
"in\_order": "true”,  
"collect\_payloads": "true"  
}  
}  
}

// in\_order can be used to toggle between ordered or unordered.

1. “measle\* vacci\*”  
{  
"query": {  
"span\_near": {  
"clauses": [  
{  
"span\_or": {  
"clauses": [  
{  
"span\_multi": {  
"match": {  
"prefix": {  
"text": {  
"value": "measle"  
}  
}  
}  
}  
}  
]  
}  
},  
{  
"span\_or": {  
"clauses": [  
{  
"span\_multi": {  
"match": {  
"prefix": {  
"text": {  
"value": "vacci"  
}  
}  
}  
}  
}  
]  
}  
}  
],  
"slop": 0,  
"in\_order": "true",  
"collect\_payloads": "true"  
}  
}  
}

2. Grouping. Now lets assume you want to find all docs where (canada OR  
toronto OR “North york”) NEAR (measles OR vaccin\*). And they should be near  
to each other by 30 characters.  
{  
"query": {  
"span\_near": {  
"clauses": [  
{  
"span\_or": {  
"clauses": [  
{  
"span\_near": {  
"clauses": [  
{  
"span\_term": {  
"text": "North"  
}  
},  
{  
"span\_term": {  
"text": "york"  
}  
}  
],  
"slop": 0,  
"in\_order": "true",  
"collect\_payloads": "true"  
}  
},  
{  
"span\_term": {  
"text": "toronto"  
}  
},  
{  
"span\_term": {  
"text": "canada"  
}  
}  
]  
}  
},  
{  
"span\_or": {  
"clauses": [  
{  
"span\_term": {  
"text": "measles"  
}  
},  
{  
"span\_multi": {  
"match": {  
"prefix": {  
"text": {  
"value": "vaccin"  
}  
}  
}  
}  
}  
]  
}  
}  
],  
"slop": 30,  
"in\_order": "false",  
"collect\_payloads": "true"  
}  
}  
}

If any one knows better solution than this one please comment. Any  
suggestions how I can build a parser to take query from user e.g (quick AND  
near(foxes OR rats, toronto OR ontario, 30)) and convert that to elastic  
search span\_near using above workaround. Boolean operators and parenthesis  
precedence is what I am finding hard to handle. Any open source PHP library  
which can help me change user written queries with parenthesis and boolean  
operator to ES filters.

@shay banon, @ steven @uri: any plans to have such operator support in the  
query\_string

References:

[http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/query-dsl-span-near-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/query-dsl-span-near-query.html)

[http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/query-dsl-span-multi-term-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/query-dsl-span-multi-term-query.html)

[http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/query-dsl-span-or-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/query-dsl-span-or-query.html)

--  
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/248757df-b957-4c67-8df7-6ba4efc93623%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/248757df-b957-4c67-8df7-6ba4efc93623%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Kevin\_Brown](https://avatars.discourse-cdn.com/v4/letter/k/73ab20/32.png) [@Kevin\_Brown](https://discuss.elastic.co/u/Kevin_Brown)
#### Post date: [March 17, 2016, 8:44pm UTC](https://discuss.elastic.co/t/workaround-for-using-wild-cards-in-phrases-and-proximity-searches-elastic-search/18373/2 "2016-03-17T20:44:36Z")

</div>

Saud, thanks for your post. Did you by chance figure a good way to convert user written queries into Elasticsearch queries? I'm facing the same issues!

> [@saud\_ur\_rehman](#):
>
> If any one knows better solution than this one please comment. Any  
> suggestions how I can build a parser to take query from user e.g (quick AND  
> near(foxes OR rats, toronto OR ontario, 30)) and convert that to elastic  
> search span\_near using above workaround. Boolean operators and parenthesis  
> precedence is what I am finding hard to handle. Any open source PHP library  
> which can help me change user written queries with parenthesis and boolean  
> operator to ES filters.

---

<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 5, 2017, 11:07pm UTC](https://discuss.elastic.co/t/workaround-for-using-wild-cards-in-phrases-and-proximity-searches-elastic-search/18373/3 "2017-07-05T23:07:28Z")

</div>


