---
type: api
title: Perform reranking inference on the service
description: ## Required authorization
url: https://www.elastic.co/elastic/docs-builder/docs/4641/api/doc/elasticsearch-serverless/operation/operation-inference-rerank
resource: https://www.elastic.co/elastic/docs-builder/docs/4641/api/doc/elasticsearch-serverless/operation/operation-inference-rerank
products:
  - Elasticsearch Serverless
---

# Perform reranking inference on the service

Authentication: required

## Paths

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

#### Path parameters

- `inference_id` (string) — required
  The unique identifier for the inference endpoint.

## Required authorization

- Cluster privileges: `monitor_inference`

## Query string parameters

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

## Request body

`application/json`

- `query` (union oneOf) — required
  Query input.
The query can be specified as a single string, or as an object.
The object form additionally allows specifying non-text inputs, such as images.

> info
> Only the `elastic` service currently supports non-text queries for the `rerank` task. For all other services, the query must be a string.

string example:
```
"query": "some query text"
```
object example:
```
"query": {
  "type": "image",
  "format": "base64",
  "value": "data:image/jpeg;base64,..."
}
```
object example using the `url` format (available in Elasticsearch 9.6.0 and later):
```
"query": {
  "type": "image",
  "format": "url",
  "value": "https://example.com/image.jpg"
}
```
  - **string**
  - **Input object**
    An object describing a single input for the `rerank` task, which additionally allows specifying non-text inputs, such as images.
    - `type` (enum) — required
      The type of input. Not all services and models support all input types.
      Values: `text`, `image`
    - `format` (enum) — optional
      The format of the input. For the `text` type this must be `text`. For the `image` type this must be `base64` or `url`.
If not specified, this defaults to `text` for the `text` type and `base64` for the `image` type.
The `url` format is available in Elasticsearch 9.6.0 and later.
Not all services and models support all formats.
      Values: `text`, `base64`, `url`
    - `value` (string) — required
      The value of the input. For the `base64` format, this must be a base64-encoded data URI, that is, "data:content/type;base64,...".
For the `url` format, this must be a URL that points to the content, that is, "https://example.com/image.jpg".
- `input` (union oneOf) — required
  The documents to rank.
The input can be specified as a single string or an array of strings, or as an object or an array of objects.
The object form additionally allows specifying non-text inputs, such as images.

> info
> Only the `elastic` service currently supports non-text inputs for the `rerank` task. For all other services, the input must be a string or an array of strings.

string example:
```
"input": "some document text"
```
string array example:
```
"input": ["some document text", "some more document text"]
```
object example:
```
"input": {
  "type": "image",
  "format": "base64",
  "value": "data:image/jpeg;base64,..."
}
```
object array example:
```
"input": [
  {
    "type": "text",
    "format": "text",
    "value": "some document text"
  },
  {
    "type": "image",
    "format": "base64",
    "value": "data:image/jpeg;base64,..."
  }
]
```
object array example using the `url` format (available in Elasticsearch 9.6.0 and later):
```
"input": [
  {
    "type": "image",
    "format": "url",
    "value": "https://example.com/image.jpg"
  }
]
```
  - **String or array of strings**
    Allows specifying text-only documents to rank for the `rerank` task.
  - **Object or array of objects**
    Allows specifying documents to rank as objects, which additionally supports non-text inputs, such as images.
- `return_documents` (boolean) — optional
  Include the document text in the response.
- `top_n` (number) — optional
  Limit the response to the top N documents.
- `task_settings` (object) — optional
  Task settings for the individual inference request.
These settings are specific to the task type you specified and override the task settings specified when initializing the service.

## Response

`application/json`

- `rerank` (object[])
  - `index` (number)
  - `relevance_score` (number)
  - `text` (string)
## Code examples

### Rerank task

#### Console

```console
POST _inference/rerank/cohere_rerank
{
  "input": ["luke", "like", "leia", "chewy","r2d2", "star", "wars"],
  "query": "star wars main character"
}
```

