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

# Perform reranking inference on the service

Availability: Generally available since 8.11

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

## 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
  The amount of time to wait for the inference request to complete.

## Request body

`application/json`

- `query` (string) — required
  Query input.
- `input` (union string | string[]) — required
  The text on which you want to perform the inference task.
It can be a single string or an array.

> info
> Inference endpoints for the `completion` task type currently only support a single string as input.
- `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

#### 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(List.of("luke","like","leia","chewy","r2d2","star","wars"))
    .query("star wars main character")
);
```

#### C#

```c#
var response = await client.Inference
    .RerankAsync(inferenceId: "cohere_rerank", d1 => d1
        .Input("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"
    }
  ]
}
```

