Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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*"
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
like image 352
Toshi Avatar asked Nov 22 '17 15:11

Toshi


People also ask

Is Elasticsearch similar to SQL?

It is a component that allows SQL-like queries to be executed in real-time against Elasticsearch. You can think of Elasticsearch SQL as a translator, one that understands both SQL and Elasticsearch and makes it easy to read and process data in real-time, at scale by leveraging Elasticsearch capabilities.

Is Elasticsearch better than SQL?

You want Elasticsearch when you're doing a lot of text search, where traditional RDBMS databases are not performing really well (poor configuration, acts as a black-box, poor performance). Elasticsearch is highly customizable, extendable through plugins. You can build robust search without much knowledge quite fast.

What is like and Rlike?

LIKE and RLIKE operators are commonly used to filter data based on string patterns. They usually act on a field placed on the left-hand side of the operator, but can also act on a constant (literal) expression. The right-hand side of the operator represents the pattern.

Can you query Elasticsearch with SQL?

Use your SQL skills to query data within Elasticsearch, harnessing the power of Elastic with a familiar language. Send your SQL queries via a CLI, REST endpoint, ODBC, or JDBC to get your results with newfound speed.


2 Answers

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.

like image 161
kgf3JfUtW Avatar answered Oct 26 '22 10:10

kgf3JfUtW


That's what you're looking for. Just put desired amount of wildcard queries in your bool/must:

{
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "text": {
              "value": "*quick*"
            }
          }
        },
        {
          "wildcard": {
            "text": {
              "value": "*brown*"
            }
          }
        },
        {
          "wildcard": {
            "text": {
              "value": "*fox*"
            }
          }
        }
      ]
    }
  }
}
like image 44
Evaldas Buinauskas Avatar answered Oct 26 '22 10:10

Evaldas Buinauskas