Elastic query DSL: Wildcards in terms filter?

36,455

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.*"
         }
      }
    }
  }
}
Share:
36,455
Mohitt
Author by

Mohitt

Updated on July 18, 2022

Comments

  • Mohitt
    Mohitt almost 2 years

    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?