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

# Perform reranking inference on the service

Availability: 8.11+

## Paths

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

- [Api key auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v9/authentication#apikeyauth)
- [Basic auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v9/authentication#basicauth)
- [Bearer auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch/v9/authentication#bearerauth)

#### Path Parameters

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

## Description



## Required authorization

* Cluster privileges: `monitor_inference`

## Query String Parameters

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

## Request Body

`application/json`

- `query` (RerankQuery) — 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,..."
}
```
- `input` (RerankInput) — 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,..."
  }
]
```
- `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` (TaskSettings) — 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` ([] RankedDocument)
  - `index` (number)
  - `relevance_score` (number)
  - `text` (string)
## Code Examples

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

## Response Examples

#### Rerank task

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

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

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

