---
type: api
title: Update a datafeed
description: You must stop and start the datafeed for the changes to be applied.
url: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/operation/operation-ml-update-datafeed
resource: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/operation/operation-ml-update-datafeed
products:
  - Elasticsearch
---

# Update a datafeed

Availability: 6.4+

## Paths

- `POST` `/_ml/datafeeds/{datafeed_id}/_update`

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

#### Path Parameters

- `datafeed_id` (string Id)
  A numerical character string that uniquely identifies the datafeed.
This identifier can contain lowercase alphanumeric characters (a-z and 0-9), hyphens, and underscores.
It must start and end with alphanumeric characters.

## Description

You must stop and start the datafeed for the changes to be applied.
When Elasticsearch security features are enabled, your datafeed remembers which roles the user who updated it had at
the time of the update and runs the query using those same roles. If you provide secondary authorization headers,
those credentials are used instead.

## Required authorization

* Cluster privileges: `manage_ml`

## Query String Parameters

- `allow_no_indices` (boolean · default: true)
  A setting that does two separate checks on the index expression.
If `false`, the request returns an error (1) if any wildcard expression
(including `_all` and `*`) resolves to zero matching indices or (2) if the
complete set of resolved indices, aliases or data streams is empty after all
expressions are evaluated. If `true`, index expressions that resolve to no
indices are allowed and the request returns an empty result.
- `expand_wildcards` (string ExpandWildcards · default: open)
  Type of index that wildcard patterns can match. If the request can target data streams, this argument determines
whether wildcard expressions match hidden data streams. Supports comma-separated values.

Supported values include:
  - `all`: Match any data stream or index, including hidden ones.
  - `open`: Match open, non-hidden indices. Also matches any non-hidden data stream.
  - `closed`: Match closed, non-hidden indices. Also matches any non-hidden data stream. Data streams cannot be closed.
  - `hidden`: Match hidden data streams and hidden indices. Must be combined with `open`, `closed`, or `both`.
  - `none`: Wildcard expressions are not accepted.
  Values: `all`, `open`, `closed`, `hidden`, `none`
- `ignore_throttled` (boolean · default: true) — deprecated
  If `true`, concrete, expanded or aliased indices are ignored when frozen.
- `ignore_unavailable` (boolean · default: false)
  If `false`, the request returns an error if it targets a concrete (non-wildcarded)
index, alias, or data stream that is missing, closed, or otherwise unavailable.
If `true`, unavailable concrete targets are silently ignored.

## Request Body

`application/json`

- `aggregations` (map string to {} AggregationContainer) — optional
  If set, the datafeed performs aggregation searches. Support for aggregations is limited and should be used only
with low cardinality data.
  See [AggregationContainer](https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/types/_types-aggregations-aggregationcontainer)
- `chunking_config` ({} ChunkingConfig) — optional
  Datafeeds might search over long time periods, for several months or years. This search is split into time
chunks in order to ensure the load on Elasticsearch is managed. Chunking configuration controls how the size of
these time chunks are calculated; it is an advanced configuration option.
  - `mode` (enum ChunkingMode) — required
    If the mode is `auto`, the chunk size is dynamically calculated;
this is the recommended value when the datafeed does not use aggregations.
If the mode is `manual`, chunking is applied according to the specified `time_span`;
use this mode when the datafeed uses aggregations. If the mode is `off`, no chunking is applied.
    Values: `auto`, `manual`, `off`
  - `time_span` (string Duration · default: 3h) — optional
    The time span that each search will be querying. This setting is applicable only when the `mode` is set to `manual`.
- `delayed_data_check_config` ({} DelayedDataCheckConfig) — optional
  Specifies whether the datafeed checks for missing data and the size of the window. The datafeed can optionally
search over indices that have already been read in an effort to determine whether any data has subsequently been
added to the index. If missing data is found, it is a good indication that the `query_delay` is set too low and
the data is being indexed after the datafeed has passed that moment in time. This check runs only on real-time
datafeeds.
  - `check_window` (string Duration) — optional
    The window of time that is searched for late data. This window of time ends with the latest finalized bucket.
It defaults to null, which causes an appropriate `check_window` to be calculated when the real-time datafeed runs.
In particular, the default `check_window` span calculation is based on the maximum of `2h` or `8 * bucket_span`.
  - `enabled` (boolean) — required
    Specifies whether the datafeed periodically checks for delayed data.
- `frequency` (string Duration) — optional
  The interval at which scheduled queries are made while the datafeed runs in real time. The default value is
either the bucket span for short bucket spans, or, for longer bucket spans, a sensible fraction of the bucket
span. When `frequency` is shorter than the bucket span, interim results for the last (partial) bucket are
written then eventually overwritten by the full bucket results. If the datafeed uses aggregations, this value
must be divisible by the interval of the date histogram aggregation.
- `indices` ([] string) — optional
  An array of index names. Wildcards are supported. If any of the indices are in remote clusters, the machine