#### JavaScript

```javascript
const response = await client.inference.rerank({
  inference_id: "cohere_rerank",
  input: ["luke", "like", "leia", "chewy", "r2d2", "star", "wars"],
  query: "star wars main character",
});
```

#### Python

```python
resp = client.inference.rerank(
    inference_id="cohere_rerank",
    input=[
        "luke",
        "like",
        "leia",
        "chewy",
        "r2d2",
        "star",
        "wars"
    ],
    query="star wars main character",
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_inference/rerank/cohere_rerank" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "input": [
      "luke",
      "like",
      "leia",
      "chewy",
      "r2d2",
      "star",
      "wars"
    ],
    "query": "star wars main character"
  }'
```

#### Java

```java
client.inference().rerank(r -> r
    .inferenceId("cohere_rerank")
    .input(i -> i
        .string(List.of("luke","like","leia","chewy","r2d2","star","wars"))
    )
    .query(q -> q
        .string("star wars main character")
    )
);
```

#### C#

```c#
var response = await client.Inference
    .RerankAsync(inferenceId: "cohere_rerank", d1 => d1
        .Input(new string[] { "luke", "like", "leia", "chewy", "r2d2", "star", "wars" })
        .Query("star wars main character")
    );
```

#### PHP

```php
$resp = $client->inference()->rerank([
    "inference_id" => "cohere_rerank",
    "body" => [
        "input" => array(
            "luke",
            "like",
            "leia",
            "chewy",
            "r2d2",
            "star",
            "wars",
        ),
        "query" => "star wars main character",
    ],
]);
```

#### Ruby

```ruby
response = client.inference.rerank(
  inference_id: "cohere_rerank",
  body: {
    "input": [
      "luke",
      "like",
      "leia",
      "chewy",
      "r2d2",
      "star",
      "wars"
    ],
    "query": "star wars main character"
  }
)
```

### Rerank task using the url format

#### Console

```console
POST _inference/rerank/my-multimodal-rerank-endpoint
{
  "query": {
    "type": "image",
    "format": "url",
    "value": "https://example.com/image.jpg"
  },
  "input": ["some document text", "some more document text"],
  "top_n": 2
}
```

#### JavaScript

```javascript
const response = await client.inference.rerank({
  inference_id: "my-multimodal-rerank-endpoint",
  query: {
    type: "image",
    format: "url",
    value: "https://example.com/image.jpg",
  },
  input: ["some document text", "some more document text"],
  top_n: 2,
});
```

#### Python

```python
resp = client.inference.rerank(
    inference_id="my-multimodal-rerank-endpoint",
    query={
        "type": "image",
        "format": "url",
        "value": "https://example.com/image.jpg"
    },
    input=[
        "some document text",
        "some more document text"
    ],
    top_n=2,
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_inference/rerank/my-multimodal-rerank-endpoint" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "query": {
      "type": "image",
      "format": "url",
      "value": "https://example.com/image.jpg"
    },
    "input": [
      "some document text",
      "some more document text"
    ],
    "top_n": 2
  }'
```

#### PHP

```php
$resp = $client->inference()->rerank([
    "inference_id" => "my-multimodal-rerank-endpoint",
    "body" => [
        "query" => [
            "type" => "image",
            "format" => "url",
            "value" => "https://example.com/image.jpg",
        ],
        "input" => array(
            "some document text",
            "some more document text",
        ),
        "top_n" => 2,
    ],
]);
```

#### Ruby

```ruby
response = client.inference.rerank(
  inference_id: "my-multimodal-rerank-endpoint",
  body: {
    "query": {
      "type": "image",
      "format": "url",
      "value": "https://example.com/image.jpg"
    },
    "input": [
      "some document text",
      "some more document text"
    ],
    "top_n": 2
  }
)
```

### Rerank task using the url format for inputs

#### Console

