---
type: api
title: Translate SQL into Elasticsearch queries
description: **All methods and paths for this operation:**
url: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/elasticsearch/v8/operation/operation-sql-translate
resource: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/elasticsearch/v8/operation/operation-sql-translate
products:
  - Elasticsearch
---

# Translate SQL into Elasticsearch queries


Availability: Generally available since 6.3

## Paths

- `POST` (also `GET`) `/_sql/translate`

## Description

Translate an SQL search into a search API request containing Query DSL.
It accepts the same request body parameters as the SQL search API, excluding `cursor`.

## Required authorization

- Index privileges: `read`

## Authentication

- Api key: `Authorization: <value>`
  Elasticsearch APIs support key-based authentication.
  You must create an API key and use the encoded value in the request header.
  For example:
  
  
  ```
  curl -X GET "${ES_URL}/_cat/indices?v=true" \
    -H "Authorization: ApiKey ${API_KEY}"
  ```
  
  To get API keys, use the `/_security/api_key` APIs.
- Basic: `Authorization: Basic <credentials>`
- Bearer: `Authorization: Bearer <token>`
  Elasticsearch APIs support the use of bearer tokens in the `Authorization` HTTP header to authenticate with the API.
  For examples, refer to [Token-based authentication services](https://www.elastic.co/guide/en/elasticsearch/reference/current/token-authentication-services.html)

## Request body

`application/json`

- `fetch_size` (number · default: 1000) — optional
  The maximum number of rows (or entries) to return in one response.
- `filter` ({} Query · default: none) — optional
  The Elasticsearch query DSL for additional filtering.
  See [Query](https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/elasticsearch/v8/types/_types-query_dsl-querycontainer)
- `query` (string) — required
  The SQL query to run.
- `time_zone` (string · default: Z) — optional
  The ISO-8601 time zone ID for the search.

## Response

`application/json`

- `aggregations` (map string to {} Aggregation)
  See [Aggregation](https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/elasticsearch/v8/types/_types-aggregations-aggregationcontainer)
- `size` (number)
- `_source` (union)
- `fields` (object[])
  - `field` (string)
    A wildcard pattern. The request returns values for field names matching this pattern.
  - `format` (string)
    The format in which the values are returned.
  - `include_unmapped` (boolean)
- `query` ({} Query)
  See [Query](https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/elasticsearch/v8/types/_types-query_dsl-querycontainer)
- `sort` (union)
- `track_total_hits` (boolean | number)
## Code examples

#### Console

```console
POST _sql/translate
{
  "query": "SELECT * FROM library ORDER BY page_count DESC",
  "fetch_size": 10
}
```

#### JavaScript

```javascript
const response = await client.sql.translate({
  query: "SELECT * FROM library ORDER BY page_count DESC",
  fetch_size: 10,
});
```

#### Python

```python
resp = client.sql.translate(
    query="SELECT * FROM library ORDER BY page_count DESC",
    fetch_size=10,
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_sql/translate" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "query": "SELECT * FROM library ORDER BY page_count DESC",
    "fetch_size": 10
  }'
```

#### Java

```java
client.sql().translate(t -> t
    .fetchSize(10)
    .query("SELECT * FROM library ORDER BY page_count DESC")
);
```

#### C#

```c#
var response = await client.Sql
    .TranslateAsync(d1 => d1
        .FetchSize(10)
        .Query("SELECT * FROM library ORDER BY page_count DESC")
    );
```

#### PHP

```php
$resp = $client->sql()->translate([
    "body" => [
        "query" => "SELECT * FROM library ORDER BY page_count DESC",
        "fetch_size" => 10,
    ],
]);
```

#### Ruby

```ruby
response = client.sql.translate(
  body: {
    "query": "SELECT * FROM library ORDER BY page_count DESC",
    "fetch_size": 10
  }
)
```

