Search code examples
javaelasticsearchwildcardaster

How to use term with wildcard in ElasticSearch?


'Terms' and 'Wildcard' is provided by Elasticsearch. 'Terms' is search for multiple OR conditions:

        {
          "terms": {
           "IP": [
              "192.168.100.11",
              "192.168.100.13"
            ]
          }

'Wildcard' is recognized by the * (star):

        {
          "wildcard": {
            "IP": "192.168.*.11"
          }

I want to merge 'wildcard' + 'terms' functions. How can I do that? For example:

        {
          "wildcard": {
           "IP": [
              "192.168.*.11",
              "192.168.*.13"
            ]
          }

Solution

  • You can use bool's should part, I don't think there is a "terms" like query for wildcard and should behaves like an OR:

    {
      "query": {
        "bool": {
          "should": [
            {"wildcard": {"IP": "192.168.*.11"}},
            {"wildcard": {"IP": "192.168.*.13"}}
          ]
        }
      }
    }