```console
POST _inference/rerank/my-multimodal-rerank-endpoint
{
  "query": "a photo of a cat",
  "input": [
    {
      "type": "image",
      "format": "url",
      "value": "https://example.com/image1.jpg"
    },
    {
      "type": "image",
      "format": "url",
      "value": "https://example.com/image2.jpg"
    }
  ],
  "top_n": 2
}
```

#### JavaScript

```javascript
const response = await client.inference.rerank({
  inference_id: "my-multimodal-rerank-endpoint",
  query: "a photo of a cat",
  input: [
    {
      type: "image",
      format: "url",
      value: "https://example.com/image1.jpg",
    },
    {
      type: "image",
      format: "url",
      value: "https://example.com/image2.jpg",
    },
  ],
  top_n: 2,
});
```

#### Python

```python
resp = client.inference.rerank(
    inference_id="my-multimodal-rerank-endpoint",
    query="a photo of a cat",
    input=[
        {
            "type": "image",
            "format": "url",
            "value": "https://example.com/image1.jpg"
        },
        {
            "type": "image",
            "format": "url",
            "value": "https://example.com/image2.jpg"
        }
    ],
    top_n=2,
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_inference/rerank/my-multimodal-rerank-endpoint" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "query": "a photo of a cat",
    "input": [
      {
        "type": "image",
        "format": "url",
        "value": "https://example.com/image1.jpg"
      },
      {
        "type": "image",
        "format": "url",
        "value": "https://example.com/image2.jpg"
      }
    ],
    "top_n": 2
  }'
```

#### PHP

```php
$resp = $client->inference()->rerank([
    "inference_id" => "my-multimodal-rerank-endpoint",
    "body" => [
        "query" => "a photo of a cat",
        "input" => array(
            [
                "type" => "image",
                "format" => "url",
                "value" => "https://example.com/image1.jpg",
            ],
            [
                "type" => "image",
                "format" => "url",
                "value" => "https://example.com/image2.jpg",
            ],
        ),
        "top_n" => 2,
    ],
]);
```

#### Ruby

```ruby
response = client.inference.rerank(
  inference_id: "my-multimodal-rerank-endpoint",
  body: {
    "query": "a photo of a cat",
    "input": [
      {
        "type": "image",
        "format": "url",
        "value": "https://example.com/image1.jpg"
      },
      {
        "type": "image",
        "format": "url",
        "value": "https://example.com/image2.jpg"
      }
    ],
    "top_n": 2
  }
)
```

## Response examples

#### Rerank task

`application/json`

A successful response from `POST _inference/rerank/cohere_rerank`.

```json
{
  "rerank": [
    {
      "index": "2",
      "relevance_score": "0.011597361",
      "text": "leia"
    },
    {
      "index": "0",
      "relevance_score": "0.006338922",
      "text": "luke"
    },
    {
      "index": "5",
      "relevance_score": "0.0016166499",
      "text": "star"
    },
    {
      "index": "4",
      "relevance_score": "0.0011695103",
      "text": "r2d2"
    },
    {
      "index": "1",
      "relevance_score": "5.614787E-4",
      "text": "like"
    },
    {
      "index": "6",
      "relevance_score": "3.7850367E-4",
      "text": "wars"
    },
    {
      "index": "3",
      "relevance_score": "1.2508839E-5",
      "text": "chewy"
    }
  ]
}
```

#### Rerank task

`application/json`

A successful response from `POST _inference/rerank/bge-reranker-base-mkn`.

```json
{
  "rerank": [
    {
      "index": 6,
      "relevance_score": 0.50955844
    },
    {
      "index": 5,
      "relevance_score": 0.084341794
    }
  ]
}
```

#### Rerank task

`application/json`

A successful response from `POST _inference/rerank/bge-reranker-base-mkn`.

```json
{
  "rerank": [
    {
      "index": 6,
      "relevance_score": 0.50955844,
      "text": "wars"
    },
    {
      "index": 5,
      "relevance_score": 0.084341794,
      "text": "star"
    },
    {
      "index": 3,
      "relevance_score": 0.004520818,
      "text": "chewy"
    }
  ]
}
```

