How do I query using Wildcard


#1

Hi all,

I have a query that I've got working nicely but returns no data if I use a wildcard.

Do I have to enable wildcards somewhere or should I be using the wildcard query instead?

If I need to use the wildcard query then I need to start detecting wildcards in the user's search phrase and then alter the query - I don't like the idea of that so any help appreciated...?


(Mark Walkom) #2

What does the query look like?


#3

The simplified query below works

GET contacts/_search
{
    "query": {
      "bool": {
        "should": [ 
          {"nested": {
            "path": "organisation",
            "query": {
              "bool": {
                "should": [
                    {"multi_match": {
                  "query": "te",
                  "fields": ["organisation.name"],
                  "type": "best_fields"
                }},
                {"nested": {
                  "path": "screenData",
                  "query": {
                    "multi_match": {
                      "query": "advertising",
                      "fields": ["screenData.valueString"],
                      "type": "best_fields"
                    }
                  }
                }}
                ]
              }
            }
          }
        }]
      } 
    }
}

but a wildcarded equivalent doesn't..

GET contacts/_search
{
    "query": {
      "bool": {
        "should": [ 
          {"nested": {
            "path": "organisation",
            "query": {
              "bool": {
                "should": [
                    {"multi_match": {
                  "query": "te",
                  "fields": ["organisation.name"],
                  "type": "best_fields"
                }},
                {"nested": {
                  "path": "screenData",
                  "query": {
                    "multi_match": {
                      "query": "advertis*",
                      "fields": ["screenData.valueString"],
                      "type": "best_fields"
                    }
                  }
                }}
                ]
              }
            }
          }
        }]
      } 
    }
}

I have the screenData.valueString field analysed with the english analyser but have tried standard and not_analysed with the same results: no records.


#4

Anyone got any ideas how I can avoid having to use a specific wildcard query - I thought I could just use wildcards in all query types?

Is there a setting I need to enable somewhere?


(Dan Tuffery) #5

The match and multi_match queries do not support wildcards. In general you should avoid using wildcards in your queries as they are not efficient. A more efficient solution would be to index the fields using ngrams.

If you can't use ngrams and need to use a wildcard query this will do what you want:

{
  "query":{
	"bool":{
	  "should":[
		{
		  "nested":{
			"path":"organisation",
			"query":{
			    "multi_match":{
				"query":"te",
				"fields":[
				  "organisation.name"
				],
				"type":"best_fields"
			    }
			}
		  }
		},
		{
		  "nested":{
		      "path":"screenData",
		         "query":{
			     "wildcard":{
				"screenData.valueString":"advertis*"
			     }
			 }
		     } 
		}
	    ]
	}
    }
}

#6

OK, thanks - that's a shame as I was wanting to allow "power-users" the ability to use wildcards and phrase searching with double-quotes etc... a la Google.

So its a detect & query rewrite then - pity!


(system) #7