Multiple wildcards in one query in elasticsearch
Asked Answered
J

2

9
curl localhost:9200/tweet/posts/_search -d '{
  "query": {
    "and": [
      {
        "wildcard": {
          "_all": "*pet*"
        }
      },
      {
        "wildcard": {
          "_all": "*rom*"
        }
      }
    ]
  }
}'

This gives me a parse exception. I want to run a MySQL like(%test%) type query with an AND condition. Is there any other good way to do in elasticsearch.

Jamshedpur answered 15/5, 2014 at 14:40 Comment(0)
T
16

Maybe something like this?

{
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "_all": {
              "value": "*pet*"
            }
          }
        },
        {
          "wildcard": {
            "_all": {
              "value": "*rom*"
            }
          }
        }
      ]
    }
  }
}
Tweezers answered 15/5, 2014 at 19:0 Comment(2)
if i have to run same query on selected fields instead of _all then how can i do that. I have to run search on these fields only ["name","address","contact","zip"]. Please help me.Jamshedpur
I believe you then need to change the "must" to a "should" and then add one wildcard per field. At least one "should" will need to match a record for it to be returned (if you have no must clause).Tweezers
G
1

dis_max query can support wildcard queries not a bool. Please take a look here: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-dis-max-query.html I would write something like this:

{
 "query": {
     "dis_max": {
         "queries": [
               {"wildcard": {
                 "_all": {"value" : "*pet*"}
               }},
               {"wildcard": {
                 "_all": {"value" : "*rom*"}
               }}
           ]
     }
   }
}
Gomel answered 11/3, 2022 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.