Browse Source

ESQL: Add example of regex REPLACE (#134664)

Nik Everett 4 weeks ago
parent
commit
a682ed8af9

+ 12 - 2
docs/reference/query-languages/esql/_snippets/functions/examples/replace.md

@@ -1,17 +1,27 @@
 % This is generated by ESQL's AbstractFunctionTestCase. Do not edit it. See ../README.md for how to regenerate it.
 
-**Example**
+**Examples**
 
 This example replaces any occurrence of the word "World" with the word "Universe":
 
 ```esql
 ROW str = "Hello World"
 | EVAL str = REPLACE(str, "World", "Universe")
-| KEEP str
 ```
 
 | str:keyword |
 | --- |
 | Hello Universe |
 
+This example removes all spaces:
+
+```esql
+ROW str = "Hello World"
+| EVAL str = REPLACE(str, "\\\\s+", "")
+```
+
+| str:keyword |
+| --- |
+| HelloWorld |
+
 

+ 2 - 1
docs/reference/query-languages/esql/kibana/definition/functions/replace.json

@@ -198,7 +198,8 @@
     }
   ],
   "examples" : [
-    "ROW str = \"Hello World\"\n| EVAL str = REPLACE(str, \"World\", \"Universe\")\n| KEEP str"
+    "ROW str = \"Hello World\"\n| EVAL str = REPLACE(str, \"World\", \"Universe\")",
+    "ROW str = \"Hello World\"\n| EVAL str = REPLACE(str, \"\\\\\\\\s+\", \"\")"
   ],
   "preview" : false,
   "snapshot_only" : false

+ 0 - 1
docs/reference/query-languages/esql/kibana/docs/functions/replace.md

@@ -7,5 +7,4 @@ with the replacement string `newStr`.
 ```esql
 ROW str = "Hello World"
 | EVAL str = REPLACE(str, "World", "Universe")
-| KEEP str
 ```

+ 0 - 14
x-pack/plugin/esql/qa/testFixtures/src/main/resources/docs.csv-spec

@@ -335,20 +335,6 @@ date_string:keyword | date:date
 // end::dateParse-result[]
 ;
 
-docsReplace
-//tag::replaceString[]
-ROW str = "Hello World"
-| EVAL str = REPLACE(str, "World", "Universe")
-| KEEP str
-// end::replaceString[]
-;
-
-//tag::replaceString-result[]
-str:keyword
-Hello Universe
-// end::replaceString-result[]
-;
-
 docsCase
 // tag::case[]
 FROM employees

+ 27 - 0
x-pack/plugin/esql/qa/testFixtures/src/main/resources/string.csv-spec

@@ -1223,6 +1223,33 @@ first_name:keyword | result:keyword
 Alejandro          | null
 ;
 
+docsReplaceText
+//tag::replaceString[]
+ROW str = "Hello World"
+| EVAL str = REPLACE(str, "World", "Universe")
+// end::replaceString[]
+;
+
+//tag::replaceString-result[]
+str:keyword
+Hello Universe
+// end::replaceString-result[]
+;
+
+docsReplaceRegex
+//tag::replaceRegex[]
+ROW str = "Hello World"
+| EVAL str = REPLACE(str, "\\s+", "")
+// end::replaceRegex[]
+;
+
+//tag::replaceRegex-result[]
+str:keyword
+HelloWorld
+// end::replaceRegex-result[]
+;
+
+
 left
 // tag::left[]
 FROM employees

+ 7 - 5
x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/string/Replace.java

@@ -50,11 +50,13 @@ public class Replace extends EsqlScalarFunction {
         description = """
             The function substitutes in the string `str` any match of the regular expression `regex`
             with the replacement string `newStr`.""",
-        examples = @Example(
-            file = "docs",
-            tag = "replaceString",
-            description = "This example replaces any occurrence of the word \"World\" with the word \"Universe\":"
-        )
+        examples = {
+            @Example(
+                file = "string",
+                tag = "replaceString",
+                description = "This example replaces any occurrence of the word \"World\" with the word \"Universe\":"
+            ),
+            @Example(file = "string", tag = "replaceRegex", description = "This example removes all spaces:") }
     )
     public Replace(
         Source source,