Hatem Said
Hatem Said

Reputation: 353

use wildcard with Terms in elasticsearch

I wanted to simulate SQL's IN so I used terms filter, but terms does not support wild cards like adding astrisck in "*egypt*".

so how can i achieve the following query?

PS: i am using elastica

{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "country_name": [
              "*egypt*",
              "*italy*"
            ]
          }
        }
      ]
    }
  },
  "sort": [
    {
      "rank": {
        "order": "desc"
      }
    }
  ]
}

Upvotes: 1

Views: 2137

Answers (1)

Ruben Vardanyan
Ruben Vardanyan

Reputation: 1308

terms query does not support wildcards. You can use match or wildcard query instead. If your problem is multiple values to filter you can combine queries inside should, so it will look like this

{
  "query": {
    "bool": {
      "should": [
        {
          "wildcard": {
            "country_name": "*egypt*"
          }
        },
        {
            "wildcard": {
                "country_name": "*italy*"
            }
        }
      ]
    }
  },
  "sort": [
    {
      "rank": {
        "order": "desc"
      }
    }
  ]
}

Upvotes: 2

Related Questions