Elasticsearch equal SQL %Like%

Toshi picture Toshi · Nov 22, 2017 · Viewed 7.5k times · Source

Coming from here i'm asking myselve for the elasticsearch syntax for such querys:

WHERE text LIKE "%quick%"
  AND text LIKE "%brown%"
  AND text LIKE "%fox%" 

my try (unfortunately without success)

  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must": [
              {
                "terms": {
                  "text": [
                    "*quick*",
                    "*brown*",
                    "*fox*"
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }

Answer

kgf3JfUtW picture kgf3JfUtW · Nov 22, 2017

Try using bool and wildcard to do such a query.

{
    "query": {
        "bool": {
            "must": [
                {
                    "wildcard": {
                        "text": "*quick*"
                    }
                },
                {
                    "wildcard": {
                        "text": "*brown*"
                    }
                },
                {
                    "wildcard": {
                        "text": "*fox*"
                    }
                }
            ]
        }
    }
}

Wildcard Query Matches documents that have fields matching a wildcard expression (not analyzed). Supported wildcards are *, which matches any character sequence (including the empty one), and ?, which matches any single character.