learning nodes must have the `remote_cluster_client` role.
- `indices_options` ({} IndicesOptions) — optional
  Specifies index expansion options that are used during search.
  - `allow_no_indices` (boolean) — optional
    A setting that does two separate checks on the index expression.
If `false`, the request returns an error (1) if any wildcard expression
(including `_all` and `*`) resolves to zero matching indices or (2) if the
complete set of resolved indices, aliases or data streams is empty after all
expressions are evaluated. If `true`, index expressions that resolve to no
indices are allowed and the request returns an empty result.
  - `expand_wildcards` (string ExpandWildcards) — optional
    Type of index that wildcard patterns can match. If the request can target data streams, this argument
determines whether wildcard expressions match hidden data streams. Supports comma-separated values,
such as `open,hidden`.

Supported values include:
  - `all`: Match any data stream or index, including hidden ones.
  - `open`: Match open, non-hidden indices. Also matches any non-hidden data stream.
  - `closed`: Match closed, non-hidden indices. Also matches any non-hidden data stream. Data streams cannot be closed.
  - `hidden`: Match hidden data streams and hidden indices. Must be combined with `open`, `closed`, or `both`.
  - `none`: Wildcard expressions are not accepted.
    Values: `all`, `open`, `closed`, `hidden`, `none`
  - `ignore_unavailable` (boolean · default: false) — optional
    If `false`, the request returns an error if it targets a concrete (non-wildcarded)
index, alias, or data stream that is missing, closed, or otherwise unavailable.
If `true`, unavailable concrete targets are silently ignored.
  - `ignore_throttled` (boolean · default: true) — optional
    If true, concrete, expanded or aliased indices are ignored when frozen.
- `job_id` (string Id) — optional
- `max_empty_searches` (number) — optional
  If a real-time datafeed has never seen any data (including during any initial training period), it automatically
stops and closes the associated job after this many real-time searches return no documents. In other words,
it stops after `frequency` times `max_empty_searches` of real-time operation. If not set, a datafeed with no
end time that sees no data remains started until it is explicitly stopped. By default, it is not set.
- `max_consecutive_extraction_failures` (number) — optional
  The maximum number of consecutive real-time data extraction failures the datafeed tolerates before it
automatically stops itself, leaving the associated job open. The consecutive-failure counter resets on any
cycle that extracts successfully, including empty-data cycles. If not set, the threshold defaults to roughly
one day's worth of searches based on the datafeed `frequency` (floored at 1). Set to `-1` to disable auto-stop
and retry indefinitely. Values of `0` or less than `-1` are rejected.
- `query` ({} QueryContainer · default: {"match_all": {"boost": 1}}) — optional
  The Elasticsearch query domain-specific language (DSL). This value corresponds to the query object in an
Elasticsearch search POST body. All the options that are supported by Elasticsearch can be used, as this
object is passed verbatim to Elasticsearch. Note that if you change the query, the analyzed data is also
changed. Therefore, the time required to learn might be long and the understandability of the results is
unpredictable. If you want to make significant changes to the source data, it is recommended that you
clone the job and datafeed and make the amendments in the clone. Let both run in parallel and close one
when you are satisfied with the results of the job.
  See [QueryContainer](https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/types/_types-query_dsl-querycontainer)
- `query_delay` (string Duration) — optional
  The number of seconds behind real time that data is queried. For example, if data from 10:04 a.m. might
not be searchable in Elasticsearch until 10:06 a.m., set this property to 120 seconds. The default
value is randomly selected between `60s` and `120s`. This randomness improves the query performance
when there are multiple jobs running on the same node.
- `runtime_mappings` (RuntimeFields) — optional
  Specifies runtime fields for the datafeed search.
- `script_fields` (map string to ScriptField) — optional
  Specifies scripts that evaluate custom expressions and returns script fields to the datafeed.
The detector configuration objects in a job can contain functions that use these script fields.
  Map values ({} ScriptField)
  - `script` (string Script) — required
  - `ignore_failure` (boolean) — optional
- `scroll_size` (number · default: 1000) — optional
  The size parameter that is used in Elasticsearch searches when the datafeed does not use aggregations.
The maximum value is the value of `index.max_result_window`.

## Response

`application/json`

- `authorization` ({} DatafeedAuthorization)
  - `api_key` ({} ApiKeyAuthorization)
    If an API key was used for the most recent update to the datafeed, its name and identifier are listed in the response.
    - `id` (string)
      The identifier for the API key.
    - `name` (string)
      The name of the API key.
  - `roles` ([] string)
    If a user ID was used for the most recent update to the datafeed, its roles at the time of the update are listed in the response.
  - `service_account` (string)
    If a service account was used for the most recent update to the datafeed, the account name is listed in the response.
- `aggregations` (map string to {} AggregationContainer)
  See [AggregationContainer](https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/types/_types-aggregations-aggregationcontainer)
- `chunking_config` ({} ChunkingConfig)
  - `mode` (enum ChunkingMode)
    If the mode is `auto`, the chunk size is dynamically calculated;
