---
type: api
title: Get multiple term vectors
description: **All methods and paths for this operation:**
url: https://www.elastic.co/elastic/docs-builder/docs/4641/api/doc/elasticsearch-serverless/operation/operation-mtermvectors
resource: https://www.elastic.co/elastic/docs-builder/docs/4641/api/doc/elasticsearch-serverless/operation/operation-mtermvectors
products:
  - Elasticsearch Serverless
---

# Get multiple term vectors

Authentication: required

## Paths

- `POST` (also `GET`) `/{index}/_mtermvectors`
- `POST` (also `GET`) `/_mtermvectors`

#### Path parameters

- `index` (string) — optional, leave it out to use `/_mtermvectors`
  The name of the index that contains the documents.

## Description

Get multiple term vectors with a single request.
You can specify existing documents by index and ID or provide artificial documents in the body of the request.
You can specify the index in the request body or request URI.
The response contains a `docs` array with all the fetched termvectors.
Each element has the structure provided by the termvectors API.

**Artificial documents**

You can also use `mtermvectors` to generate term vectors for artificial documents provided in the body of the request.
The mapping used is determined by the specified `_index`.

## Required authorization

- Index privileges: `read`

## Query string parameters

- `ids` (string[]) — optional
  A comma-separated list of documents ids. You must define ids as parameter or set "ids" or "docs" in the request body
- `fields` (union string | string[]) — optional
  A comma-separated list or wildcard expressions of fields to include in the statistics.
It is used as the default list unless a specific field list is provided in the `completion_fields` or `fielddata_fields` parameters.
- `field_statistics` (boolean · default: true) — optional
  If `true`, the response includes the document count, sum of document frequencies, and sum of total term frequencies.
- `offsets` (boolean · default: true) — optional
  If `true`, the response includes term offsets.
- `payloads` (boolean · default: true) — optional
  If `true`, the response includes term payloads.
- `positions` (boolean · default: true) — optional
  If `true`, the response includes term positions.
- `preference` (string) — optional
  The node or shard the operation should be performed on.
It is random by default.
- `realtime` (boolean · default: true) — optional
  If true, the request is real-time as opposed to near-real-time.
- `routing` (union string | string[]) — optional
  A custom value used to route operations to a specific shard.
Not allowed when `index.slice.enabled` is `true` for the target index; use `_slice` instead.
- `term_statistics` (boolean · default: false) — optional
  If true, the response includes term frequency and document frequency.
- `version` (number) — optional
  If `true`, returns the document version as part of a hit.
- `version_type` (enum) — optional
  The version type.

Supported values include:
  - `internal`: Use internal versioning that starts at 1 and increments with each update or delete.
  - `external`: Only index the document if the specified version is strictly higher than the version of the stored document or if there is no existing document.
  - `external_gte`: Only index the document if the specified version is equal or higher than the version of the stored document or if there is no existing document.
NOTE: The `external_gte` version type is meant for special use cases and should be used with care.
If used incorrectly, it can result in loss of data.
  Values: `internal`, `external`, `external_gte`

## Request body

`application/json`

- `docs` (object[]) — optional
  An array of existing or artificial documents.
  - `_id` (string) — optional
    The ID of the document.
  - `_index` (string) — optional
    The index of the document.
  - `doc` (object) — optional
    An artificial document (a document not present in the index) for which you want to retrieve term vectors.
  - `fields` (union oneOf) — optional
    Comma-separated list or wildcard expressions of fields to include in the statistics.
Used as the default list unless a specific field list is provided in the `completion_fields` or `fielddata_fields` parameters.
    One of: `string[]` or `string`
  - `field_statistics` (boolean · default: true) — optional
    If `true`, the response includes the document count, sum of document frequencies, and sum of total term frequencies.
  - `filter` (object) — optional
    Filter terms based on their tf-idf scores.
    - `max_doc_freq` (number) — optional
      Ignore words which occur in more than this many docs.
Defaults to unbounded.
    - `max_num_terms` (number · default: 25) — optional
      The maximum number of terms that must be returned per field.
    - `max_term_freq` (number) — optional
      Ignore words with more than this frequency in the source doc.
It defaults to unbounded.
    - `max_word_length` (number · default: 0) — optional
      The maximum word length above which words will be ignored.
Defaults to unbounded.
    - `min_doc_freq` (number · default: 1) — optional
      Ignore terms which do not occur in at least this many docs.
    - `min_term_freq` (number · default: 1) — optional
      Ignore words with less than this frequency in the source doc.
    - `min_word_length` (number · default: 0) — optional
      The minimum word length below which words will be ignored.
  - `offsets` (boolean · default: true) — optional
    If `true`, the response includes term offsets.
  - `payloads` (boolean · default: true) — optional
    If `true`, the response includes term payloads.
  - `positions` (boolean · default: true) — optional
    If `true`, the response includes term positions.
  - `routing` (union oneOf) — optional
    Custom value used to route operations to a specific shard.
    One of: `string[]` or `string`
  - `term_statistics` (boolean · default: false) — optional
    If true, the response includes term frequency and document frequency.
  - `version` (number) — optional
    If `true`, returns the document version as part of a hit.
  - `version_type` (enum) — optional
    Specific version type.

