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.

Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
user2742004
  • 91
  • 1
  • 1
  • 2

2 Answers2

16

Maybe something like this?

{
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "_all": {
              "value": "*pet*"
            }
          }
        },
        {
          "wildcard": {
            "_all": {
              "value": "*rom*"
            }
          }
        }
      ]
    }
  }
}
Alcanzar
  • 16,985
  • 6
  • 42
  • 59
  • 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. – user2742004 May 20 '14 at 05:47
  • 2
    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). – Alcanzar May 20 '14 at 13:29
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*"}
               }}
           ]
     }
   }
}
MichalT
  • 31
  • 5