Search code examples
sqlelasticsearchsearchsql-likeelasticsearch-5

Elasticsearch equal SQL %Like%


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*"
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }

Solution

  • 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.