Wildcard with exact phrase search using query_string


(Hanan) #1

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


(Mujtaba Hussain) #2

Are the double quotes here deliberate?


(Hanan) #3

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

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

(Jimferenczi) #4

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:

{
    "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


(Hanan) #5

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"]
               }
            }

Wildcard + phrase combination search case insensitive
(Jimferenczi) #6

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


(Hanan) #7

Thanks Jimczi, that confirmed my understanding.


(system) #8

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