Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic query DSL: Wildcards in terms filter?

Tags:

I am trying to filter the documents using terms filter. I am not sure how to introduce wildcards in filter. I tried something like this:

"filter":{   "bool":{        "must":{           "terms":{              "wildcard" :  {                 "aircraft":[                    "a380*"                 ]              }          }       }    } } 

But I get SearchParseException with this. Is there no way to use wildcard within filter framework?

like image 682
Mohitt Avatar asked May 27 '15 05:05

Mohitt


People also ask

What is wildcard search in Elasticsearch?

A wildcard operator is a placeholder that matches one or more characters. For example, the * wildcard operator matches zero or more characters. You can combine wildcard operators with other characters to create a wildcard pattern.

What is query DSL in Elasticsearch?

Think of the Query DSL as an AST (Abstract Syntax Tree) of queries, consisting of two types of clauses: Leaf query clauses. Leaf query clauses look for a particular value in a particular field, such as the match , term or range queries. These queries can be used by themselves.

What is filter in Elasticsearch?

Frequently used filters will be cached automatically by Elasticsearch, to speed up performance. Filter context is in effect whenever a query clause is passed to a filter parameter, such as the filter or must_not parameters in the bool query, the filter parameter in the constant_score query, or the filter aggregation.

How do you do a wildcard in Kibana?

There are two wildcard expressions you can use in Kibana – asterisk (*) and question mark (?). * matches any character sequence (including the empty one) and ? matches single characters. Since these queries are performed across a large number of terms, they can be extremely slow.


1 Answers

The terms filter doesn't support wildcards, queries do, though. Try this query instead

{   "query": {     "bool": {       "must": {         "wildcard": {           "aircraft": "a380*"         }       }     }   } } 

Or if you absolutely need to use filters, you can try the regexp filter, too:

{   "query": {     "filtered": {       "filter": {         "bool": {           "must": {             "regexp": {               "aircraft": "a380.*"             }           }         }       }     }   } } 

UPDATE:

In latest ES versions, use the following query instead since filtered has been removed:

{   "query": {     "bool": {       "filter": {          "regexp": {            "aircraft": "a380.*"          }       }     }   } } 
like image 130
Val Avatar answered Oct 28 '22 23:10

Val