Create or update an ES|QL view Generally available; Added in 9.4.0

PUT /_query/view/{name}

Stores an ES|QL query that runs whenever the view is referenced. A query that cannot be parsed is rejected. Replacing a reserved view fails.

Required authorization

  • Index privileges: create_view
Define reusable queries with E...

Path parameters

  • name string Required

    The view name to create or update.

Query parameters

  • validate boolean

    Whether to analyze the view query and return a warning when validation fails.

    Validation reports an unknown index or column, including an index the caller cannot read. The view is saved even when validation reports a problem. Failures unrelated to the query, such as an overloaded cluster, produce no warning.

    Default value is false.

application/json

Body Required

  • query string Required

    The ES|QL query string that defines the view. A query that cannot be parsed is rejected.

  • description string Generally available; Added in 9.6.0

    A free-text description of the view.

Responses

  • 200 application/json
    Hide response attribute Show response attribute object
    • acknowledged boolean Required

      For a successful response, this value is always true. On failure, an exception is returned instead.

PUT /_query/view/{name}
PUT /_query/view/country_addresses
{
  "query": """
    FROM addresses
    | RENAME city.country.name AS country
    | EVAL country = CASE(country == "United States of America", "United States", country)
    | STATS count=COUNT() BY country
  """,
  "description": "Address counts by country"
}
resp = client.esql.put_view(
    name="country_addresses",
    query="\n    FROM addresses\n    | RENAME city.country.name AS country\n    | EVAL country = CASE(country == \"United States of America\", \"United States\", country)\n    | STATS count=COUNT() BY country\n  ",
    description="Address counts by country",
)
const response = await client.esql.putView({
  name: "country_addresses",
  query:
    '\n    FROM addresses\n    | RENAME city.country.name AS country\n    | EVAL country = CASE(country == "United States of America", "United States", country)\n    | STATS count=COUNT() BY country\n  ',
  description: "Address counts by country",
});
response = client.esql.put_view(
  name: "country_addresses",
  body: {
    "query": "\n    FROM addresses\n    | RENAME city.country.name AS country\n    | EVAL country = CASE(country == \"United States of America\", \"United States\", country)\n    | STATS count=COUNT() BY country\n  ",
    "description": "Address counts by country"
  }
)
$resp = $client->esql()->putView([
    "name" => "country_addresses",
    "body" => [
        "query" => "\n    FROM addresses\n    | RENAME city.country.name AS country\n    | EVAL country = CASE(country == \"United States of America\", \"United States\", country)\n    | STATS count=COUNT() BY country\n  ",
        "description" => "Address counts by country",
    ],
]);
curl -X PUT -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d '{"query":"\n    FROM addresses\n    | RENAME city.country.name AS country\n    | EVAL country = CASE(country == \"United States of America\", \"United States\", country)\n    | STATS count=COUNT() BY country\n  ","description":"Address counts by country"}' "$ELASTICSEARCH_URL/_query/view/country_addresses"
var response = await client.Esql
    .PutViewAsync(name: "country_addresses", d1 => d1
        .Description("Address counts by country")
        .Query("\n    FROM addresses\n    | RENAME city.country.name AS country\n    | EVAL country = CASE(country == \"United States of America\", \"United States\", country)\n    | STATS count=COUNT() BY country\n  ")
    );
Request examples
Create a view that normalizes country names and counts addresses by country.
{
  "query": """
    FROM addresses
    | RENAME city.country.name AS country
    | EVAL country = CASE(country == "United States of America", "United States", country)
    | STATS count=COUNT() BY country
  """,
  "description": "Address counts by country"
}
Analyze the view query before saving it. When the query uses an unknown index or column, the response includes a warning and the view is still saved.
{
  "query": """
    FROM addresses
    | RENAME city.country.name AS country
    | EVAL country = CASE(country == "United States of America", "United States", country)
    | STATS count=COUNT() BY country
  """,
  "description": "Address counts by country"
}
Response examples (200)
The view definition was stored successfully.
{
  "acknowledged": true
}