---
type: api
title: Create or update an ES|QL view
description: Stores an ES|QL query that runs whenever the view is referenced.
url: https://www.elastic.co/elastic/docs-builder/docs/4641/api/doc/elasticsearch-serverless/operation/operation-esql-put-view
resource: https://www.elastic.co/elastic/docs-builder/docs/4641/api/doc/elasticsearch-serverless/operation/operation-esql-put-view
products:
  - Elasticsearch Serverless
---

# Create or update an ES|QL view

Authentication: required

## Paths

- `PUT` `/_query/view/{name}`

#### Path parameters

- `name` (string) — required
  The view name to create or update.

## Description

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.

[Define reusable queries with ES|QL views](https://www.elastic.co/docs/reference/query-languages/esql/esql-views)

## Required authorization

- Index privileges: `create_view`

## Query string parameters

- `validate` (boolean · default: false) — optional
  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.

## Request body

`application/json`

- `query` (string) — required
  The ES|QL query string that defines the view. A query that cannot be parsed is rejected.
- `description` (string) — optional
  A free-text description of the view.

## Response

`application/json`

- `acknowledged` (boolean)
  For a successful response, this value is always true. On failure, an exception is returned instead.
## Code examples

### Create a view of country counts

#### Console

```console
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"
}
```

#### JavaScript

```javascript
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",
});
```

#### Python

```python
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",
)
```

#### curl

```curl
curl -X PUT "$ELASTICSEARCH_URL/_query/view/country_addresses" \
  -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 == \u0022United States of America\u0022, \u0022United States\u0022, country)\n    | STATS count=COUNT() BY country\n  ",
    "description": "Address counts by country"
  }'
```

#### C#

```c#
var response = await client.Esql
    .PutViewAsync(name: "country_addresses", d1 => d1
        .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  ")
    );
```

#### PHP

```php
$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",
    ],
]);
```

#### Ruby

```ruby
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"
  }
)
```

### Create a view and validate the query

#### Console

```console
PUT /_query/view/country_addresses?validate=true
{
  "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"
}
```

#### JavaScript

```javascript
const response = await client.esql.putView({
  name: "country_addresses",
  validate: "true",
  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",
});
```

#### Python

```python
resp = client.esql.put_view(
    name="country_addresses",
    validate=True,
    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

```curl
curl -X PUT "$ELASTICSEARCH_URL/_query/view/country_addresses?validate=true" \
  -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 == \u0022United States of America\u0022, \u0022United States\u0022, country)\n    | STATS count=COUNT() BY country\n  ",
    "description": "Address counts by country"
  }'
```

#### C#

```c#
var response = await client.Esql
    .PutViewAsync(name: "country_addresses", d1 => d1
        .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  ")
    );
```

#### PHP

```php
$resp = $client->esql()->putView([
    "name" => "country_addresses",
    "validate" => "true",
    "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",
    ],
]);
```

#### Ruby

```ruby
response = client.esql.put_view(
  name: "country_addresses",
  validate: "true",
  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"
  }
)
```

## Response examples

#### View created

`application/json`

The view definition was stored successfully.

```json
{
  "acknowledged": true
}
```