Supported values include:
  - `internal`: Use internal versioning that starts at 1 and increments with each update or delete.
  - `external`: Only index the document if the specified version is strictly higher than the version of the stored document or if there is no existing document.
  - `external_gte`: Only index the document if the specified version is equal or higher than the version of the stored document or if there is no existing document.
NOTE: The `external_gte` version type is meant for special use cases and should be used with care.
If used incorrectly, it can result in loss of data.
    Values: `internal`, `external`, `external_gte`
- `ids` (string[]) — optional
  A simplified syntax to specify documents by their ID if they're in the same index.

## Response

`application/json`

- `docs` (object[])
  - `_id` (string)
  - `_index` (string)
  - `_version` (number)
  - `took` (number)
  - `found` (boolean)
  - `term_vectors` (map string to object)
    Map values (object)
    - `field_statistics` (object)
      - `doc_count` (number)
      - `sum_doc_freq` (number)
      - `sum_ttf` (number)
    - `terms` (map string to object)
      Map values (object)
      - `doc_freq` (number)
      - `score` (number)
      - `term_freq` (number)
      - `tokens` (object[])
        - `end_offset` (number)
        - `payload` (string)
        - `position` (number)
        - `start_offset` (number)
      - `ttf` (number)
  - `error` (object)
    - `type` (string)
      The type of error
    - `reason` (union string) — nullable
      A human-readable explanation of the error, in English.
      One of: `string`
    - `stack_trace` (string)
      The server stack trace. Present only if the `error_trace=true` parameter was sent with the request.
    - `caused_by` (object) — recursive
      Same fields as `error` in `docs`
    - `root_cause` (object[]) — recursive
      Same fields as `error` in `docs`
    - `suppressed` (object[]) — recursive
      Same fields as `error` in `docs`
## Code examples

### Get multiple term vectors

#### Console

```console
POST /my-index-000001/_mtermvectors
{
  "docs": [
      {
        "_id": "2",
        "fields": [
            "message"
        ],
        "term_statistics": true
      },
      {
        "_id": "1"
      }
  ]
}
```

#### JavaScript

```javascript
const response = await client.mtermvectors({
  index: "my-index-000001",
  docs: [
    {
      _id: "2",
      fields: ["message"],
      term_statistics: true,
    },
    {
      _id: "1",
    },
  ],
});
```

#### Python

```python
resp = client.mtermvectors(
    index="my-index-000001",
    docs=[
        {
            "_id": "2",
            "fields": [
                "message"
            ],
            "term_statistics": True
        },
        {
            "_id": "1"
        }
    ],
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/my-index-000001/_mtermvectors" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "docs": [
      {
        "_id": "2",
        "fields": [
          "message"
        ],
        "term_statistics": true
      },
      {
        "_id": "1"
      }
    ]
  }'
```

#### Java

```java
client.mtermvectors(m -> m
    .docs(List.of(MultiTermVectorsOperation.of(mu -> mu
            .id("2")
            .fields("message")
            .termStatistics(true)
        ),MultiTermVectorsOperation.of(mu -> mu
            .id("1")
        )))
    .index("my-index-000001")
);
```

#### C#

```c#
var response = await client
    .MtermvectorsAsync(d1 => d1
        .Index("my-index-000001")
        .Docs(d2 => d2
            .Fields(x => x.Message)
            .Id("2")
            .TermStatistics(true), d2 => d2
            .Id("1")
        )
    );
```

#### PHP

```php
$resp = $client->mtermvectors([
    "index" => "my-index-000001",
    "body" => [
        "docs" => array(
            [
                "_id" => "2",
                "fields" => array(
                    "message",
                ),
                "term_statistics" => true,
            ],
            [
                "_id" => "1",
            ],
        ),
    ],
]);
```

#### Ruby

```ruby
response = client.mtermvectors(
  index: "my-index-000001",
  body: {
    "docs": [
      {
        "_id": "2",
        "fields": [
          "message"
        ],
        "term_statistics": true
      },
      {
        "_id": "1"
      }
    ]
  }
)
```

### Simplified syntax

#### Console

```console
POST /my-index-000001/_mtermvectors
{
  "ids": [ "1", "2" ],
  "fields": [
    "message"
  ],
  "term_statistics": true
}
```

#### JavaScript

```javascript
const response = await client.mtermvectors({
  index: "my-index-000001",
  ids: ["1", "2"],
  fields: ["message"],
  term_statistics: true,
});
```

#### Python

