# Wildcard Query within a Span

**URL:** <https://discuss.elastic.co/t/wildcard-query-within-a-span/9607>\
**Category:** Elasticsearch\
**Created:** [November 7, 2012, 7:57pm UTC](https://discuss.elastic.co/t/wildcard-query-within-a-span/9607 "2012-11-07T19:57:11Z")\
**Posts on this page:** 4\
**Page:** 2

<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:37pm UTC](https://discuss.elastic.co/t/wildcard-query-within-a-span/9607/21 "2016-03-17T20:37:03Z")

</div>

> [@David\_Janssen](#):
>
> (toto and tata) within/20 (tutu or titi)

David,

Were you able to get your query translated into Michael's query? Would you share how?

---

<div class="post-metadata">

**Author:** ![David\_Janssen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_janssen/32/2028_2.png) [@David\_Janssen](https://discuss.elastic.co/u/David_Janssen)\
**Post date:** [March 18, 2016, 7:42am UTC](https://discuss.elastic.co/t/wildcard-query-within-a-span/9607/22 "2016-03-18T07:42:37Z")

</div>

Hi Kevin,

To convert sophisticated queries into elasticsearch query dsl, I have  
developed (in C++) an custom 'translator'.  
We use a custom boost spirit parser to parse our 'complex' queries and  
generate an boolean query tree object (it was already required by our  
previous search engine) .  
Then I have implemented 'toESQueryDSL' functions for this object to  
generate ElasticSearch DSL. This solution fits with our requirements but  
it could be far away from ours.

Regards  
David

Example :  
As you can see, to handle 'and' operator within proximity sub tree, I use a  
'huge' proximity of 100000 words... not really beautiful but it works.

(toto and tata) within/20 (tutu or titi)

=\> BooleanQueryTree

```
      within/20 BooleanQueryNode
      / \
     / \
   and or BooleanQueryNode(s)
  / \ / \
 / \ / \

```

toto tata tutu titi LeafNode

=\> toESQueryDSL  
{  
"filter": {  
"query": {  
"bool": {  
"must": [{  
"bool": {  
"must": [{  
"span\_near": {  
"clauses": [{  
"span\_near": {  
"clauses": [{  
"span\_term": {  
"content": "toto"  
}  
}, {  
"span\_term": {  
"content": "tata"  
}  
}],  
"slop": 100000,  
"in\_order": false,  
"collect\_payloads": false  
}  
}, {  
"span\_or": {  
"clauses": [{  
"span\_term": {  
"content": "tutu"  
}  
}, {  
"span\_term": {  
"content": "titi"  
}  
}]  
}  
}],  
"slop": 19,  
"in\_order": false,  
"collect\_payloads": false  
}  
}]

```
                 }
             }]
         }
     }
 }

```

}

---

<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 18, 2016, 2:09pm UTC](https://discuss.elastic.co/t/wildcard-query-within-a-span/9607/23 "2016-03-18T14:09:39Z")

</div>

Thank you so much for sharing that, David!

We need to use PHP so this doesn't look too good... I was thinking of writing a custom query parser, did you happen to look at that angle? [http://tastefulcode.com/2014/04/04/building-a-query-parser-for-elasticsearch/](http://tastefulcode.com/2014/04/04/building-a-query-parser-for-elasticsearch/)

---

<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/wildcard-query-within-a-span/9607/24 "2017-07-05T23:07:11Z")

</div>



[Previous page](https://discuss.elastic.co/t/wildcard-query-within-a-span/9607.md?page=1)
