The eval command evaluates the expression and appends the result to the search result.
eval <field>=<expression> ["," <field>=<expression> ]...
- field: mandatory. If the field name does not exist, a new field is added. If the field name already exists, it will be overridden.
- expression: mandatory. Any expression supported by the system.
This example shows creating a new field doubleAge for each document. The new doubleAge field is the result of multiplying age by 2.
source=accounts
| eval doubleAge = age * 2
| fields age, doubleAge
Expected output:
fetched rows / total rows = 4/4
+-----+-----------+
| age | doubleAge |
|-----+-----------|
| 32 | 64 |
| 36 | 72 |
| 28 | 56 |
| 33 | 66 |
+-----+-----------+
This example shows overriding the existing age field by adding 1 to it.
source=accounts
| eval age = age + 1
| fields age
Expected output:
fetched rows / total rows = 4/4
+-----+
| age |
|-----|
| 33 |
| 37 |
| 29 |
| 34 |
+-----+
This example shows creating a new field ddAge using a field defined in the same eval command. The new field ddAge is the result of multiplying doubleAge by 2, where doubleAge is defined in the same eval command.
source=accounts
| eval doubleAge = age * 2, ddAge = doubleAge * 2
| fields age, doubleAge, ddAge
Expected output:
fetched rows / total rows = 4/4
+-----+-----------+-------+
| age | doubleAge | ddAge |
|-----+-----------+-------|
| 32 | 64 | 128 |
| 36 | 72 | 144 |
| 28 | 56 | 112 |
| 33 | 66 | 132 |
+-----+-----------+-------+
This example shows using the + operator for string concatenation. You can concatenate string literals and field values.
source=accounts
| eval greeting = 'Hello ' + firstname
| fields firstname, greeting
Expected output:
fetched rows / total rows = 4/4
+-----------+---------------+
| firstname | greeting |
|-----------+---------------|
| Amber | Hello Amber |
| Hattie | Hello Hattie |
| Nanette | Hello Nanette |
| Dale | Hello Dale |
+-----------+---------------+
This example shows multiple concatenations with type casting from numeric to string.
source=accounts | eval full_info = 'Name: ' + firstname + ', Age: ' + CAST(age AS STRING) | fields firstname, age, full_info
Expected output:
fetched rows / total rows = 4/4
+-----------+-----+------------------------+
| firstname | age | full_info |
|-----------+-----+------------------------|
| Amber | 32 | Name: Amber, Age: 32 |
| Hattie | 36 | Name: Hattie, Age: 36 |
| Nanette | 28 | Name: Nanette, Age: 28 |
| Dale | 33 | Name: Dale, Age: 33 |
+-----------+-----+------------------------+
The eval command is not rewritten to OpenSearch DSL, it is only executed on the coordination node.