# Wildcard with exact phrase search using query\_string

**URL:** <https://discuss.elastic.co/t/wildcard-with-exact-phrase-search-using-query-string/102631>\
**Category:** Elasticsearch\
**Created:** [October 3, 2017, 11:12pm UTC](https://discuss.elastic.co/t/wildcard-with-exact-phrase-search-using-query-string/102631 "2017-10-03T23:12:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![telebh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/telebh/32/42272_2.png) [@telebh](https://discuss.elastic.co/u/telebh)\
**Post date:** [October 3, 2017, 11:12pm UTC](https://discuss.elastic.co/t/wildcard-with-exact-phrase-search-using-query-string/102631/1 "2017-10-03T23:12:08Z")

</div>

I have issue, I need to run exact search with wildcard using "query\_string". how can i do so? the query looks like this. this query returns "0" results

{

"query":{  
"bool":{  
"must":[  
{  
"match\_phrase":{  
"docproperties.doc\_type":"ipr"  
}  
},  
{  
"match\_phrase": {  
"afdoc.affil": ""imperial college lond?n""  
}  
}

```
     ] 
  } 

```

}  
}

URL:

[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?reply](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?reply)

---

<div class="post-metadata">

**Author:** ![mujtabahussain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mujtabahussain/32/17514_2.png) [@mujtabahussain](https://discuss.elastic.co/u/mujtabahussain)\
**Post date:** [October 4, 2017, 12:44am UTC](https://discuss.elastic.co/t/wildcard-with-exact-phrase-search-using-query-string/102631/2 "2017-10-04T00:44:35Z")

</div>

> [@telebh](#):
>
> ""imperial college lond?n""

Are the double quotes here deliberate?

---

<div class="post-metadata">

**Author:** ![telebh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/telebh/32/42272_2.png) [@telebh](https://discuss.elastic.co/u/telebh)\
**Post date:** [October 4, 2017, 7:07pm UTC](https://discuss.elastic.co/t/wildcard-with-exact-phrase-search-using-query-string/102631/3 "2017-10-04T19:07:08Z")

</div>

> [@telebh](#):
>
> "match\_phrase": {
> 
> "afdoc.affil": ""imperial college lond?n""
> 
> }

ops sorry i meant to escape the double quotes,it should looks like the following

```
"match_phrase": {
"afdoc.affil": "\"imperial college lond?n\""
}

```

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [October 5, 2017, 3:29pm UTC](https://discuss.elastic.co/t/wildcard-with-exact-phrase-search-using-query-string/102631/4 "2017-10-05T15:29:26Z")

</div>

You're using a `match_phrase` query so the quotes are not needed. This query does not handle wildcard so `?` is treated as any other character.  
If you want to perform a wildcard in a phrase query you'll have to use the `span` queries:

```auto
{
    "query": {
        "span_near" : {
            "clauses" : [
                { "span_term" : { "field" : "imperial" } },
                { "span_term" : { "field" : "college" } },
                { "span_multi" : { "match": { "wildcard": { "field" : "lo?don" } } } }
            ],
            "slop" : 0,
            "in_order" : true
        }
    }
}

```

The query above is the equivalent of a phrase query, except that the last term contain a wildcard:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/span-queries.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/span-queries.html)

---

<div class="post-metadata">

**Author:** ![telebh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/telebh/32/42272_2.png) [@telebh](https://discuss.elastic.co/u/telebh)\
**Post date:** [October 5, 2017, 4:04pm UTC](https://discuss.elastic.co/t/wildcard-with-exact-phrase-search-using-query-string/102631/5 "2017-10-05T16:04:17Z")

</div>

that's very helpful, many thanks Jimczi. is there away also run exact phrase search including wildcard using "query\_string"  
for example:

```
{  
               "query_string":{  
                  "query": "\"imperial college lond?n\"",
                  "fields": ["afdoc.affil"]
               }
            }
```

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [October 5, 2017, 4:17pm UTC](https://discuss.elastic.co/t/wildcard-with-exact-phrase-search-using-query-string/102631/6 "2017-10-05T16:17:00Z")

</div>

Nope, only `span` queries are able to run this complex query.

---

<div class="post-metadata">

**Author:** ![telebh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/telebh/32/42272_2.png) [@telebh](https://discuss.elastic.co/u/telebh)\
**Post date:** [October 9, 2017, 4:10pm UTC](https://discuss.elastic.co/t/wildcard-with-exact-phrase-search-using-query-string/102631/7 "2017-10-09T16:10:37Z")

</div>

Thanks Jimczi, that confirmed my understanding.

---

<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:** [November 6, 2017, 4:11pm UTC](https://discuss.elastic.co/t/wildcard-with-exact-phrase-search-using-query-string/102631/8 "2017-11-06T16:11:08Z")

</div>

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