like-rlike.asciidoc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. [role="xpack"]
  2. [[sql-like-rlike-operators]]
  3. === LIKE and RLIKE Operators
  4. `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
  5. the operator, but can also act on a constant (literal) expression. The right-hand side of the operator represents the pattern.
  6. Both can be used in the `WHERE` clause of the `SELECT` statement, but `LIKE` can also be used in other places, such as defining an
  7. <<sql-index-patterns, index pattern>> or across various <<sql-commands, SHOW commands>>.
  8. This section covers only the `SELECT ... WHERE ...` usage.
  9. NOTE: One significant difference between `LIKE`/`RLIKE` and the <<sql-functions-search, full-text search predicates>> is that the former
  10. act on <<sql-multi-field, exact fields>> while the latter also work on <<text, analyzed>> fields. If the field used with `LIKE`/`RLIKE` doesn't
  11. have an exact not-normalized sub-field (of <<keyword, keyword>> type) {es-sql} will not be able to run the query. If the field is either exact
  12. or has an exact sub-field, it will use it as is, or it will automatically use the exact sub-field even if it wasn't explicitly specified in the statement.
  13. [[sql-like-operator]]
  14. ==== `LIKE`
  15. .Synopsis:
  16. [source, sql]
  17. --------------------------------------------------
  18. expression <1>
  19. LIKE constant_exp <2>
  20. --------------------------------------------------
  21. <1> typically a field, or a constant expression
  22. <2> pattern
  23. *Description*: The SQL `LIKE` operator is used to compare a value to similar values using wildcard operators. There are two wildcards used in conjunction
  24. with the `LIKE` operator:
  25. * The percent sign (%)
  26. * The underscore (_)
  27. The percent sign represents zero, one or multiple characters. The underscore represents a single number or character. These symbols can be used in combinations.
  28. NOTE: No other characters have special meaning or act as wildcard. Characters often used as wildcards in other languages (`*` or `?`) are treated as normal characters.
  29. [source, sql]
  30. ----
  31. include-tagged::{sql-specs}/docs/docs.csv-spec[simpleLike]
  32. ----
  33. There is, also, the possibility of using an escape character if one needs to match the wildcard characters themselves. This can be done
  34. by using the `ESCAPE [escape_character]` statement after the `LIKE ...` operator:
  35. SELECT name, author FROM library WHERE name LIKE 'Dune/%' ESCAPE '/';
  36. In the example above `/` is defined as an escape character which needs to be placed before the `%` or `_` characters if one needs to
  37. match those characters in the pattern specifically. By default, there is no escape character defined.
  38. IMPORTANT: Even though `LIKE` is a valid option when searching or filtering in {es-sql}, full-text search predicates
  39. `MATCH` and `QUERY` are <<sql-like-prefer-full-text, faster and much more powerful and are the preferred alternative>>.
  40. [[sql-rlike-operator]]
  41. ==== `RLIKE`
  42. .Synopsis:
  43. [source, sql]
  44. --------------------------------------------------
  45. expression <1>
  46. RLIKE constant_exp <2>
  47. --------------------------------------------------
  48. <1> typically a field, or a constant expression
  49. <2> pattern
  50. *Description*: This operator is similar to `LIKE`, but the user is not limited to search for a string based on a fixed pattern with the percent sign (`%`)
  51. and underscore (`_`); the pattern in this case is a regular expression which allows the construction of more flexible patterns.
  52. For supported syntax, see <<regexp-syntax>>.
  53. [source, sql]
  54. ----
  55. include-tagged::{sql-specs}/docs/docs.csv-spec[simpleRLike]
  56. ----
  57. IMPORTANT: Even though `RLIKE` is a valid option when searching or filtering in {es-sql}, full-text search predicates
  58. `MATCH` and `QUERY` are <<sql-like-prefer-full-text, faster and much more powerful and are the preferred alternative>>.
  59. [[sql-like-prefer-full-text]]
  60. ==== Prefer full-text search predicates
  61. When using `LIKE`/`RLIKE`, do consider using <<sql-functions-search, full-text search predicates>> which are faster, much more powerful
  62. and offer the option of sorting by relevancy (results can be returned based on how well they matched).
  63. For example:
  64. [cols="<m,<m"]
  65. |===
  66. ^s|LIKE/RLIKE ^s|QUERY/MATCH
  67. |`foo LIKE 'bar'` |`MATCH(foo, 'bar')`
  68. |`foo LIKE 'bar' AND tar LIKE 'goo'` |`MATCH('foo^2, tar^5', 'bar goo', 'operator=and')`
  69. |`foo LIKE 'barr'` |`QUERY('foo: bar~')`
  70. |`foo LIKE 'bar' AND tar LIKE 'goo'` |`QUERY('foo: bar AND tar: goo')`
  71. |`foo RLIKE 'ba.*'` |`MATCH(foo, 'ba', 'fuzziness=AUTO:1,5')`
  72. |`foo RLIKE 'b.{1}r'` |`MATCH(foo, 'br', 'fuzziness=1')`
  73. |===