Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 36 ยท Search Processing Language

SPL eval Command: Calculated Fields, if, case, strftime and More

Use the SPL eval command to create fields with arithmetic, conditionals (if, case), string, math, multivalue and time functions, plus null handling with coalesce and fillnull.

eval is the Swiss Army knife of SPL: it computes a new field (or overwrites one) from an expression, event by event. It is a streaming command, so it is cheap and runs on indexers in parallel. Almost every real search uses it somewhere.

SPL
index=web
| eval kb = round(bytes / 1024, 1)
| eval is_error = if(status >= 500, "yes", "no")

You can chain several assignments in one command, separated by commas: | eval a=1, b=a+1 (later assignments can use earlier ones).

Operators and data types

Arithmetic: + - * / %. Comparison: = == != < > <= >=. Logical: AND OR NOT XOR. String concatenation uses . (or + when both sides are strings): eval full = first . " " . last. String literals use double quotes; field names with special characters use single quotes: eval x = 'my field' + 1.

Common pitfalls: a field that holds text that looks like a number is a string; use tonumber(x) to convert. Dividing by a field that may be zero gives null (no error), so check first.

Conditional functions

SPL
| eval severity = if(status >= 500, "server", if(status >= 400, "client", "ok"))
| eval bucket = case(bytes < 1000, "small",
                     bytes < 100000, "medium",
                     true(), "large")
| eval who = coalesce(user, username, "anonymous")
| eval safe = if(isnull(bytes), 0, bytes)
| eval has_ip = if(isnotnull(clientip), 1, 0)
| eval label = if(match(uri_path, "^/api/"), "api", "page")
  • if(condition, valueIfTrue, valueIfFalse).
  • case(c1, v1, c2, v2, ...) returns the value for the first true condition; add true() as the last condition for a default (otherwise unmatched events get null).
  • coalesce(a, b, c) returns the first non-null argument.
  • match(string, regex) and like(string, "pattern%") test strings; in(field, "a", "b") tests membership.
Python Stand-in: if, case and coalesce

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Math functions

round(x, n), floor, ceiling, abs, sqrt, pow(x,y), log(x,base), exp, min(a,b,...), max(a,b,...), random(). (Note: min() and max() inside eval take several arguments from one event; the stats versions aggregate across events.)

SPL
| eval pct = round(errors / requests * 100, 2)
| eval mb = round(bytes / pow(1024, 2), 2)

String functions

len, lower, upper, trim, ltrim, rtrim, substr(s, start, length) (1-based), replace(s, regex, replacement), split(s, delimiter) (makes a multivalue field), urldecode, tostring, tonumber, spath (JSON), printf, sha256.

SPL
| eval domain = lower(substr(email, 1 + len(user) + 1))
| eval path_clean = replace(uri_path, "/\d+", "/<id>")
| eval parts = split(uri_path, "/")
| eval first_part = mvindex(parts, 1)

The replace example turns /orders/8841 into /orders/<id>, a very useful trick for grouping URLs. Note regex backslashes are doubled in some contexts; check your version's behavior if a pattern fails.

Python Stand-in: normalizing URLs like eval replace()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Time functions

Event time is _time, stored as epoch seconds. Conversions:

SPL
| eval day = strftime(_time, "%Y-%m-%d")
| eval hour = strftime(_time, "%H")
| eval weekday = strftime(_time, "%A")
| eval epoch = strptime("2026-11-12 09:14:03", "%Y-%m-%d %H:%M:%S")
| eval age_hours = round((now() - _time) / 3600, 1)
| eval ts = relative_time(now(), "-1d@d")

strftime formats epoch to text; strptime parses text to epoch; now() is the search start; relative_time applies a relative modifier. Use tostring(seconds, "duration") to show HH:MM:SS.

Python Stand-in: strftime and strptime

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Multivalue functions

A field can hold several values (for example from values() or split()). Functions: mvcount, mvindex(mv, i), mvjoin(mv, sep), mvappend, mvfilter(condition), mvdedup, and the mvexpand command that makes one event per value.

SPL
| eval n_tags = mvcount(tags)
| eval first_tag = mvindex(tags, 0)
| eval tag_list = mvjoin(tags, ", ")
| mvexpand tags

Where to use eval

  • Before stats to build flags and buckets (eval is_error=if(...)).
  • After stats to compute ratios and formats.
  • With where to filter on computed values.
  • With where when a filter depends on a computed value (the plain search command cannot evaluate expressions).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which function returns the first non-null value from a list of fields?

    coalesce(a, b, c) returns the first argument that is not null.
  2. 2. How do you provide a default branch in case()?

    true() always matches, so it catches everything the earlier conditions did not.
  3. 3. Which function turns the epoch _time into a text date?

    strftime formats; strptime parses text into epoch.
  4. 4. What operator concatenates strings in eval?

    Use the dot: eval full = first . " " . last.

Summary

  • eval field = expression computes new fields event by event; separate assignments with commas.
  • Use if, case (with true() default), coalesce, match, like for logic.
  • String, math, time and multivalue function families cover most needs.
  • Combine eval with stats: build flags before, compute rates after.

Next: Subsearches and combining data