# 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/18361
**Category:** Elasticsearch
**Created:** [June 27, 2014, 1:23am UTC](https://discuss.elastic.co/t/workaround-for-using-wild-cards-in-phrases-and-proximity-searches-elastic-search/18361 "2014-06-27T01:23:33Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![saud\_rehman](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@saud\_rehman](https://discuss.elastic.co/u/saud_rehman)
#### Post date: [June 27, 2014, 1:23am UTC](https://discuss.elastic.co/t/workaround-for-using-wild-cards-in-phrases-and-proximity-searches-elastic-search/18361/1 "2014-06-27T01:23: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)

---

<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:19am UTC](https://discuss.elastic.co/t/workaround-for-using-wild-cards-in-phrases-and-proximity-searches-elastic-search/18361/2 "2017-07-06T01:19:24Z")

</div>


