|
| 1 | +============= |
| 2 | +replace |
| 3 | +============= |
| 4 | + |
| 5 | +.. rubric:: Table of contents |
| 6 | + |
| 7 | +.. contents:: |
| 8 | + :local: |
| 9 | + :depth: 2 |
| 10 | + |
| 11 | + |
| 12 | +Description |
| 13 | +============ |
| 14 | +Using ``replace`` command to replace text in one or more fields in the search result. |
| 15 | + |
| 16 | +Note: This command is only available when Calcite engine is enabled. |
| 17 | + |
| 18 | + |
| 19 | +Syntax |
| 20 | +============ |
| 21 | +replace '<pattern>' WITH '<replacement>' [, '<pattern>' WITH '<replacement>']... IN <field-name>[, <field-name>]... |
| 22 | + |
| 23 | + |
| 24 | +Parameters |
| 25 | +========== |
| 26 | +* **pattern**: mandatory. The text pattern you want to replace. Currently supports only plain text literals (no wildcards or regular expressions). |
| 27 | +* **replacement**: mandatory. The text you want to replace with. |
| 28 | +* **field-name**: mandatory. One or more field names where the replacement should occur. |
| 29 | + |
| 30 | + |
| 31 | +Examples |
| 32 | +======== |
| 33 | + |
| 34 | +Example 1: Replace text in one field |
| 35 | +------------------------------------ |
| 36 | + |
| 37 | +The example shows replacing text in one field. |
| 38 | + |
| 39 | +PPL query:: |
| 40 | + |
| 41 | + os> source=accounts | replace "IL" WITH "Illinois" IN state | fields state; |
| 42 | + fetched rows / total rows = 4/4 |
| 43 | + +----------+ |
| 44 | + | state | |
| 45 | + |----------| |
| 46 | + | Illinois | |
| 47 | + | TN | |
| 48 | + | VA | |
| 49 | + | MD | |
| 50 | + +----------+ |
| 51 | + |
| 52 | + |
| 53 | +Example 2: Replace text in multiple fields |
| 54 | +------------------------------------ |
| 55 | + |
| 56 | +The example shows replacing text in multiple fields. |
| 57 | + |
| 58 | +PPL query:: |
| 59 | + |
| 60 | + os> source=accounts | replace "IL" WITH "Illinois" IN state, address | fields state, address; |
| 61 | + fetched rows / total rows = 4/4 |
| 62 | + +----------+----------------------+ |
| 63 | + | state | address | |
| 64 | + |----------+----------------------| |
| 65 | + | Illinois | 880 Holmes Lane | |
| 66 | + | TN | 671 Bristol Street | |
| 67 | + | VA | 789 Madison Street | |
| 68 | + | MD | 467 Hutchinson Court | |
| 69 | + +----------+----------------------+ |
| 70 | + |
| 71 | + |
| 72 | +Example 3: Replace with other commands in a pipeline |
| 73 | +------------------------------------ |
| 74 | + |
| 75 | +The example shows using replace with other commands in a query pipeline. |
| 76 | + |
| 77 | +PPL query:: |
| 78 | + |
| 79 | + os> source=accounts | replace "IL" WITH "Illinois" IN state | where age > 30 | fields state, age; |
| 80 | + fetched rows / total rows = 3/3 |
| 81 | + +----------+-----+ |
| 82 | + | state | age | |
| 83 | + |----------+-----| |
| 84 | + | Illinois | 32 | |
| 85 | + | TN | 36 | |
| 86 | + | MD | 33 | |
| 87 | + +----------+-----+ |
| 88 | + |
| 89 | +Example 4: Replace with multiple pattern/replacement pairs |
| 90 | +------------------------------------ |
| 91 | + |
| 92 | +The example shows using multiple pattern/replacement pairs in a single replace command. The replacements are applied sequentially. |
| 93 | + |
| 94 | +PPL query:: |
| 95 | + |
| 96 | + os> source=accounts | replace "IL" WITH "Illinois", "TN" WITH "Tennessee" IN state | fields state; |
| 97 | + fetched rows / total rows = 4/4 |
| 98 | + +-----------+ |
| 99 | + | state | |
| 100 | + |-----------| |
| 101 | + | Illinois | |
| 102 | + | Tennessee | |
| 103 | + | VA | |
| 104 | + | MD | |
| 105 | + +-----------+ |
| 106 | + |
| 107 | +Example 5: Pattern matching with LIKE and replace |
| 108 | +------------------------------------ |
| 109 | + |
| 110 | +Since replace command only supports plain string literals, you can use LIKE command with replace for pattern matching needs. |
| 111 | + |
| 112 | +PPL query:: |
| 113 | + |
| 114 | + os> source=accounts | where LIKE(address, '%Holmes%') | replace "Holmes" WITH "HOLMES" IN address | fields address, state, gender, age, city; |
| 115 | + fetched rows / total rows = 1/1 |
| 116 | + +-----------------+-------+--------+-----+--------+ |
| 117 | + | address | state | gender | age | city | |
| 118 | + |-----------------+-------+--------+-----+--------| |
| 119 | + | 880 HOLMES Lane | IL | M | 32 | Brogan | |
| 120 | + +-----------------+-------+--------+-----+--------+ |
| 121 | + |
| 122 | + |
| 123 | +Limitations |
| 124 | +=========== |
| 125 | +* Only supports plain text literals for pattern matching. Wildcards and regular expressions are not supported. |
| 126 | +* Pattern and replacement values must be string literals. |
| 127 | +* The replace command modifies the specified fields in-place. |
0 commit comments