this is the recommended value when the datafeed does not use aggregations.
If the mode is `manual`, chunking is applied according to the specified `time_span`;
use this mode when the datafeed uses aggregations. If the mode is `off`, no chunking is applied.
    Values: `auto`, `manual`, `off`
  - `time_span` (string Duration · default: 3h)
    The time span that each search will be querying. This setting is applicable only when the `mode` is set to `manual`.
- `delayed_data_check_config` ({} DelayedDataCheckConfig)
  - `check_window` (string Duration)
    The window of time that is searched for late data. This window of time ends with the latest finalized bucket.
It defaults to null, which causes an appropriate `check_window` to be calculated when the real-time datafeed runs.
In particular, the default `check_window` span calculation is based on the maximum of `2h` or `8 * bucket_span`.
  - `enabled` (boolean)
    Specifies whether the datafeed periodically checks for delayed data.
- `datafeed_id` (string Id)
- `frequency` (string Duration)
  The interval at which scheduled queries are made while the datafeed runs in real time. The default value is either the bucket span for short bucket spans, or, for longer bucket spans, a sensible fraction of the bucket span. For example: `150s`. When `frequency` is shorter than the bucket span, interim results for the last (partial) bucket are written then eventually overwritten by the full bucket results. If the datafeed uses aggregations, this value must be divisible by the interval of the date histogram aggregation.
- `indices` ([] string)
- `indices_options` ({} IndicesOptions)
  - `allow_no_indices` (boolean)
    A setting that does two separate checks on the index expression.
If `false`, the request returns an error (1) if any wildcard expression
(including `_all` and `*`) resolves to zero matching indices or (2) if the
complete set of resolved indices, aliases or data streams is empty after all
expressions are evaluated. If `true`, index expressions that resolve to no
indices are allowed and the request returns an empty result.
  - `expand_wildcards` (string ExpandWildcards)
    Type of index that wildcard patterns can match. If the request can target data streams, this argument
determines whether wildcard expressions match hidden data streams. Supports comma-separated values,
such as `open,hidden`.

Supported values include:
  - `all`: Match any data stream or index, including hidden ones.
  - `open`: Match open, non-hidden indices. Also matches any non-hidden data stream.
  - `closed`: Match closed, non-hidden indices. Also matches any non-hidden data stream. Data streams cannot be closed.
  - `hidden`: Match hidden data streams and hidden indices. Must be combined with `open`, `closed`, or `both`.
  - `none`: Wildcard expressions are not accepted.
    Values: `all`, `open`, `closed`, `hidden`, `none`
  - `ignore_unavailable` (boolean · default: false)
    If `false`, the request returns an error if it targets a concrete (non-wildcarded)
index, alias, or data stream that is missing, closed, or otherwise unavailable.
If `true`, unavailable concrete targets are silently ignored.
  - `ignore_throttled` (boolean · default: true)
    If true, concrete, expanded or aliased indices are ignored when frozen.
- `job_id` (string Id)
- `max_empty_searches` (number)
- `max_consecutive_extraction_failures` (number)
- `query` ({} QueryContainer)
  See [QueryContainer](https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch/types/_types-query_dsl-querycontainer)
- `query_delay` (string Duration)
- `runtime_mappings` (RuntimeFields)
- `script_fields` (map string to ScriptField)
  Map values ({} ScriptField)
  - `script` (string Script)
  - `ignore_failure` (boolean)
- `scroll_size` (number)
## Code Examples

#### Console

```console
POST _ml/datafeeds/datafeed-test-job/_update
{
  "query": {
    "term": {
      "geo.src": "US"
    }
  }
}
```

#### JavaScript

```javascript
const response = await client.ml.updateDatafeed({
  datafeed_id: "datafeed-test-job",
  query: {
    term: {
      "geo.src": "US",
    },
  },
});
```

#### Python

```python
resp = client.ml.update_datafeed(
    datafeed_id="datafeed-test-job",
    query={
        "term": {
            "geo.src": "US"
        }
    },
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_ml/datafeeds/datafeed-test-job/_update" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "query": {
      "term": {
        "geo.src": "US"
      }
    }
  }'
```

#### Java

```java
client.ml().updateDatafeed(u -> u
    .datafeedId("datafeed-test-job")
    .query(q -> q
        .term(t -> t
            .field("geo.src")
            .value(FieldValue.of("US"))
        )
    )
);
```

#### C#

```c#
var response = await client.MachineLearning
    .UpdateDatafeedAsync(datafeedId: "datafeed-test-job", d1 => d1
        .Query(d2 => d2
            .Term(d3 => d3
                .Field(x => x.Geo.Src)
                .Value("US")
            )
        )
    );
```

#### PHP

```php
$resp = $client->ml()->updateDatafeed([
    "datafeed_id" => "datafeed-test-job",
    "body" => [
        "query" => [
            "term" => [
                "geo.src" => "US",
            ],
        ],
    ],
]);
```

#### Ruby

```ruby
response = client.ml.update_datafeed(
  datafeed_id: "datafeed-test-job",
  body: {
    "query": {
      "term": {
        "geo.src": "US"
      }
    }
  }
)
```