```python
resp = client.mtermvectors(
    index="my-index-000001",
    ids=[
        "1",
        "2"
    ],
    fields=[
        "message"
    ],
    term_statistics=True,
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/my-index-000001/_mtermvectors" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "ids": [
      "1",
      "2"
    ],
    "fields": [
      "message"
    ],
    "term_statistics": true
  }'
```

#### Java

```java
client.mtermvectors(m -> m
    .ids(List.of("1","2"))
    .index("my-index-000001")
);
```

#### C#

```c#
var response = await client
    .MtermvectorsAsync(d1 => d1
        .Index("my-index-000001")
        .Ids(["1", "2"])
    );
```

#### PHP

```php
$resp = $client->mtermvectors([
    "index" => "my-index-000001",
    "body" => [
        "ids" => array(
            "1",
            "2",
        ),
        "fields" => array(
            "message",
        ),
        "term_statistics" => true,
    ],
]);
```

#### Ruby

```ruby
response = client.mtermvectors(
  index: "my-index-000001",
  body: {
    "ids": [
      "1",
      "2"
    ],
    "fields": [
      "message"
    ],
    "term_statistics": true
  }
)
```

### Artificial documents

#### Console

```console
POST /_mtermvectors
{
  "docs": [
      {
        "_index": "my-index-000001",
        "doc" : {
            "message" : "test test test"
        }
      },
      {
        "_index": "my-index-000001",
        "doc" : {
          "message" : "Another test ..."
        }
      }
  ]
}
```

#### JavaScript

```javascript
const response = await client.mtermvectors({
  docs: [
    {
      _index: "my-index-000001",
      doc: {
        message: "test test test",
      },
    },
    {
      _index: "my-index-000001",
      doc: {
        message: "Another test ...",
      },
    },
  ],
});
```

#### Python

```python
resp = client.mtermvectors(
    docs=[
        {
            "_index": "my-index-000001",
            "doc": {
                "message": "test test test"
            }
        },
        {
            "_index": "my-index-000001",
            "doc": {
                "message": "Another test ..."
            }
        }
    ],
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_mtermvectors" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "docs": [
      {
        "_index": "my-index-000001",
        "doc": {
          "message": "test test test"
        }
      },
      {
        "_index": "my-index-000001",
        "doc": {
          "message": "Another test ..."
        }
      }
    ]
  }'
```

#### Java

```java
client.mtermvectors(m -> m
    .docs(List.of(MultiTermVectorsOperation.of(mu -> mu
            .index("my-index-000001")
            .doc(JsonData.fromJson("""
{"message":"test test test"}
"""))
        ),MultiTermVectorsOperation.of(mu -> mu
            .index("my-index-000001")
            .doc(JsonData.fromJson("""
{"message":"Another test ..."}
"""))
        )))
);
```

#### C#

```c#
var response = await client
    .MtermvectorsAsync(d1 => d1
        .Docs(d2 => d2
            .Doc(JsonSerializer.Deserialize<JsonElement>("""
            {
              "message": "test test test"
            }
            """))
            .Index("my-index-000001"), d2 => d2
            .Doc(JsonSerializer.Deserialize<JsonElement>("""
            {
              "message": "Another test ..."
            }
            """))
            .Index("my-index-000001")
        )
    );
```

#### PHP

```php
$resp = $client->mtermvectors([
    "body" => [
        "docs" => array(
            [
                "_index" => "my-index-000001",
                "doc" => [
                    "message" => "test test test",
                ],
            ],
            [
                "_index" => "my-index-000001",
                "doc" => [
                    "message" => "Another test ...",
                ],
            ],
        ),
    ],
]);
```

#### Ruby

```ruby
response = client.mtermvectors(
  body: {
    "docs": [
      {
        "_index": "my-index-000001",
        "doc": {
          "message": "test test test"
        }
      },
      {
        "_index": "my-index-000001",
        "doc": {
          "message": "Another test ..."
        }
      }
    ]
  }
)
```

## Response examples

#### 200

`application/json`

Generated from the API spec. Values are representative. Unions use the first variant; deeply nested or large bodies may omit fields.

```json
{
  "docs": [
    {
      "_id": "string",
      "_index": "string",
      "_version": 0,
      "took": 0,
      "found": false,
      "term_vectors": {
        "key": {
          "field_statistics": {
            "doc_count": 0,
            "sum_doc_freq": 0,
            "sum_ttf": 0
          },
          "terms": {}
        }
      },
      "error": {
        "type": "string",
        "reason": "string",
        "stack_trace": "string",
        "caused_by": {
          "type": "string",
          "reason": "string",
          "stack_trace": "string",
          "caused_by": {
            "type": "string",
            "stack_trace": "string"
          },
          "root_cause": [],
          "suppressed": []
        },
        "root_cause": [
          {
            "type": "string",
            "stack_trace": "string"
          }
        ],
        "suppressed": [
          {
            "type": "string",
            "stack_trace": "string"
          }
        ]
      }
    }
  ]
}
```

