---
type: api
title: Perform sparse embedding inference on the service
url: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/elasticsearch/v8/operation/operation-inference-sparse-embedding
resource: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/elasticsearch/v8/operation/operation-inference-sparse-embedding
products:
  - Elasticsearch
---

# Perform sparse embedding inference on the service

Availability: Generally available since 8.11

## Paths

- `POST` `/_inference/sparse_embedding/{inference_id}`

#### Path parameters

- `inference_id` (string) — required
  The inference Id

## 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)

## Query string parameters

- `timeout` (string · default: 30s) — optional
  Specifies the amount of time to wait for the inference request to complete.

## Request body

`application/json`

- `input` (union string | string[]) — required
  Inference input.
Either a string or an array of strings.
- `task_settings` (object) — optional
  Optional task settings

## Response

`application/json`

- `sparse_embedding` (object[])
  - `is_truncated` (boolean)
    Indicates if the text input was truncated in the request sent to the service
  - `embedding` (object)
## Code examples

#### Console

```console
POST _inference/sparse_embedding/my-elser-model
{
  "input": "The sky above the port was the color of television tuned to a dead channel."
}
```

#### JavaScript

```javascript
const response = await client.inference.sparseEmbedding({
  inference_id: "my-elser-model",
  input:
    "The sky above the port was the color of television tuned to a dead channel.",
});
```

#### Python

```python
resp = client.inference.sparse_embedding(
    inference_id="my-elser-model",
    input="The sky above the port was the color of television tuned to a dead channel.",
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_inference/sparse_embedding/my-elser-model" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "input": "The sky above the port was the color of television tuned to a dead channel."
  }'
```

#### Java

```java
client.inference().sparseEmbedding(s -> s
    .inferenceId("my-elser-model")
    .input("The sky above the port was the color of television tuned to a dead channel.")
);
```

#### C#

```c#
var response = await client.Inference
    .SparseEmbeddingAsync(inferenceId: "my-elser-model", d1 => d1
        .Input("The sky above the port was the color of television tuned to a dead channel.")
    );
```

#### PHP

```php
$resp = $client->inference()->sparseEmbedding([
    "inference_id" => "my-elser-model",
    "body" => [
        "input" => "The sky above the port was the color of television tuned to a dead channel.",
    ],
]);
```

#### Ruby

```ruby
response = client.inference.sparse_embedding(
  inference_id: "my-elser-model",
  body: {
    "input": "The sky above the port was the color of television tuned to a dead channel."
  }
)
```

## Response examples

#### Sparse embedding task

An abbreviated response from `POST _inference/sparse_embedding/my-elser-model`.

```json
{
  "sparse_embedding": [
    {
      "is_truncated": false,
      "embedding": {
        "port": 2.1259406,
        "sky": 1.7073475,
        "color": 1.6922266,
        "dead": 1.6247464,
        "television": 1.3525393,
        "above": 1.2425821,
        "tuned": 1.1440028,
        "colors": 1.1218185,
        "tv": 1.0111054,
        "ports": 1.0067928,
        "poem": 1.0042328,
        "channel": 0.99471164,
        "tune": 0.96235967,
        "scene": 0.9020516
      }
    }
  ]
}
```

