Elasticsearch Py
Elasticsearch Py
Elasticsearch Py
Release 7.0.0
Honza Král
1 Compatibility 3
2 Installation 5
3 Example Usage 7
4 Features 9
4.1 Persistent Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.2 Automatic Retries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.3 Sniffing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.4 Thread safety . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.5 SSL and Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.6 Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5 Environment considerations 13
5.1 Compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.2 Running on AWS with IAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6 Customization 15
6.1 Custom serializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
7 Contents 17
7.1 API Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
7.2 X-Pack APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
7.3 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
7.4 Connection Layer API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
7.5 Transport classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
7.6 Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
7.7 Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
8 License 97
i
ii
Elasticsearch Documentation, Release 7.0.0
Official low-level client for Elasticsearch. Its goal is to provide common ground for all Elasticsearch-related code in
Python; because of this it tries to be opinion-free and very extendable.
For a more high level client library with more limited scope, have a look at elasticsearch-dsl - it is a more pythonic
library sitting on top of elasticsearch-py.
Contents 1
Elasticsearch Documentation, Release 7.0.0
2 Contents
CHAPTER 1
Compatibility
The library is compatible with all Elasticsearch versions since 0.90.x but you have to use a matching major
version:
For Elasticsearch 6.0 and later, use the major version 6 (6.x.y) of the library.
For Elasticsearch 5.0 and later, use the major version 5 (5.x.y) of the library.
For Elasticsearch 2.0 and later, use the major version 2 (2.x.y) of the library, and so on.
The recommended way to set your requirements in your setup.py or requirements.txt is:
# Elasticsearch 6.x
elasticsearch>=6.0.0,<7.0.0
# Elasticsearch 5.x
elasticsearch>=5.0.0,<6.0.0
# Elasticsearch 2.x
elasticsearch>=2.0.0,<3.0.0
If you have a need to have multiple versions installed at the same time older versions are also released as
elasticsearch2 and elasticsearch5.
3
Elasticsearch Documentation, Release 7.0.0
4 Chapter 1. Compatibility
CHAPTER 2
Installation
5
Elasticsearch Documentation, Release 7.0.0
6 Chapter 2. Installation
CHAPTER 3
Example Usage
doc = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': datetime.now(),
}
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res['result'])
es.indices.refresh(index="test-index")
7
Elasticsearch Documentation, Release 7.0.0
Features
This client was designed as very thin wrapper around Elasticsearch’s REST API to allow for maximum flexibility. This
means that there are no opinions in this client; it also means that some of the APIs are a little cumbersome to use from
Python. We have created some Helpers to help with this issue as well as a more high level library (elasticsearch-dsl)
on top of this one to provide a more convenient way of working with Elasticsearch.
elasticsearch-py uses persistent connections inside of individual connection pools (one per each configured or
sniffed node). Out of the box you can choose between two http protocol implementations. See Transport classes for
more information.
The transport layer will create an instance of the selected connection class per node and keep track of the health of
individual nodes - if a node becomes unresponsive (throwing exceptions while connecting to it) it’s put on a timeout
by the ConnectionPool class and only returned to the circulation after the timeout is over (or when no live nodes
are left). By default nodes are randomized before being passed into the pool and round-robin strategy is used for load
balancing.
You can customize this behavior by passing parameters to the Connection Layer API (all keyword arguments to the
Elasticsearch class will be passed through). If what you want to accomplish is not supported you should be
able to create a subclass of the relevant component and pass it in as a parameter to be used instead of the default
implementation.
If a connection to a node fails due to connection issues (raises ConnectionError) it is considered in faulty state.
It will be placed on hold for dead_timeout seconds and the request will be retried on another node. If a connection
fails multiple times in a row the timeout will get progressively larger to avoid hitting a node that’s, by all indication,
down. If no live connection is available, the connection that has the smallest timeout will be used.
9
Elasticsearch Documentation, Release 7.0.0
By default retries are not triggered by a timeout (ConnectionTimeout), set retry_on_timeout to True to
also retry on timeouts.
4.3 Sniffing
The client can be configured to inspect the cluster state to get a list of nodes upon startup, periodically and/or on
failure. See Transport parameters for details.
Some example configurations:
from elasticsearch import Elasticsearch
# you can specify to sniff on startup to inspect the cluster and load
# balance across all nodes
es = Elasticsearch(["seed1", "seed2"], sniff_on_start=True)
The client is thread safe and can be used in a multi threaded environment. Best practice is to create a single global
instance of the client and use it throughout your application. If your application is long-running consider turning on
Sniffing to make sure the client is up to date on the cluster location.
By default we allow urllib3 to open up to 10 connections to each node, if your application calls for more paral-
lelism, use the maxsize parameter to raise the limit:
# allow up to 25 connections to each node
es = Elasticsearch(["host1", "host2"], maxsize=25)
Note: Since we use persistent connections throughout the client it means that the client doesn’t tolerate fork very
well. If your application calls for multiple processes make sure you create a fresh client after call to fork. Note that
Python’s multiprocessing module uses fork to create new processes on POSIX systems.
You can configure the client to use SSL for connecting to your elasticsearch cluster, including certificate verification
and HTTP auth:
from elasticsearch import Elasticsearch
10 Chapter 4. Features
Elasticsearch Documentation, Release 7.0.0
es = Elasticsearch(
['localhost', 'otherhost'],
http_auth=('user', 'secret'),
scheme="https",
port=443,
)
context = create_default_context(cafile="path/to/cert.pem")
es = Elasticsearch(
['localhost', 'otherhost'],
http_auth=('user', 'secret'),
scheme="https",
port=443,
ssl_context=context,
)
Warning: elasticsearch-py doesn’t ship with default set of root certificates. To have working SSL cer-
tificate validation you need to either specify your own as cafile or capath or cadata or install certifi which
will be picked up automatically.
4.6 Logging
elasticsearch-py uses the standard logging library from python to define two loggers: elasticsearch and
elasticsearch.trace. elasticsearch is used by the client to log standard activity, depending on the log
level. elasticsearch.trace can be used to log requests to the server in the form of curl commands using
pretty-printed json that can then be executed from command line. Because it is designed to be shared (for example to
demonstrate an issue) it also just uses localhost:9200 as the address instead of the actual address of the host. If
the trace logger has not been configured already it is set to propagate=False so it needs to be activated separately.
4.6. Logging 11
Elasticsearch Documentation, Release 7.0.0
12 Chapter 4. Features
CHAPTER 5
Environment considerations
When using the client there are several limitations of your environment that could come into play.
When using an HTTP load balancer you cannot use the Sniffing functionality - the cluster would supply the client with
IP addresses to directly connect to the cluster, circumventing the load balancer. Depending on your configuration this
might be something you don’t want or break completely.
In some environments (notably on Google App Engine) your HTTP requests might be restricted so that GET requests
won’t accept body. In that case use the send_get_body_as parameter of Transport to send all bodies via post:
5.1 Compression
When using capacity-constrained networks (low throughput), it may be handy to enable compression. This is espe-
cially useful when doing bulk loads or inserting large documents. This will configure compression on the request.
If you want to use this client with IAM based authentication on AWS you can use the requests-aws4auth package:
host = 'YOURHOST.us-east-1.es.amazonaws.com'
awsauth = AWS4Auth(YOUR_ACCESS_KEY, YOUR_SECRET_KEY, REGION, 'es')
(continues on next page)
13
Elasticsearch Documentation, Release 7.0.0
es = Elasticsearch(
hosts=[{'host': host, 'port': 443}],
http_auth=awsauth,
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection
)
print(es.info())
Customization
By default, JSONSerializer is used to encode all outgoing requests. However, you can implement your own custom
serializer:
class SetEncoder(JSONSerializer):
def default(self, obj):
if isinstance(obj, set):
return list(obj)
if isinstance(obj, Something):
return 'CustomSomethingRepresentation'
return JSONSerializer.default(self, obj)
es = Elasticsearch(serializer=SetEncoder())
15
Elasticsearch Documentation, Release 7.0.0
16 Chapter 6. Customization
CHAPTER 7
Contents
All the API calls map the raw REST api as closely as possible, including the distinction between required and optional
arguments to the calls. This means that the code makes distinction between positional and keyword arguments; we,
however, recommend that people use keyword arguments for all calls for consistency and safety.
Note: for compatibility with the Python ecosystem we use from_ instead of from and doc_type instead of type
as parameter names.
Some parameters are added by the client itself and can be used in all API calls.
Ignore
An API call is considered successful (and will return a response) if elasticsearch returns a 2XX response. Otherwise
an instance of TransportError (or a more specific subclass) will be raised. You can see other exception and error
states in Exceptions. If you do not wish an exception to be raised you can always pass in an ignore parameter with
either a single status code that should be ignored or a list of them:
17
Elasticsearch Documentation, Release 7.0.0
Timeout
Global timeout can be set when constructing the client (see Connection’s timeout parameter) or on a per-request
basis using request_timeout (float value in seconds) as part of any API call, this value will get passed to the
perform_request method of the connection class:
Note: Some API calls also accept a timeout parameter that is passed to Elasticsearch server. This timeout is
internal and doesn’t guarantee that the request will end in the specified time.
Response Filtering
The filter_path parameter is used to reduce the response returned by elasticsearch. For example, to only return
_id and _type, do:
It also supports the * wildcard character to match any field or part of a field’s name:
es.search(index='test-index', filter_path=['hits.hits._*'])
7.1.2 Elasticsearch
If you want to turn on Sniffing you have several options (described in Transport):
18 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
Different hosts can have different parameters, use a dictionary per node to specify those:
# connect to localhost directly and another node using SSL on port 443
# and an url_prefix. Note that ``port`` needs to be an int.
es = Elasticsearch([
{'host': 'localhost'},
{'host': 'othernode', 'port': 443, 'url_prefix': 'es', 'use_ssl': True},
])
If using SSL, there are several parameters that control how we deal with certificates (see
Urllib3HttpConnection for detailed description of the options):
es = Elasticsearch(
['localhost:443', 'other_host:443'],
# turn on SSL
use_ssl=True,
# make sure we verify SSL certificates
verify_certs=True,
# provide a path to CA certs on disk
ca_certs='/path/to/CA_certs'
)
If using SSL, but don’t verify the certs, a warning message is showed optionally (see
Urllib3HttpConnection for detailed description of the options):
es = Elasticsearch(
['localhost:443', 'other_host:443'],
# turn on SSL
use_ssl=True,
# no verify SSL certificates
verify_certs=False,
# don't show warnings about ssl certs verification
ssl_show_warn=False
)
SSL client authentication is supported (see Urllib3HttpConnection for detailed description of the op-
tions):
es = Elasticsearch(
['localhost:443', 'other_host:443'],
# turn on SSL
use_ssl=True,
# make sure we verify SSL certificates
verify_certs=True,
# provide a path to CA certs on disk
ca_certs='/path/to/CA_certs',
# PEM formatted SSL client certificate
client_cert='/path/to/clientcert.pem',
# PEM formatted SSL client key
client_key='/path/to/clientkey.pem'
)
Alternatively you can use RFC-1738 formatted URLs, as long as they are not in conflict with other options:
es = Elasticsearch(
[
'http://user:secret@localhost:9200/',
'https://user:secret@other_host:443/production'
],
verify_certs=True
)
By default, JSONSerializer is used to encode all outgoing requests. However, you can implement your own
custom serializer:
from elasticsearch.serializer import JSONSerializer
class SetEncoder(JSONSerializer):
def default(self, obj):
if isinstance(obj, set):
return list(obj)
if isinstance(obj, Something):
return 'CustomSomethingRepresentation'
return JSONSerializer.default(self, obj)
es = Elasticsearch(serializer=SetEncoder())
Parameters
• hosts – list of nodes we should connect to. Node should be a dictionary ({“host”: “lo-
calhost”, “port”: 9200}), the entire dictionary will be passed to the Connection class as
kwargs, or a string in the format of host[:port] which will be translated to a dictionary
automatically. If no value is given the Urllib3HttpConnection class defaults will be
used.
• transport_class – Transport subclass to use.
• kwargs – any additional arguments will be passed on to the Transport class and, sub-
sequently, to the Connection instances.
bulk(**kwargs)
Perform many index/delete operations in a single API call.
See the bulk() helper function for a more friendly API. http://www.elastic.co/guide/en/elasticsearch/
reference/current/docs-bulk.html
Parameters
• body – The operation definition and data (action-data pairs), separated by newlines
• index – Default index for items which don’t provide one
• _source – True or false to return the _source field or not, or default list of fields to return,
can be overridden on each sub- request
• _source_exclude – Default list of fields to exclude from the returned _source field,
can be overridden on each sub-request
• _source_include – Default list of fields to extract and return from the _source field,
can be overridden on each sub-request
• fields – Default comma-separated list of fields to return in the response for updates,
can be overridden on each sub-request
• pipeline – The pipeline id to preprocess incoming documents with
20 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
• refresh – If true then refresh the effected shards to make this operation visible to search,
if wait_for then wait for a refresh to make this operation visible to search, if false (the
default) then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
• routing – Specific routing value
• timeout – Explicit operation timeout
• wait_for_active_shards – Sets the number of shard copies that must be active
before proceeding with the bulk operation. Defaults to 1, meaning the primary shard only.
Set to all for all shard copies, otherwise set to any non-negative value less than or equal to
the total number of copies for the shard (number of replicas + 1)
clear_scroll(**kwargs)
Clear the scroll request created by specifying the scroll parameter to search. http://www.elastic.co/guide/
en/elasticsearch/reference/current/search-request-scroll.html
Parameters
• scroll_id – A comma-separated list of scroll IDs to clear
• body – A comma-separated list of scroll IDs to clear if none was specified via the scroll_id
parameter
count(**kwargs)
Execute a query and get the number of matches for that query. http://www.elastic.co/guide/en/
elasticsearch/reference/current/search-count.html
Parameters
• index – A list of index names or a string containing a comma-separated list of index
names to restrict the results to
• body – A query to restrict the results specified with the Query DSL (optional)
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• analyze_wildcard – Specify whether wildcard and prefix queries should be analyzed
(default: false)
• analyzer – The analyzer to use for the query string
• default_operator – The default operator for query string query (AND or OR), de-
fault ‘OR’, valid choices are: ‘AND’, ‘OR’
• df – The field to use as default where no field prefix is given in the query string
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
• lenient – Specify whether format-based query failures (such as providing text to a
numeric field) should be ignored
• min_score – Include only documents with a specific _score value in the result
• preference – Specify the node or shard the operation should be performed on (default:
random)
• q – Query in the Lucene query string syntax
• routing – Specific routing value
create(**kwargs)
Adds a typed JSON document in a specific index, making it searchable. Behind the scenes this
method calls index(. . . , op_type=’create’) http://www.elastic.co/guide/en/elasticsearch/reference/current/
docs-index_.html
Parameters
• index – The name of the index
• id – Document ID
• body – The document
• parent – ID of the parent document
• pipeline – The pipeline id to preprocess incoming documents with
• refresh – If true then refresh the affected shards to make this operation visible to search,
if wait_for then wait for a refresh to make this operation visible to search, if false (the
default) then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
• routing – Specific routing value
• timeout – Explicit operation timeout
• timestamp – Explicit timestamp for the document
• ttl – Expiration time for the document
• version – Explicit version number for concurrency control
• version_type – Specific version type, valid choices are: ‘internal’, ‘external’, ‘exter-
nal_gte’, ‘force’
• wait_for_active_shards – Sets the number of shard copies that must be active
before proceeding with the index operation. Defaults to 1, meaning the primary shard
only. Set to all for all shard copies, otherwise set to any non-negative value less than or
equal to the total number of copies for the shard (number of replicas + 1)
delete(**kwargs)
Delete a typed JSON document from a specific index based on its id. http://www.elastic.co/guide/en/
elasticsearch/reference/current/docs-delete.html
Parameters
• index – The name of the index
• id – The document ID
• parent – ID of parent document
• refresh – If true then refresh the effected shards to make this operation visible to search,
if wait_for then wait for a refresh to make this operation visible to search, if false (the
default) then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
• routing – Specific routing value
• timeout – Explicit operation timeout
• version – Explicit version number for concurrency control
• version_type – Specific version type, valid choices are: ‘internal’, ‘external’, ‘exter-
nal_gte’, ‘force’
• wait_for_active_shards – Sets the number of shard copies that must be active
before proceeding with the delete operation. Defaults to 1, meaning the primary shard
22 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
only. Set to all for all shard copies, otherwise set to any non-negative value less than or
equal to the total number of copies for the shard (number of replicas + 1)
delete_by_query(**kwargs)
Delete all documents matching a query. https://www.elastic.co/guide/en/elasticsearch/reference/current/
docs-delete-by-query.html
Parameters
• index – A list of index names to search, or a string containing a comma-separated list of
index names to search; use _all or the empty string to perform the operation on all indices
• body – The search definition using the Query DSL
• _source – True or false to return the _source field or not, or a list of fields to return
• _source_exclude – A list of fields to exclude from the returned _source field
• _source_include – A list of fields to extract and return from the _source field
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• analyze_wildcard – Specify whether wildcard and prefix queries should be analyzed
(default: false)
• analyzer – The analyzer to use for the query string
• conflicts – What to do when the delete-by-query hits version conflicts?, default
‘abort’, valid choices are: ‘abort’, ‘proceed’
• default_operator – The default operator for query string query (AND or OR), de-
fault ‘OR’, valid choices are: ‘AND’, ‘OR’
• df – The field to use as default where no field prefix is given in the query string
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• from_ – Starting offset (default: 0)
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
• lenient – Specify whether format-based query failures (such as providing text to a
numeric field) should be ignored
• preference – Specify the node or shard the operation should be performed on (default:
random)
• q – Query in the Lucene query string syntax
• refresh – Should the effected indexes be refreshed?
• request_cache – Specify if request cache should be used for this request or not, de-
faults to index level setting
• requests_per_second – The throttle for this request in sub-requests per second. -1
means no throttle., default 0
• routing – A comma-separated list of specific routing values
• scroll – Specify how long a consistent view of the index should be maintained for
scrolled search
• scroll_size – Size on the scroll request powering the update_by_query
24 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
• version_type – Specific version type, valid choices are: ‘internal’, ‘external’, ‘exter-
nal_gte’, ‘force’
exists_source(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/master/docs-get.html
Parameters
• index – The name of the index
• id – The document ID
• _source – True or false to return the _source field or not, or a list of fields to return
• _source_exclude – A list of fields to exclude from the returned _source field
• _source_include – A list of fields to extract and return from the _source field
• parent – The ID of the parent document
• preference – Specify the node or shard the operation should be performed on (default:
random)
• realtime – Specify whether to perform the operation in realtime or search mode
• refresh – Refresh the shard containing the document before performing the operation
• routing – Specific routing value
• version – Explicit version number for concurrency control
• version_type – Specific version type, valid choices are: ‘internal’, ‘external’, ‘exter-
nal_gte’, ‘force’
explain(**kwargs)
The explain api computes a score explanation for a query and a specific document. This can give useful
feedback whether a document matches or didn’t match a specific query. http://www.elastic.co/guide/en/
elasticsearch/reference/current/search-explain.html
Parameters
• index – The name of the index
• id – The document ID
• body – The query definition using the Query DSL
• _source – True or false to return the _source field or not, or a list of fields to return
• _source_exclude – A list of fields to exclude from the returned _source field
• _source_include – A list of fields to extract and return from the _source field
• analyze_wildcard – Specify whether wildcards and prefix queries in the query string
query should be analyzed (default: false)
• analyzer – The analyzer for the query string query
• default_operator – The default operator for query string query (AND or OR), de-
fault ‘OR’, valid choices are: ‘AND’, ‘OR’
• df – The default field for query string query (default: _all)
• lenient – Specify whether format-based query failures (such as providing text to a
numeric field) should be ignored
• parent – The ID of the parent document
• preference – Specify the node or shard the operation should be performed on (default:
random)
• q – Query in the Lucene query string syntax
• routing – Specific routing value
• stored_fields – A comma-separated list of stored fields to return in the response
field_caps(**kwargs)
The field capabilities API allows to retrieve the capabilities of fields among multiple indices. http://www.
elastic.co/guide/en/elasticsearch/reference/current/search-field-caps.html
Parameters
• index – A list of index names, or a string containing a comma-separated list of index
names; use _all or the empty string to perform the operation on all indices
• body – Field json objects containing an array of field names
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• fields – A comma-separated list of field names
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
get(**kwargs)
Get a typed JSON document from the index based on its id. http://www.elastic.co/guide/en/elasticsearch/
reference/current/docs-get.html
Parameters
• index – The name of the index
• id – The document ID
• _source – True or false to return the _source field or not, or a list of fields to return
• _source_exclude – A list of fields to exclude from the returned _source field
• _source_include – A list of fields to extract and return from the _source field
• parent – The ID of the parent document
• preference – Specify the node or shard the operation should be performed on (default:
random)
• realtime – Specify whether to perform the operation in realtime or search mode
• refresh – Refresh the shard containing the document before performing the operation
• routing – Specific routing value
• stored_fields – A comma-separated list of stored fields to return in the response
• version – Explicit version number for concurrency control
• version_type – Specific version type, valid choices are: ‘internal’, ‘external’, ‘exter-
nal_gte’, ‘force’
26 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
get_script(**kwargs)
Retrieve a script from the API. http://www.elastic.co/guide/en/elasticsearch/reference/master/
modules-scripting.html
Parameters id – Script ID
get_source(**kwargs)
Get the source of a document by it’s index, type and id. http://www.elastic.co/guide/en/elasticsearch/
reference/current/docs-get.html
Parameters
• index – The name of the index
• id – The document ID
• _source – True or false to return the _source field or not, or a list of fields to return
• _source_exclude – A list of fields to exclude from the returned _source field
• _source_include – A list of fields to extract and return from the _source field
• parent – The ID of the parent document
• preference – Specify the node or shard the operation should be performed on (default:
random)
• realtime – Specify whether to perform the operation in realtime or search mode
• refresh – Refresh the shard containing the document before performing the operation
• routing – Specific routing value
• version – Explicit version number for concurrency control
• version_type – Specific version type, valid choices are: ‘internal’, ‘external’, ‘exter-
nal_gte’, ‘force’
index(**kwargs)
Adds or updates a typed JSON document in a specific index, making it searchable. http://www.elastic.co/
guide/en/elasticsearch/reference/current/docs-index_.html
Parameters
• index – The name of the index
• body – The document
• id – Document ID
• doc_type – Document type, defaults to _doc. Not used on ES 7 clusters.
• op_type – Explicit operation type, default ‘index’, valid choices are: ‘index’, ‘create’
• parent – ID of the parent document
• pipeline – The pipeline id to preprocess incoming documents with
• refresh – If true then refresh the affected shards to make this operation visible to search,
if wait_for then wait for a refresh to make this operation visible to search, if false (the
default) then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
• routing – Specific routing value
• timeout – Explicit operation timeout
• timestamp – Explicit timestamp for the document
28 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
30 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
render_search_template(**kwargs)
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-template.html
Parameters
• id – The id of the stored search template
• body – The search definition template and its params
scroll(**kwargs)
Scroll a search request created by specifying the scroll parameter. http://www.elastic.co/guide/en/
elasticsearch/reference/current/search-request-scroll.html
Parameters
• scroll_id – The scroll ID
• body – The scroll ID if not passed by URL or query parameter.
• scroll – Specify how long a consistent view of the index should be maintained for
scrolled search
• rest_total_hits_as_int – This parameter is used to restore the total hits as a
number in the response. This param is added version 6.x to handle mixed cluster queries
where nodes are in multiple versions (7.0 and 6.latest)
search(**kwargs)
Execute a search query and get back search hits that match the query. http://www.elastic.co/guide/en/
elasticsearch/reference/current/search-search.html
Parameters
• index – A list of index names to search, or a string containing a comma-separated list of
index names to search; use _all or empty string to perform the operation on all indices
• body – The search definition using the Query DSL
• _source – True or false to return the _source field or not, or a list of fields to return
• _source_exclude – A list of fields to exclude from the returned _source field
• _source_include – A list of fields to extract and return from the _source field
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• allow_partial_search_results – Set to false to return an overall failure if the
request would produce partial results. Defaults to True, which will allow partial results in
the case of timeouts or partial failures
• analyze_wildcard – Specify whether wildcard and prefix queries should be analyzed
(default: false)
• analyzer – The analyzer to use for the query string
• batched_reduce_size – The number of shard results that should be reduced at once
on the coordinating node. This value should be used as a protection mechanism to reduce
the memory overhead per search request if the potential number of shards in the request
can be large., default 512
• default_operator – The default operator for query string query (AND or OR), de-
fault ‘OR’, valid choices are: ‘AND’, ‘OR’
• df – The field to use as default where no field prefix is given in the query string
32 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
• preference – Specify the node or shard the operation should be performed on (default:
random)
• profile – Specify whether to profile the query execution
• routing – A comma-separated list of specific routing values
• scroll – Specify how long a consistent view of the index should be maintained for
scrolled search
• search_type – Search operation type, valid choices are: ‘query_then_fetch’,
‘query_and_fetch’, ‘dfs_query_then_fetch’, ‘dfs_query_and_fetch’
• typed_keys – Specify whether aggregation and suggester names should be prefixed by
their respective types in the response
termvectors(**kwargs)
Returns information and statistics on terms in the fields of a particular document. The document could
be stored in the index or artificially provided by the user (Added in 1.4). Note that for documents stored
in the index, this is a near realtime API as the term vectors are not available until the next refresh. http:
//www.elastic.co/guide/en/elasticsearch/reference/current/docs-termvectors.html
Parameters
• index – The index in which the document resides.
• id – The id of the document, when not specified a doc param should be supplied.
• body – Define parameters and or supply a document to get termvectors for. See docu-
mentation.
• field_statistics – Specifies if document count, sum of document frequencies and
sum of total term frequencies should be returned., default True
• fields – A comma-separated list of fields to return.
• offsets – Specifies if term offsets should be returned., default True
• parent – Parent id of documents.
• payloads – Specifies if term payloads should be returned., default True
• positions – Specifies if term positions should be returned., default True
• preference – Specify the node or shard the operation should be performed on (default:
random).
• realtime – Specifies if request is real-time as opposed to near- real-time (default: true).
• routing – Specific routing value.
• term_statistics – Specifies if total term frequency and document frequency should
be returned., default False
• version – Explicit version number for concurrency control
• version_type – Specific version type, valid choices are: ‘internal’, ‘external’, ‘exter-
nal_gte’, ‘force’
update(**kwargs)
Update a document based on a script or partial data provided. http://www.elastic.co/guide/en/elasticsearch/
reference/current/docs-update.html
Parameters
• index – The name of the index
34 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
• id – Document ID
• body – The request definition using either script or partial doc
• _source – True or false to return the _source field or not, or a list of fields to return
• _source_exclude – A list of fields to exclude from the returned _source field
• _source_include – A list of fields to extract and return from the _source field
• fields – A comma-separated list of fields to return in the response
• if_seq_no –
• if_primary_term –
• lang – The script language (default: painless)
• parent – ID of the parent document. Is is only used for routing and when for the upsert
request
• refresh – If true then refresh the effected shards to make this operation visible to search,
if wait_for then wait for a refresh to make this operation visible to search, if false (the
default) then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_forarg
retry_on_conflict: Specify how many times should the operation be retried when a conflict
occurs (default: 0)
• routing – Specific routing value
• timeout – Explicit operation timeout
• timestamp – Explicit timestamp for the document
• ttl – Expiration time for the document
• version – Explicit version number for concurrency control
• version_type – Specific version type, valid choices are: ‘internal’, ‘force’
• wait_for_active_shards – Sets the number of shard copies that must be active
before proceeding with the update operation. Defaults to 1, meaning the primary shard
only. Set to all for all shard copies, otherwise set to any non-negative value less than or
equal to the total number of copies for the shard (number of replicas + 1)
update_by_query(**kwargs)
Perform an update on all documents matching a query. https://www.elastic.co/guide/en/elasticsearch/
reference/current/docs-update-by-query.html
Parameters
• index – A list of index names to search, or a string containing a comma-separated list of
index names to search; use _all or the empty string to perform the operation on all indices
• body – The search definition using the Query DSL
• _source – True or false to return the _source field or not, or a list of fields to return
• _source_exclude – A list of fields to exclude from the returned _source field
• _source_include – A list of fields to extract and return from the _source field
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• analyze_wildcard – Specify whether wildcard and prefix queries should be analyzed
(default: false)
36 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
7.1.3 Indices
class elasticsearch.client.IndicesClient(client)
analyze(**kwargs)
Perform the analysis process on a text and return the tokens breakdown of the text. http://www.elastic.co/
guide/en/elasticsearch/reference/current/indices-analyze.html
Parameters
• index – The name of the index to scope the operation
• body – Define analyzer/tokenizer parameters and the text on which the analysis should
be performed
• format – Format of the output, default ‘detailed’, valid choices are: ‘detailed’, ‘text’
• prefer_local – With true, specify that a local shard should be used if available, with
false, use a random shard (default: true)
clear_cache(**kwargs)
Clear either all caches or specific cached associated with one ore more indices. http://www.elastic.co/
guide/en/elasticsearch/reference/current/indices-clearcache.html
Parameters
• index – A comma-separated list of index name to limit the operation
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• field_data – Clear field data
• fielddata – Clear field data
• fields – A comma-separated list of fields to clear when using the field_data parameter
(default: all)
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
• query – Clear query caches
• recycler – Clear the recycler cache
• request – Clear request cache
• request_cache – Clear request cache
close(**kwargs)
Close an index to remove it’s overhead from the cluster. Closed index is blocked for read/write operations.
http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html
Parameters
• index – The name of the index
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
• master_timeout – Specify timeout for connection to master
• timeout – Explicit operation timeout
create(**kwargs)
Create an index in Elasticsearch. http://www.elastic.co/guide/en/elasticsearch/reference/current/
indices-create-index.html
Parameters
• index – The name of the index
• body – The configuration for the index (settings and mappings)
• master_timeout – Specify timeout for connection to master
• timeout – Explicit operation timeout
• wait_for_active_shards – Set the number of active shards to wait for before the
operation returns.
• include_type_name – Specify whether requests and responses should include a type
name (default: depends on Elasticsearch version).
delete(**kwargs)
Delete an index in Elasticsearch http://www.elastic.co/guide/en/elasticsearch/reference/current/
indices-delete-index.html
Parameters
• index – A comma-separated list of indices to delete; use _all or * string to delete all
indices
• allow_no_indices – Ignore if a wildcard expression resolves to no concrete indices
(default: false)
• expand_wildcards – Whether wildcard expressions should get expanded to open or
closed indices (default: open), default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’,
‘all’
• ignore_unavailable – Ignore unavailable indexes (default: false)
• master_timeout – Specify timeout for connection to master
• timeout – Explicit operation timeout
delete_alias(**kwargs)
Delete specific alias. http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html
Parameters
• index – A comma-separated list of index names (supports wildcards); use _all for all
indices
38 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
• name – A comma-separated list of aliases to delete (supports wildcards); use _all to delete
all aliases for the specified indices.
• master_timeout – Specify timeout for connection to master
• timeout – Explicit timeout for the operation
delete_template(**kwargs)
Delete an index template by its name. http://www.elastic.co/guide/en/elasticsearch/reference/current/
indices-templates.html
Parameters
• name – The name of the template
• master_timeout – Specify timeout for connection to master
• timeout – Explicit operation timeout
exists(**kwargs)
Return a boolean indicating whether given index exists. http://www.elastic.co/guide/en/elasticsearch/
reference/current/indices-exists.html
Parameters
• index – A comma-separated list of index names
• allow_no_indices – Ignore if a wildcard expression resolves to no concrete indices
(default: false)
• expand_wildcards – Whether wildcard expressions should get expanded to open or
closed indices (default: open), default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’,
‘all’
• flat_settings – Return settings in flat format (default: false)
• ignore_unavailable – Ignore unavailable indexes (default: false)
• include_defaults – Whether to return all default setting for each of the indices.,
default False
• local – Return local information, do not retrieve the state from master node (default:
false)
exists_alias(**kwargs)
Return a boolean indicating whether given alias exists. http://www.elastic.co/guide/en/elasticsearch/
reference/current/indices-aliases.html
Parameters
• index – A comma-separated list of index names to filter aliases
• name – A comma-separated list of alias names to return
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘all’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
• local – Return local information, do not retrieve the state from master node (default:
false)
exists_template(**kwargs)
Return a boolean indicating whether given template exists. http://www.elastic.co/guide/en/elasticsearch/
reference/current/indices-templates.html
Parameters
• name – The comma separated names of the index templates
• flat_settings – Return settings in flat format (default: false)
• local – Return local information, do not retrieve the state from master node (default:
false)
• master_timeout – Explicit operation timeout for connection to master node
exists_type(**kwargs)
Check if a type/types exists in an index/indices. http://www.elastic.co/guide/en/elasticsearch/reference/
current/indices-types-exists.html
Parameters
• index – A comma-separated list of index names; use _all to check the types across all
indices
• doc_type – A comma-separated list of document types to check
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
• local – Return local information, do not retrieve the state from master node (default:
false)
flush(**kwargs)
Explicitly flush one or more indices. http://www.elastic.co/guide/en/elasticsearch/reference/current/
indices-flush.html
Parameters
• index – A comma-separated list of index names; use _all or empty string for all indices
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• force – Whether a flush should be forced even if it is not necessarily needed ie. if no
changes will be committed to the index. This is useful if transaction log IDs should be
incremented even if no uncommitted changes are present. (This setting can be considered
as internal)
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
• wait_if_ongoing – If set to true the flush operation will block until the flush can be
executed if another flush operation is already executing. The default is true. If set to false
the flush will be skipped iff if another flush operation is already running.
40 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
flush_synced(**kwargs)
Perform a normal flush, then add a generated unique marker (sync_id) to all shards. http://www.elastic.co/
guide/en/elasticsearch/reference/current/indices-synced-flush.html
Parameters
• index – A comma-separated list of index names; use _all or empty string for all indices
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
forcemerge(**kwargs)
The force merge API allows to force merging of one or more indices through an API. The merge relates
to the number of segments a Lucene index holds within each shard. The force merge operation allows to
reduce the number of segments by merging them.
This call will block until the merge is complete. If the http connection is lost, the request will continue
in the background, and any new requests will block until the previous force merge is complete. http:
//www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html
Parameters
• index – A comma-separated list of index names; use _all or empty string to perform the
operation on all indices
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• flush – Specify whether the index should be flushed after performing the operation
(default: true)
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
• max_num_segments – The number of segments the index should be merged into (de-
fault: dynamic)
• only_expunge_deletes – Specify whether the operation should only expunge
deleted documents
• operation_threading – TODO: ?
get(**kwargs)
The get index API allows to retrieve information about one or more indexes. http://www.elastic.co/guide/
en/elasticsearch/reference/current/indices-get-index.html
Parameters
• index – A comma-separated list of index names
• allow_no_indices – Ignore if a wildcard expression resolves to no concrete indices
(default: false)
42 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
44 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
put_mapping(**kwargs)
Register specific mapping definition for a specific type. http://www.elastic.co/guide/en/elasticsearch/
reference/current/indices-put-mapping.html
Parameters
• doc_type – The name of the document type
• body – The mapping definition
• index – A comma-separated list of index names the mapping should be added to (sup-
ports wildcards); use _all or omit to add the mapping on all indices.
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
• master_timeout – Specify timeout for connection to master
• timeout – Explicit operation timeout
• include_type_name – Specify whether requests and responses should include a type
name (default: depends on Elasticsearch version).
put_settings(**kwargs)
Change specific index level settings in real time. http://www.elastic.co/guide/en/elasticsearch/reference/
current/indices-update-settings.html
Parameters
• body – The index settings to be updated
• index – A comma-separated list of index names; use _all or empty string to perform the
operation on all indices
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• flat_settings – Return settings in flat format (default: false)
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
• master_timeout – Specify timeout for connection to master
• preserve_existing – Whether to update existing settings. If set to true existing
settings on an index remain unchanged, the default is false
put_template(**kwargs)
Create an index template that will automatically be applied to new indices created. http://www.elastic.co/
guide/en/elasticsearch/reference/current/indices-templates.html
Parameters
• name – The name of the template
• body – The template definition
• create – Whether the index template should only be added if new or can also replace an
existing one, default False
• flat_settings – Return settings in flat format (default: false)
• master_timeout – Specify timeout for connection to master
• order – The order for this template when merging multiple matching ones (higher num-
bers are merged later, overriding the lower numbers)
• timeout – Explicit operation timeout
• include_type_name – Specify whether requests and responses should include a type
name (default: depends on Elasticsearch version).
recovery(**kwargs)
The indices recovery API provides insight into on-going shard recoveries. Recovery status may be re-
ported for specific indices, or cluster-wide. http://www.elastic.co/guide/en/elasticsearch/reference/current/
indices-recovery.html
Parameters
• index – A comma-separated list of index names; use _all or empty string to perform the
operation on all indices
• active_only – Display only those recoveries that are currently on- going, default False
• detailed – Whether to display detailed information about shard recovery, default False
refresh(**kwargs)
Explicitly refresh one or more index, making all operations performed since the last refresh available for
search. http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html
Parameters
• index – A comma-separated list of index names; use _all or empty string to perform the
operation on all indices
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
rollover(**kwargs)
The rollover index API rolls an alias over to a new index when the existing index is considered to be too
large or too old.
The API accepts a single alias name and a list of conditions. The alias must point to a single index only. If
the index satisfies the specified conditions then a new index is created and the alias is switched to point to
the new alias. http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html
Parameters
• alias – The name of the alias to rollover
• new_index – The name of the rollover index
• body – The conditions that needs to be met for executing rollover
• dry_run – If set to true the rollover action will only be validated but not actually per-
formed even if a condition matches. The default is false
46 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
48 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
• index – A comma-separated list of index names; use _all or empty string to perform the
operation on all indices
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
• only_ancient_segments – If true, only ancient (an older Lucene major release)
segments will be upgraded
• wait_for_completion – Specify whether the request should block until the all seg-
ments are upgraded (default: false)
validate_query(**kwargs)
Validate a potentially expensive query without executing it. http://www.elastic.co/guide/en/elasticsearch/
reference/current/search-validate.html
Parameters
• index – A comma-separated list of index names to restrict the operation; use _all or
empty string to perform the operation on all indices
• doc_type – A comma-separated list of document types to restrict the operation; leave
empty to perform the operation on all types
• body – The query definition specified with the Query DSL
• all_shards – Execute validation on all shards instead of one random shard per index
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• analyze_wildcard – Specify whether wildcard and prefix queries should be analyzed
(default: false)
• analyzer – The analyzer to use for the query string
• default_operator – The default operator for query string query (AND or OR), de-
fault ‘OR’, valid choices are: ‘AND’, ‘OR’
• df – The field to use as default where no field prefix is given in the query string
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• explain – Return detailed information about the error
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
• lenient – Specify whether format-based query failures (such as providing text to a
numeric field) should be ignored
• operation_threading – TODO: ?
• q – Query in the Lucene query string syntax
• rewrite – Provide a more detailed explanation showing the actual Lucene query that
will be executed.
7.1.4 Ingest
class elasticsearch.client.IngestClient(client)
delete_pipeline(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/plugins/current/ingest.html
Parameters
• id – Pipeline ID
• master_timeout – Explicit operation timeout for connection to master node
• timeout – Explicit operation timeout
get_pipeline(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/plugins/current/ingest.html
Parameters
• id – Comma separated list of pipeline ids. Wildcards supported
• master_timeout – Explicit operation timeout for connection to master node
put_pipeline(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/plugins/current/ingest.html
Parameters
• id – Pipeline ID
• body – The ingest definition
• master_timeout – Explicit operation timeout for connection to master node
• timeout – Explicit operation timeout
simulate(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/plugins/current/ingest.html
Parameters
• body – The simulate definition
• id – Pipeline ID
• verbose – Verbose mode. Display data output for each processor in executed pipeline,
default False
7.1.5 Cluster
class elasticsearch.client.ClusterClient(client)
allocation_explain(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-allocation-explain.html
Parameters
• body – The index, shard, and primary flag to explain. Empty means ‘explain the first
unassigned shard’
• include_disk_info – Return information about disk usage and shard sizes (default:
false)
50 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
• body – The settings to be updated. Can be either transient or persistent (survives cluster
restart).
• flat_settings – Return settings in flat format (default: false)
• master_timeout – Explicit operation timeout for connection to master node
• timeout – Explicit operation timeout
reroute(**kwargs)
Explicitly execute a cluster reroute allocation command including specific commands. http://www.elastic.
co/guide/en/elasticsearch/reference/current/cluster-reroute.html
Parameters
• body – The definition of commands to perform (move, cancel, allocate)
• dry_run – Simulate the operation only and return the resulting state
• explain – Return an explanation of why the commands can or cannot be executed
• master_timeout – Explicit operation timeout for connection to master node
• metric – Limit the information returned to the specified metrics. Defaults to all but
metadata, valid choices are: ‘_all’, ‘blocks’, ‘metadata’, ‘nodes’, ‘routing_table’, ‘mas-
ter_node’, ‘version’
• retry_failed – Retries allocation of shards that are blocked due to too many subse-
quent allocation failures
• timeout – Explicit operation timeout
state(**kwargs)
Get a comprehensive state information of the whole cluster. http://www.elastic.co/guide/en/elasticsearch/
reference/current/cluster-state.html
Parameters
• metric – Limit the information returned to the specified metrics
• index – A comma-separated list of index names; use _all or empty string to perform the
operation on all indices
• allow_no_indices – Whether to ignore if a wildcard indices expression resolves into
no concrete indices. (This includes _all string or when no indices have been specified)
• expand_wildcards – Whether to expand wildcard expression to concrete indices that
are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’
• flat_settings – Return settings in flat format (default: false)
• ignore_unavailable – Whether specified concrete indices should be ignored when
unavailable (missing or closed)
• local – Return local information, do not retrieve the state from master node (default:
false)
• master_timeout – Specify timeout for connection to master
stats(**kwargs)
The Cluster Stats API allows to retrieve statistics from a cluster wide perspective. The API returns basic
index metrics and information about the current nodes that form the cluster. http://www.elastic.co/guide/
en/elasticsearch/reference/current/cluster-stats.html
Parameters
52 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
• node_id – A comma-separated list of node IDs or names to limit the returned informa-
tion; use _local to return information from the node you’re connecting to, leave empty to
get information from all nodes
• flat_settings – Return settings in flat format (default: false)
• timeout – Explicit operation timeout
7.1.6 Nodes
class elasticsearch.client.NodesClient(client)
hot_threads(**kwargs)
An API allowing to get the current hot threads on each node in the cluster. https://www.elastic.co/guide/
en/elasticsearch/reference/current/cluster-nodes-hot-threads.html
Parameters
• node_id – A comma-separated list of node IDs or names to limit the returned informa-
tion; use _local to return information from the node you’re connecting to, leave empty to
get information from all nodes
• type – The type to sample (default: cpu), valid choices are: ‘cpu’, ‘wait’, ‘block’
• ignore_idle_threads – Don’t show threads that are in known-idle places, such as
waiting on a socket select or pulling from an empty task queue (default: true)
• interval – The interval for the second sampling of threads
• snapshots – Number of samples of thread stacktrace (default: 10)
• threads – Specify the number of threads to provide information for (default: 3)
• timeout – Explicit operation timeout
info(**kwargs)
The cluster nodes info API allows to retrieve one or more (or all) of the cluster nodes information. https:
//www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-info.html
Parameters
• node_id – A comma-separated list of node IDs or names to limit the returned informa-
tion; use _local to return information from the node you’re connecting to, leave empty to
get information from all nodes
• metric – A comma-separated list of metrics you wish returned. Leave empty to return
all.
• flat_settings – Return settings in flat format (default: false)
• timeout – Explicit operation timeout
reload_secure_settings(**kwargs)
Reload any settings that have been marked as “reloadable” https://www.elastic.co/guide/en/elasticsearch/
reference/current/secure-settings.html#reloadable-secure-settings
stats(**kwargs)
The cluster nodes stats API allows to retrieve one or more (or all) of the cluster nodes statistics. https:
//www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-stats.html
Parameters
• node_id – A comma-separated list of node IDs or names to limit the returned informa-
tion; use _local to return information from the node you’re connecting to, leave empty to
get information from all nodes
• metric – Limit the information returned to the specified metrics
• index_metric – Limit the information returned for indices metric to the specific index
metrics. Isn’t used if indices (or all) metric isn’t specified.
• completion_fields – A comma-separated list of fields for fielddata and suggest in-
dex metric (supports wildcards)
• fielddata_fields – A comma-separated list of fields for fielddata index metric (sup-
ports wildcards)
• fields – A comma-separated list of fields for fielddata and completion index metric
(supports wildcards)
• groups – A comma-separated list of search groups for search index metric
• include_segment_file_sizes – Whether to report the aggregated disk usage of
each one of the Lucene index files (only applies if segment stats are requested), default
False
• level – Return indices stats aggregated at index, node or shard level, default ‘node’,
valid choices are: ‘indices’, ‘node’, ‘shards’
• timeout – Explicit operation timeout
• types – A comma-separated list of document types for the indexing index metric
usage(**kwargs)
The cluster nodes usage API allows to retrieve information on the usage of features for each node. http:
//www.elastic.co/guide/en/elasticsearch/reference/master/cluster-nodes-usage.html
Parameters
• node_id – A comma-separated list of node IDs or names to limit the returned informa-
tion; use _local to return information from the node you’re connecting to, leave empty to
get information from all nodes
• metric – Limit the information returned to the specified metrics
• human – Whether to return time and byte values in human-readable format., default False
• timeout – Explicit operation timeout
7.1.7 Cat
class elasticsearch.client.CatClient(client)
aliases(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-alias.html
Parameters
• name – A comma-separated list of alias names to return
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• help – Return help information, default False
54 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
• local – Return local information, do not retrieve the state from master node (default:
false)
• master_timeout – Explicit operation timeout for connection to master node
• s – Comma-separated list of column names or column aliases to sort by
• v – Verbose mode. Display column headers, default False
allocation(**kwargs)
Allocation provides a snapshot of how shards have located around the cluster and the state of disk usage.
https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-allocation.html
Parameters
• node_id – A comma-separated list of node IDs or names to limit the returned informa-
tion
• bytes – The unit in which to display byte values, valid choices are: ‘b’, ‘k’, ‘kb’, ‘m’,
‘mb’, ‘g’, ‘gb’, ‘t’, ‘tb’, ‘p’, ‘pb’
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• help – Return help information, default False
• local – Return local information, do not retrieve the state from master node (default:
false)
• master_timeout – Explicit operation timeout for connection to master node
• s – Comma-separated list of column names or column aliases to sort by
• v – Verbose mode. Display column headers, default False
count(**kwargs)
Count provides quick access to the document count of the entire cluster, or individual indices. https:
//www.elastic.co/guide/en/elasticsearch/reference/current/cat-count.html
Parameters
• index – A comma-separated list of index names to limit the returned information
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• help – Return help information, default False
• local – Return local information, do not retrieve the state from master node (default:
false)
• master_timeout – Explicit operation timeout for connection to master node
• s – Comma-separated list of column names or column aliases to sort by
• v – Verbose mode. Display column headers, default False
fielddata(**kwargs)
Shows information about currently loaded fielddata on a per-node basis. https://www.elastic.co/guide/en/
elasticsearch/reference/current/cat-fielddata.html
Parameters
• fields – A comma-separated list of fields to return the fielddata size
• bytes – The unit in which to display byte values, valid choices are: ‘b’, ‘k’, ‘kb’, ‘m’,
‘mb’, ‘g’, ‘gb’, ‘t’, ‘tb’, ‘p’, ‘pb’
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• help – Return help information, default False
• local – Return local information, do not retrieve the state from master node (default:
false)
• master_timeout – Explicit operation timeout for connection to master node
• s – Comma-separated list of column names or column aliases to sort by
• v – Verbose mode. Display column headers, default False
health(**kwargs)
health is a terse, one-line representation of the same information from health() API https://www.elastic.
co/guide/en/elasticsearch/reference/current/cat-health.html
Parameters
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• help – Return help information, default False
• local – Return local information, do not retrieve the state from master node (default:
false)
• master_timeout – Explicit operation timeout for connection to master node
• s – Comma-separated list of column names or column aliases to sort by
• ts – Set to false to disable timestamping, default True
• v – Verbose mode. Display column headers, default False
help(**kwargs)
A simple help for the cat api. https://www.elastic.co/guide/en/elasticsearch/reference/current/cat.html
Parameters
• help – Return help information, default False
• s – Comma-separated list of column names or column aliases to sort by
indices(**kwargs)
The indices command provides a cross-section of each index. https://www.elastic.co/guide/en/
elasticsearch/reference/current/cat-indices.html
Parameters
• index – A comma-separated list of index names to limit the returned information
• bytes – The unit in which to display byte values, valid choices are: ‘b’, ‘k’, ‘m’, ‘g’
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• health – A health status (“green”, “yellow”, or “red” to filter only indices matching the
specified health status, default None, valid choices are: ‘green’, ‘yellow’, ‘red’
• help – Return help information, default False
56 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
• local – Return local information, do not retrieve the state from master node (default:
false)
• master_timeout – Explicit operation timeout for connection to master node
• pri – Set to true to return stats only for primary shards, default False
• s – Comma-separated list of column names or column aliases to sort by
• v – Verbose mode. Display column headers, default False
master(**kwargs)
Displays the master’s node ID, bound IP address, and node name. https://www.elastic.co/guide/en/
elasticsearch/reference/current/cat-master.html
Parameters
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• help – Return help information, default False
• local – Return local information, do not retrieve the state from master node (default:
false)
• master_timeout – Explicit operation timeout for connection to master node
• s – Comma-separated list of column names or column aliases to sort by
• v – Verbose mode. Display column headers, default False
nodeattrs(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-nodeattrs.html
Parameters
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• help – Return help information, default False
• local – Return local information, do not retrieve the state from master node (default:
false)
• master_timeout – Explicit operation timeout for connection to master node
• s – Comma-separated list of column names or column aliases to sort by
• v – Verbose mode. Display column headers, default False
nodes(**kwargs)
The nodes command shows the cluster topology. https://www.elastic.co/guide/en/elasticsearch/reference/
current/cat-nodes.html
Parameters
• format – a short version of the Accept header, e.g. json, yaml
• full_id – Return the full node ID instead of the shortened version (default: false)
• h – Comma-separated list of column names to display
• help – Return help information, default False
• local – Return local information, do not retrieve the state from master node (default:
false)
58 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
repositories(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-repositories.html
Parameters
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• help – Return help information, default False
• local – Return local information, do not retrieve the state from master node, default
False
• master_timeout – Explicit operation timeout for connection to master node
• s – Comma-separated list of column names or column aliases to sort by
• v – Verbose mode. Display column headers, default False
segments(**kwargs)
The segments command is the detailed view of Lucene segments per index. https://www.elastic.co/guide/
en/elasticsearch/reference/current/cat-segments.html
Parameters
• index – A comma-separated list of index names to limit the returned information
• bytes – The unit in which to display byte values, valid choices are: ‘b’, ‘k’, ‘kb’, ‘m’,
‘mb’, ‘g’, ‘gb’, ‘t’, ‘tb’, ‘p’, ‘pb’
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• help – Return help information, default False
• s – Comma-separated list of column names or column aliases to sort by
• v – Verbose mode. Display column headers, default False
shards(**kwargs)
The shards command is the detailed view of what nodes contain which shards. https://www.elastic.co/
guide/en/elasticsearch/reference/current/cat-shards.html
Parameters
• index – A comma-separated list of index names to limit the returned information
• bytes – The unit in which to display byte values, valid choices are: ‘b’, ‘k’, ‘kb’, ‘m’,
‘mb’, ‘g’, ‘gb’, ‘t’, ‘tb’, ‘p’, ‘pb’
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• help – Return help information, default False
• local – Return local information, do not retrieve the state from master node (default:
false)
• master_timeout – Explicit operation timeout for connection to master node
• s – Comma-separated list of column names or column aliases to sort by
• v – Verbose mode. Display column headers, default False
snapshots(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-snapshots.html
Parameters
• repository – Name of repository from which to fetch the snapshot information
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• help – Return help information, default False
• ignore_unavailable – Set to true to ignore unavailable snapshots, default False
• master_timeout – Explicit operation timeout for connection to master node
• s – Comma-separated list of column names or column aliases to sort by
• v – Verbose mode. Display column headers, default False
tasks(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html
Parameters
• actions – A comma-separated list of actions that should be returned. Leave empty to
return all.
• detailed – Return detailed task information (default: false)
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• help – Return help information, default False
• nodes – A comma-separated list of node IDs or names to limit the returned information;
use _local to return information from the node you’re connecting to, leave empty to get
information from all nodes
• parent_task_id – Return tasks with specified parent task id. Set to -1 to return all.
• s – Comma-separated list of column names or column aliases to sort by
• v – Verbose mode. Display column headers, default False
templates(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-templates.html
Parameters
• name – A pattern that returned template names must match
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• help – Return help information, default False
• local – Return local information, do not retrieve the state from master node (default:
false)
• master_timeout – Explicit operation timeout for connection to master node
• s – Comma-separated list of column names or column aliases to sort by
• v – Verbose mode. Display column headers, default False
60 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
thread_pool(**kwargs)
Get information about thread pools. https://www.elastic.co/guide/en/elasticsearch/reference/current/
cat-thread-pool.html
Parameters
• thread_pool_patterns – A comma-separated list of regular-expressions to filter the
thread pools in the output
• format – a short version of the Accept header, e.g. json, yaml
• h – Comma-separated list of column names to display
• help – Return help information, default False
• local – Return local information, do not retrieve the state from master node (default:
false)
• master_timeout – Explicit operation timeout for connection to master node
• s – Comma-separated list of column names or column aliases to sort by
• size – The multiplier in which to display values, valid choices are: ‘’, ‘k’, ‘m’, ‘g’, ‘t’,
‘p’
• v – Verbose mode. Display column headers, default False
7.1.8 Snapshot
class elasticsearch.client.SnapshotClient(client)
create(**kwargs)
Create a snapshot in repository http://www.elastic.co/guide/en/elasticsearch/reference/current/
modules-snapshots.html
Parameters
• repository – A repository name
• snapshot – A snapshot name
• body – The snapshot definition
• master_timeout – Explicit operation timeout for connection to master node
• wait_for_completion – Should this request wait until the operation has completed
before returning, default False
create_repository(**kwargs)
Registers a shared file system repository. http://www.elastic.co/guide/en/elasticsearch/reference/current/
modules-snapshots.html
Parameters
• repository – A repository name
• body – The repository definition
• master_timeout – Explicit operation timeout for connection to master node
• timeout – Explicit operation timeout
• verify – Whether to verify the repository after creation
delete(**kwargs)
Deletes a snapshot from a repository. http://www.elastic.co/guide/en/elasticsearch/reference/current/
modules-snapshots.html
Parameters
• repository – A repository name
• snapshot – A snapshot name
• master_timeout – Explicit operation timeout for connection to master node
delete_repository(**kwargs)
Removes a shared file system repository. http://www.elastic.co/guide/en/elasticsearch/reference/current/
modules-snapshots.html
Parameters
• repository – A comma-separated list of repository names
• master_timeout – Explicit operation timeout for connection to master node
• timeout – Explicit operation timeout
get(**kwargs)
Retrieve information about a snapshot. http://www.elastic.co/guide/en/elasticsearch/reference/current/
modules-snapshots.html
Parameters
• repository – A repository name
• snapshot – A comma-separated list of snapshot names
• ignore_unavailable – Whether to ignore unavailable snapshots, defaults to false
which means a NotFoundError snapshot_missing_exception is thrown
• master_timeout – Explicit operation timeout for connection to master node
• verbose – Whether to show verbose snapshot info or only show the basic info found in
the repository index blob
get_repository(**kwargs)
Return information about registered repositories. http://www.elastic.co/guide/en/elasticsearch/reference/
current/modules-snapshots.html
Parameters
• repository – A comma-separated list of repository names
• local – Return local information, do not retrieve the state from master node (default:
false)
• master_timeout – Explicit operation timeout for connection to master node
restore(**kwargs)
Restore a snapshot. http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-snapshots.
html
Parameters
• repository – A repository name
• snapshot – A snapshot name
• body – Details of what to restore
62 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
7.1.9 Tasks
class elasticsearch.client.TasksClient(client)
cancel(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html
Parameters
• task_id – Cancel the task with specified task id (node_id:task_number)
• actions – A comma-separated list of actions that should be cancelled. Leave empty to
cancel all.
• nodes – A comma-separated list of node IDs or names to limit the returned information;
use _local to return information from the node you’re connecting to, leave empty to get
information from all nodes
• parent_task_id – Cancel tasks with specified parent task id (node_id:task_number).
Set to -1 to cancel all.
get(**kwargs)
Retrieve information for a particular task. http://www.elastic.co/guide/en/elasticsearch/reference/current/
tasks.html
Parameters
• task_id – Return the task with specified id (node_id:task_number)
• wait_for_completion – Wait for the matching tasks to complete (default: false)
X-Pack is an Elastic Stack extension that bundles security, alerting, monitoring, reporting, and graph capabilities into
one easy-to-install package. While the X-Pack components are designed to work together seamlessly, you can easily
enable or disable the features you want to use.
7.2.1 Info
info(**kwargs)
Retrieve information about xpack, including build number/timestamp and license status https://www.
elastic.co/guide/en/elasticsearch/reference/current/info-api.html
Parameters
• categories – Comma-separated list of info categories. Can be any of: build, license,
features
• human – Presents additional info for humans (feature descriptions and X-Pack tagline)
usage(**kwargs)
Retrieve information about xpack features usage
Parameters master_timeout – Specify timeout for watch write operation
64 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
X-Pack Graph Explore enables you to extract and summarize information about the documents and terms in your
Elasticsearch index.
class elasticsearch.client.xpack.graph.GraphClient(client)
explore(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/graph-explore-api.html
Parameters
• index – A comma-separated list of index names to search; use _all or empty string to
perform the operation on all indices
• doc_type – A comma-separated list of document types to search; leave empty to per-
form the operation on all types
• body – Graph Query DSL
• routing – Specific routing value
• timeout – Explicit operation timeout
delete(**kwargs)
https://www.elastic.co/guide/en/x-pack/current/license-management.html
get(**kwargs)
https://www.elastic.co/guide/en/x-pack/current/license-management.html
Parameters local – Return local information, do not retrieve the state from master node (de-
fault: false)
get_basic_status(**kwargs)
https://www.elastic.co/guide/en/x-pack/current/license-management.html
get_trial_status(**kwargs)
https://www.elastic.co/guide/en/x-pack/current/license-management.html
post(**kwargs)
https://www.elastic.co/guide/en/x-pack/current/license-management.html
Parameters
• body – licenses to be installed
• acknowledge – whether the user has acknowledged acknowledge messages (default:
false)
post_start_basic(**kwargs)
https://www.elastic.co/guide/en/x-pack/current/license-management.html
Parameters acknowledge – whether the user has acknowledged acknowledge messages (de-
fault: false)
post_start_trial(**kwargs)
https://www.elastic.co/guide/en/x-pack/current/license-management.html
Parameters
• acknowledge – whether the user has acknowledged acknowledge messages (default:
false)
• doc_type – The type of trial license to generate (default: “trial”)
Machine Learning can be useful for discovering new patterns about your data. For a more detailed explanation about
X-Pack’s machine learning please refer to the official documentation.
class elasticsearch.client.xpack.ml.MlClient(client)
close_job(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-close-job.html
Parameters
• job_id – The name of the job to close
• body – The URL params optionally sent in the body
• allow_no_jobs – Whether to ignore if a wildcard expression matches no jobs. (This
includes _all string or when no jobs have been specified)
• force – True if the job should be forcefully closed
• timeout – Controls the time to wait until a job has closed. Default to 30 minutes
delete_calendar(**kwargs)
‘<>‘_
Parameters calendar_id – The ID of the calendar to delete
delete_calendar_event(**kwargs)
‘<>‘_
Parameters
• calendar_id – The ID of the calendar to modify
• event_id – The ID of the event to remove from the calendar
delete_calendar_job(**kwargs)
‘<>‘_
Parameters
• calendar_id – The ID of the calendar to modify
• job_id – The ID of the job to remove from the calendar
delete_datafeed(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-delete-datafeed.html
Parameters
• datafeed_id – The ID of the datafeed to delete
• force – True if the datafeed should be forcefully deleted
66 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
delete_expired_data(**kwargs)
‘<>‘_
delete_filter(**kwargs)
‘<>‘_
Parameters filter_id – The ID of the filter to delete
delete_forecast(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-delete-forecast.html
Parameters
• job_id – The ID of the job from which to delete forecasts
• forecast_id – The ID of the forecast to delete, can be comma delimited list. Leaving
blank implies _all
• allow_no_forecasts – Whether to ignore if _all matches no forecasts
• timeout – Controls the time to wait until the forecast(s) are deleted. Default to 30
seconds
delete_job(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-delete-job.html
Parameters
• job_id – The ID of the job to delete
• force – True if the job should be forcefully deleted, default False
• wait_for_completion – Should this request wait until the operation has completed
before returning, default True
delete_model_snapshot(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-delete-snapshot.html
Parameters
• job_id – The ID of the job to fetch
• snapshot_id – The ID of the snapshot to delete
find_file_structure(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-file-structure.html
Parameters
• body – The contents of the file to be analyzed
• charset – Optional parameter to specify the character set of the file
• column_names – Optional parameter containing a comma separated list of the column
names for a delimited file
• delimiter – Optional parameter to specify the delimiter character for a delimited file -
must be a single character
• explain – Whether to include a commentary on how the structure was derived, default
False
• format – Optional parameter to specify the high level file format, valid choices are:
‘ndjson’, ‘xml’, ‘delimited’, ‘semi_structured_text’
• grok_pattern – Optional parameter to specify the Grok pattern that should be used to
extract fields from messages in a semi-structured text file
68 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
get_datafeeds(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-datafeed.html
Parameters
• datafeed_id – The ID of the datafeeds to fetch
• allow_no_datafeeds – Whether to ignore if a wildcard expression matches no
datafeeds. (This includes _all string or when no datafeeds have been specified)
get_filters(**kwargs)
‘<>‘_
Parameters
• filter_id – The ID of the filter to fetch
• from – skips a number of filters
• size – specifies a max number of filters to get
get_influencers(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-influencer.html
Parameters
• job_id – None
• body – Influencer selection criteria
• desc – whether the results should be sorted in decending order
• end – end timestamp for the requested influencers
• exclude_interim – Exclude interim results
• from – skips a number of influencers
• influencer_score – influencer score threshold for the requested influencers
• size – specifies a max number of influencers to get
• sort – sort field for the requested influencers
• start – start timestamp for the requested influencers
get_job_stats(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-job-stats.html
Parameters
• job_id – The ID of the jobs stats to fetch
• allow_no_jobs – Whether to ignore if a wildcard expression matches no jobs. (This
includes _all string or when no jobs have been specified)
get_jobs(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-job.html
Parameters
• job_id – The ID of the jobs to fetch
• allow_no_jobs – Whether to ignore if a wildcard expression matches no jobs. (This
includes _all string or when no jobs have been specified)
get_model_snapshots(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-snapshot.html
70 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
Parameters
• job_id – The ID of the job to fetch
• snapshot_id – The ID of the snapshot to fetch
• body – Model snapshot selection criteria
• desc – True if the results should be sorted in descending order
• end – The filter ‘end’ query parameter
• from – Skips a number of documents
• size – The default number of documents returned in queries as a string.
• sort – Name of the field to sort on
• start – The filter ‘start’ query parameter
get_overall_buckets(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-overall-buckets.html
Parameters
• job_id – The job IDs for which to calculate overall bucket results
• body – Overall bucket selection details if not provided in URI
• allow_no_jobs – Whether to ignore if a wildcard expression matches no jobs. (This
includes _all string or when no jobs have been specified)
• bucket_span – The span of the overall buckets. Defaults to the longest job bucket_span
• end – Returns overall buckets with timestamps earlier than this time
• exclude_interim – If true overall buckets that include interim buckets will be ex-
cluded
• overall_score – Returns overall buckets with overall scores higher than this value
• start – Returns overall buckets with timestamps after this time
• top_n – The number of top job bucket scores to be used in the overall_score calculation
get_records(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-record.html
Parameters
• job_id – None
• body – Record selection criteria
• desc – Set the sort direction
• end – End time filter for records
• exclude_interim – Exclude interim results
• from – skips a number of records
• record_score –
• size – specifies a max number of records to get
• sort – Sort records by a particular field
• start – Start time filter for records
info(**kwargs)
‘<>‘_
open_job(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-open-job.html
Parameters job_id – The ID of the job to open
post_calendar_events(**kwargs)
‘<>‘_
Parameters
• calendar_id – The ID of the calendar to modify
• body – A list of events
post_data(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-post-data.html
Parameters
• job_id – The name of the job receiving the data
• body – The data to process
• reset_end – Optional parameter to specify the end of the bucket resetting range
• reset_start – Optional parameter to specify the start of the bucket resetting range
preview_datafeed(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-preview-datafeed.html
Parameters datafeed_id – The ID of the datafeed to preview
put_calendar(**kwargs)
‘<>‘_
Parameters
• calendar_id – The ID of the calendar to create
• body – The calendar details
put_calendar_job(**kwargs)
‘<>‘_
Parameters
• calendar_id – The ID of the calendar to modify
• job_id – The ID of the job to add to the calendar
put_datafeed(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-put-datafeed.html
Parameters
• datafeed_id – The ID of the datafeed to create
• body – The datafeed config
put_filter(**kwargs)
‘<>‘_
Parameters
• filter_id – The ID of the filter to create
72 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
Security API can be used to help secure your Elasticsearch cluster. Integrating with LDAP and Active Directory.
class elasticsearch.client.xpack.security.SecurityClient(client)
authenticate(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-authenticate.html
change_password(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-change-password.html
Parameters
• body – the new password for the user
• username – The username of the user to change the password for
• refresh – If true (the default) then refresh the affected shards to make this operation
visible to search, if wait_for then wait for a refresh to make this operation visible to search,
if false then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
74 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
clear_cached_realms(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-cache.html
Parameters
• realms – Comma-separated list of realms to clear
• usernames – Comma-separated list of usernames to clear from the cache
clear_cached_roles(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-role-cache.html
Parameters name – Role name
create_api_key(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html
Parameters
• body – The api key request to create an API key
• refresh – If true (the default) then refresh the affected shards to make this operation
visible to search, if wait_for then wait for a refresh to make this operation visible to search,
if false then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
delete_privileges(**kwargs)
TODO
Parameters
• application – Application name
• name – Privilege name
• refresh – If true (the default) then refresh the affected shards to make this operation
visible to search, if wait_for then wait for a refresh to make this operation visible to search,
if false then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
delete_role(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-delete-role.html
Parameters
• name – Role name
• refresh – If true (the default) then refresh the affected shards to make this operation
visible to search, if wait_for then wait for a refresh to make this operation visible to search,
if false then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
delete_role_mapping(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-delete-role-mapping.html
Parameters
• name – Role-mapping name
• refresh – If true (the default) then refresh the affected shards to make this operation
visible to search, if wait_for then wait for a refresh to make this operation visible to search,
if false then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
delete_user(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-delete-user.html
Parameters
• username – username
• refresh – If true (the default) then refresh the affected shards to make this operation
visible to search, if wait_for then wait for a refresh to make this operation visible to search,
if false then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
disable_user(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-disable-user.html
Parameters
• username – The username of the user to disable
• refresh – If true (the default) then refresh the affected shards to make this operation
visible to search, if wait_for then wait for a refresh to make this operation visible to search,
if false then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
enable_user(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-enable-user.html
Parameters
• username – The username of the user to enable
• refresh – If true (the default) then refresh the affected shards to make this operation
visible to search, if wait_for then wait for a refresh to make this operation visible to search,
if false then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
get_api_key(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-api-key.html
Parameters
• id – API key id of the API key to be retrieved
• name – API key name of the API key to be retrieved
• realm_name – realm name of the user who created this API key to be retrieved
• username – user name of the user who created this API key to be retrieved
get_privileges(**kwargs)
TODO
Parameters
• application – Application name
• name – Privilege name
get_role(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-role.html
Parameters name – Role name
get_role_mapping(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-role-mapping.html
Parameters name – Role-Mapping name
get_token(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-token.html
Parameters body – The token request to get
get_user(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-user.html
Parameters username – A comma-separated list of usernames
76 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
get_user_privileges(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-user-privileges.html
has_privileges(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-has-privileges.html
Parameters
• body – The privileges to test
• user – Username
invalidate_api_key(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-invalidate-api-key.html
Parameters body – The api key request to invalidate API key(s)
invalidate_token(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-invalidate-token.html
Parameters body – The token to invalidate
put_privileges(**kwargs)
TODO
Parameters
• body – The privilege(s) to add
• refresh – If true (the default) then refresh the affected shards to make this operation
visible to search, if wait_for then wait for a refresh to make this operation visible to search,
if false then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
put_role(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-put-role.html
Parameters
• name – Role name
• body – The role to add
• refresh – If true (the default) then refresh the affected shards to make this operation
visible to search, if wait_for then wait for a refresh to make this operation visible to search,
if false then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
put_role_mapping(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-put-role-mapping.html
Parameters
• name – Role-mapping name
• body – The role to add
• refresh – If true (the default) then refresh the affected shards to make this operation
visible to search, if wait_for then wait for a refresh to make this operation visible to search,
if false then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
put_user(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-put-user.html
Parameters
• username – The username of the User
• body – The user to add
• refresh – If true (the default) then refresh the affected shards to make this operation
visible to search, if wait_for then wait for a refresh to make this operation visible to search,
if false then do nothing with refreshes., valid choices are: ‘true’, ‘false’, ‘wait_for’
Watcher API can be used to notify you when certain pre-defined thresholds have happened.
class elasticsearch.client.xpack.watcher.WatcherClient(client)
ack_watch(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-ack-watch.html
Parameters
• watch_id – Watch ID
• action_id – A comma-separated list of the action ids to be acked
activate_watch(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-activate-watch.html
Parameters watch_id – Watch ID
deactivate_watch(**kwargs)
https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-deactivate-watch.html
Parameters watch_id – Watch ID
delete_watch(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-delete-watch.html
Parameters id – Watch ID
execute_watch(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-execute-watch.html
Parameters
• id – Watch ID
• body – Execution control
• debug – indicates whether the watch should execute in debug mode
get_watch(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-get-watch.html
Parameters id – Watch ID
put_watch(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-put-watch.html
Parameters
• id – Watch ID
• body – The watch
• active – Specify whether the watch is in/active by default
• if_primary_term – only update the watch if the last operation that has changed the
watch has the specified primary term
78 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
• if_seq_no – only update the watch if the last operation that has changed the watch has
the specified sequence number
• version – Explicit version number for concurrency control
start(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-start.html
stats(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-stats.html
Parameters
• metric – Controls what additional stat metrics should be include in the response
• emit_stacktraces – Emits stack traces of currently running watches
stop(**kwargs)
http://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-stop.html
Migration API helps simplify upgrading X-Pack indices from one version to another.
class elasticsearch.client.xpack.migration.MigrationClient(client)
deprecations(**kwargs)
http://www.elastic.co/guide/en/migration/current/migration-api-deprecation.html
Parameters index – Index pattern
7.3 Exceptions
class elasticsearch.ImproperlyConfigured
Exception raised when the config passed to the client is inconsistent or invalid.
class elasticsearch.ElasticsearchException
Base class for all exceptions raised by this package’s operations (doesn’t apply to
ImproperlyConfigured).
class elasticsearch.SerializationError(ElasticsearchException)
Data passed in failed to serialize properly in the Serializer being used.
class elasticsearch.TransportError(ElasticsearchException)
Exception raised when ES returns a non-OK (>=400) HTTP status code. Or when an actual connection error
happens; in that case the status_code will be set to 'N/A'.
error
A string error message.
info
Dict of returned error info from ES, where available, underlying exception when not.
status_code
The HTTP status code of the response that precipitated the error or 'N/A' if not applicable.
class elasticsearch.ConnectionError(TransportError)
Error raised when there was an exception while talking to ES. Original exception from the underlying
Connection implementation is available as .info.
7.3. Exceptions 79
Elasticsearch Documentation, Release 7.0.0
class elasticsearch.ConnectionTimeout(ConnectionError)
A network timeout. Doesn’t cause a node retry by default.
class elasticsearch.SSLError(ConnectionError)
Error raised when encountering SSL errors.
class elasticsearch.NotFoundError(TransportError)
Exception representing a 404 status code.
class elasticsearch.ConflictError(TransportError)
Exception representing a 409 status code.
class elasticsearch.RequestError(TransportError)
Exception representing a 400 status code.
All of the classes responsible for handling the connection to the Elasticsearch cluster. The default subclasses used can
be overriden by passing parameters to the Elasticsearch class. All of the arguments to the client will be passed
on to Transport, ConnectionPool and Connection.
For example if you wanted to use your own implementation of the ConnectionSelector class you can just pass
in the selector_class parameter.
Note: ConnectionPool and related options (like selector_class) will only be used if more than one con-
nection is defined. Either directly or via the Sniffing mechanism.
7.4.1 Transport
80 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
82 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
It tries to resurrect eligible connections, forces a resurrection when no connections are availible and passes
the list of live connections to the selector instance to choose from.
Returns a connection instance and it’s current fail count.
mark_dead(connection, now=None)
Mark the connection as dead (failed). Remove it from the live pool and put it on a timeout.
Parameters connection – the failed instance
mark_live(connection)
Mark connection as healthy after a resurrection. Resets the fail counter for the connection.
Parameters connection – the connection to redeem
resurrect(force=False)
Attempt to resurrect a connection from the dead pool. It will try to locate one (not all) eligible (it’s timeout
is over) connection to return to the live pool. Any resurrected connection is also returned.
Parameters force – resurrect a connection even if there is none eligible (used when we have
no live connections). If force is specified resurrect always returns a connection.
class elasticsearch.ConnectionSelector(opts)
Simple class used to select a connection from a list of currently live connection instances. In init time it is passed
a dictionary containing all the connections’ options which it can then use during the selection process. When
the select method is called it is given a list of currently live connections to choose from.
The options dictionary is the one that has been passed to Transport as hosts param and the same that is used
to construct the Connection object itself. When the Connection was created from information retrieved from the
cluster via the sniffing process it will be the dictionary returned by the host_info_callback.
Example of where this would be useful is a zone-aware selector that would only select connections from it’s
own zones and only fall back to other connections where there would be none in it’s zones.
Parameters opts – dictionary of connection instances and their options
select(connections)
Select a connection from the given list.
Parameters connections – list of live connections to choose from
If you have complex SSL logic for connecting to Elasticsearch using an SSLContext object might be more helpful.
You can create one natively using the python SSL library with the create_default_context (https://docs.python.org/3/
library/ssl.html#ssl.create_default_context) method.
To create an SSLContext object you only need to use one of cafile, capath or cadata:
Please note that the use of SSLContext is only available for Urllib3.
class elasticsearch.Urllib3HttpConnection(host=’localhost’, port=9200, http_auth=None,
use_ssl=False, verify_certs=None,
ssl_show_warn=True, ca_certs=None,
client_cert=None, client_key=None,
ssl_version=None, ssl_assert_hostname=None,
ssl_assert_fingerprint=None, max-
size=10, headers=None, ssl_context=None,
http_compress=False, **kwargs)
Default connection class using the urllib3 library and the http protocol.
Parameters
• host – hostname of the node (default: localhost)
• port – port to use (integer, default: 9200)
• url_prefix – optional url prefix for elasticsearch
• timeout – default timeout in seconds (float, default: 10)
• http_auth – optional http auth information as either ‘:’ separated string or a tuple
• use_ssl – use ssl for the connection if True
• verify_certs – whether to verify SSL certificates
• ssl_show_warn – show warning when verify certs is disabled
• ca_certs – optional path to CA bundle. See https://urllib3.readthedocs.io/en/latest/
security.html#using-certifi-with-urllib3 for instructions how to get default set
• client_cert – path to the file containing the private key and the certificate, or cert only
if using client_key
• client_key – path to the file containing the private key if using separate cert and key
files (client_cert will contain only the cert)
• ssl_version – version of the SSL protocol to use. Choices are: SSLv23 (default) SSLv2
SSLv3 TLSv1 (see PROTOCOL_* constants in the ssl module for exact options for your
environment).
• ssl_assert_hostname – use hostname verification if not False
• ssl_assert_fingerprint – verify the supplied certificate fingerprint if not None
• maxsize – the number of connections which will be kept open to this host. See https:
//urllib3.readthedocs.io/en/1.4/pools.html#api for more information.
• headers – any custom http headers to be add to requests
• http_compress – Use gzip compression
close()
Explicitly closes connection
List of transport classes that can be used, simply import your choice and pass it to the constructor of Elasticsearch
as connection_class. Note that the RequestsHttpConnection requires requests to be installed.
For example to use the requests-based connection just import it and use it:
84 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
The default connection class is based on urllib3 which is more performant and lightweight than the optional
requests-based class. Only use RequestsHttpConnection if you have need of any of requests advanced
features like custom auth plugins etc.
7.5.1 Connection
7.5.2 Urllib3HttpConnection
class elasticsearch.connection.Urllib3HttpConnection(host=’localhost’,
port=9200, http_auth=None,
use_ssl=False, ver-
ify_certs=None,
ssl_show_warn=True,
ca_certs=None,
client_cert=None,
client_key=None,
ssl_version=None,
ssl_assert_hostname=None,
ssl_assert_fingerprint=None,
maxsize=10, head-
ers=None, ssl_context=None,
http_compress=False,
**kwargs)
Default connection class using the urllib3 library and the http protocol.
Parameters
• host – hostname of the node (default: localhost)
• port – port to use (integer, default: 9200)
• url_prefix – optional url prefix for elasticsearch
• timeout – default timeout in seconds (float, default: 10)
• http_auth – optional http auth information as either ‘:’ separated string or a tuple
• use_ssl – use ssl for the connection if True
7.5.3 RequestsHttpConnection
86 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
7.6 Helpers
Collection of simple helper functions that abstract some specifics or the raw API.
There are several helpers for the bulk API since its requirement for specific formatting and other considerations can
make it cumbersome if used directly.
All bulk helpers accept an instance of Elasticsearch class and an iterable actions (any iterable, can also be a
generator, which is ideal in most cases since it will allow you to index large datasets without the need of loading them
into memory).
The items in the action iterable should be the documents we wish to index in several formats. The most common
one is the same as returned by search(), for example:
{
'_index': 'index-name',
'_type': 'document',
'_id': 42,
'_routing': 5,
'pipeline': 'my-ingest-pipeline',
'_source': {
"title": "Hello World!",
"body": "..."
}
}
Alternatively, if _source is not present, it will pop all metadata fields from the doc and use the rest as the document
data:
{
"_id": 42,
"_routing": 5,
"title": "Hello World!",
"body": "..."
}
The bulk() api accepts index, create, delete, and update actions. Use the _op_type field to specify an
action (_op_type defaults to index):
{
'_op_type': 'delete',
'_index': 'index-name',
'_type': 'document',
'_id': 42,
}
{
'_op_type': 'update',
'_index': 'index-name',
'_type': 'document',
'_id': 42,
'doc': {'question': 'The life, universe and everything.'}
}
7.6. Helpers 87
Elasticsearch Documentation, Release 7.0.0
Example:
Lets say we have an iterable of data. Lets say a list of words called mywords and we want to index those words into
individual documents where the structure of the document is like {"word": "<myword>"}.
def gendata():
mywords = ['foo', 'bar', 'baz']
for word in mywords:
yield {
"_index": "mywords",
"_type": "document",
"doc": {"word": word},
}
bulk(es, gendata())
For a more complete and complex example please take a look at https://github.com/elastic/elasticsearch-py/blob/
master/example/load.py#L76-L130
Note: When reading raw json strings from a file, you can also pass them in directly (without decoding to dicts first).
In that case, however, you lose the ability to specify anything (index, type, even id) on a per-record basis, all documents
will just be sent to elasticsearch to be indexed as-is.
88 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
• initial_backoff – number of seconds we should wait before the first retry. Any sub-
sequent retries will be powers of initial_backoff * 2**retry_number
• max_backoff – maximum number of seconds a retry will wait
• yield_ok – if set to False will skip successful documents in the output
elasticsearch.helpers.parallel_bulk(client, actions, thread_count=4, chunk_size=500,
max_chunk_bytes=104857600, queue_size=4, ex-
pand_action_callback=<function expand_action>,
*args, **kwargs)
Parallel version of the bulk helper run in multiple threads at once.
Parameters
• client – instance of Elasticsearch to use
• actions – iterator containing the actions
• thread_count – size of the threadpool to use for the bulk requests
• chunk_size – number of docs in one chunk sent to es (default: 500)
• max_chunk_bytes – the maximum size of the request in bytes (default: 100MB)
• raise_on_error – raise BulkIndexError containing errors (as .errors) from the
execution of the last chunk when some occur. By default we raise.
• raise_on_exception – if False then don’t propagate exceptions from call to bulk
and just report the items that failed as failed.
• expand_action_callback – callback executed on each action passed in, should return
a tuple containing the action line and the data line (None if data line should be omitted).
• queue_size – size of the task queue between the main thread (producing chunks to send)
and the processing threads.
elasticsearch.helpers.bulk(client, actions, stats_only=False, *args, **kwargs)
Helper for the bulk() api that provides a more human friendly interface - it consumes an iterator of actions
and sends them to elasticsearch in chunks. It returns a tuple with summary information - number of successfully
executed actions and either list of errors or number of errors if stats_only is set to True. Note that by
default we raise a BulkIndexError when we encounter an error so options like stats_only only apply
when raise_on_error is set to False.
When errors are being collected original document data is included in the error dictionary which can lead to an
extra high memory usage. If you need to process a lot of data and want to ignore/collect errors please consider
using the streaming_bulk() helper which will just return the errors and not store them in memory.
Parameters
• client – instance of Elasticsearch to use
• actions – iterator containing the actions
• stats_only – if True only report number of successful/failed operations instead of just
number of successful and a list of error responses
Any additional keyword arguments will be passed to streaming_bulk() which is used to execute the oper-
ation, see streaming_bulk() for more accepted parameters.
7.6. Helpers 89
Elasticsearch Documentation, Release 7.0.0
7.6.2 Scan
scan(es,
query={"query": {"match": {"title": "python"}}},
index="orders-*",
doc_type="books"
)
7.6.3 Reindex
90 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
Parameters
• client – instance of Elasticsearch to use (for read if target_client is specified as
well)
• source_index – index (or list of indices) to read documents from
• target_index – name of the index in the target cluster to populate
• query – body for the search() api
• target_client – optional, is specified will be used for writing (thus enabling reindex
between clusters)
• chunk_size – number of docs in one chunk sent to es (default: 500)
• scroll – Specify how long a consistent view of the index should be maintained for scrolled
search
• scan_kwargs – additional kwargs to be passed to scan()
• bulk_kwargs – additional kwargs to be passed to bulk()
7.7 Changelog
7.7. Changelog 91
Elasticsearch Documentation, Release 7.0.0
• Updates to SSLContext logic to make it easier to use and have saner defaults.
• Doc updates
• bad release
• bulk helpers now extract pipeline parameter from the action dictionary.
The client now automatically sends Content-Type http header set to application/json. If you are explicitly
passing in other encoding than json you need to set the header manually.
• Fixed sniffing
92 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
• ping now ignores all TransportError exceptions and just returns False
• expose scroll_id on ScanError
• increase default size for scan helper to 1000
Internal:
• changed Transport.perform_request to just return the body, not status as well.
Due to change in json encoding the client will no longer mask issues with encoding - if you work with non-ascii data
in python 2 you must use the unicode type or have proper encoding set in your environment.
• adding additional options for ssh - ssl_assert_hostname and ssl_assert_fingerprint to the
default connection class
• fix sniffing
• move multiprocessing import inside parallel bulk for Google App Engine
7.7. Changelog 93
Elasticsearch Documentation, Release 7.0.0
• removed thrift and memcached connections, if you wish to continue using those, extract the classes and use
them separately.
• added a new, parallel version of the bulk helper using thread pools
• In helpers, removed bulk_index as an alias for bulk. Use bulk instead.
• Timeout now doesn’t trigger a retry by default (can be overriden by setting retry_on_timeout=True)
• Introduced new parameter retry_on_status (defaulting to (503, 504, )) controls which http status
code should lead to a retry.
• Implemented url parsing according to RFC-1738
• Added support for proper SSL certificate handling
94 Chapter 7. Contents
Elasticsearch Documentation, Release 7.0.0
Elasticsearch 1.0 compatibility. See 0.4.X releases (and 0.4 branch) for code compatible with 0.90 elasticsearch.
• major breaking change - compatible with 1.0 elasticsearch releases only!
• Add an option to change the timeout used for sniff requests (sniff_timeout).
• empty responses from the server are now returned as empty strings instead of None
• get_alias now has name as another optional parameter due to issue #4539 in es repo. Note that the order of
params have changed so if you are not using keyword arguments this is a breaking change.
7.7. Changelog 95
Elasticsearch Documentation, Release 7.0.0
• changed perform_request on Connection classes to return headers as well. This is a backwards incom-
patible change for people who have developed their own connection class.
• changed deserialization mechanics. Users who provided their own serializer that didn’t extend
JSONSerializer need to specify a mimetype class attribute.
• minor bug fixes
Initial release.
96 Chapter 7. Contents
CHAPTER 8
License
97
Elasticsearch Documentation, Release 7.0.0
98 Chapter 8. License
CHAPTER 9
• genindex
• modindex
• search
99
Elasticsearch Documentation, Release 7.0.0
e
elasticsearch, 79
elasticsearch.client, 37
elasticsearch.client.xpack, 64
elasticsearch.client.xpack.graph, 65
elasticsearch.client.xpack.license, 65
elasticsearch.client.xpack.migration,
79
elasticsearch.client.xpack.ml, 66
elasticsearch.client.xpack.security, 74
elasticsearch.client.xpack.watcher, 78
elasticsearch.connection, 85
elasticsearch.helpers, 88
101
Elasticsearch Documentation, Release 7.0.0
A clear_scroll() (elasticsearch.Elasticsearch
ack_watch() (elastic- method), 21
search.client.xpack.watcher.WatcherClient close() (elasticsearch.client.IndicesClient method), 37
method), 78 close() (elasticsearch.ConnectionPool method), 82
activate_watch() (elastic- close() (elasticsearch.Transport method), 81
search.client.xpack.watcher.WatcherClient close() (elasticsearch.Urllib3HttpConnection
method), 78 method), 84
add_connection() (elasticsearch.Transport close_job() (elasticsearch.client.xpack.ml.MlClient
method), 81 method), 66
aliases() (elasticsearch.client.CatClient method), 54 ClusterClient (class in elasticsearch.client), 50
allocation() (elasticsearch.client.CatClient ConflictError (class in elasticsearch), 80
method), 55 Connection (class in elasticsearch.connection), 85
allocation_explain() (elastic- ConnectionError (class in elasticsearch), 79
search.client.ClusterClient method), 50 ConnectionPool (class in elasticsearch), 82
analyze() (elasticsearch.client.IndicesClient method), ConnectionSelector (class in elasticsearch), 83
37 ConnectionTimeout (class in elasticsearch), 79
authenticate() (elastic- count() (elasticsearch.client.CatClient method), 55
search.client.xpack.security.SecurityClient count() (elasticsearch.Elasticsearch method), 21
method), 74 create() (elasticsearch.client.IndicesClient method),
38
B create() (elasticsearch.client.SnapshotClient
bulk() (elasticsearch.Elasticsearch method), 20 method), 61
bulk() (in module elasticsearch.helpers), 89 create() (elasticsearch.Elasticsearch method), 21
create_api_key() (elastic-
C search.client.xpack.security.SecurityClient
method), 75
cancel() (elasticsearch.client.TasksClient method), 63 create_repository() (elastic-
CatClient (class in elasticsearch.client), 54 search.client.SnapshotClient method), 61
change_password() (elastic-
search.client.xpack.security.SecurityClient D
method), 74
deactivate_watch() (elastic-
clear_cache() (elasticsearch.client.IndicesClient
search.client.xpack.watcher.WatcherClient
method), 37
method), 78
clear_cached_realms() (elastic-
delete() (elasticsearch.client.IndicesClient method),
search.client.xpack.security.SecurityClient
38
method), 74
delete() (elasticsearch.client.SnapshotClient
clear_cached_roles() (elastic-
method), 61
search.client.xpack.security.SecurityClient
delete() (elasticsearch.client.xpack.license.LicenseClient
method), 75
method), 65
delete() (elasticsearch.Elasticsearch method), 22
103
Elasticsearch Documentation, Release 7.0.0
104 Index
Elasticsearch Documentation, Release 7.0.0
find_file_structure() (elastic- 70
search.client.xpack.ml.MlClient method), get_influencers() (elastic-
67 search.client.xpack.ml.MlClient method),
flush() (elasticsearch.client.IndicesClient method), 40 70
flush_job() (elasticsearch.client.xpack.ml.MlClient get_job_stats() (elastic-
method), 68 search.client.xpack.ml.MlClient method),
flush_synced() (elasticsearch.client.IndicesClient 70
method), 40 get_jobs() (elasticsearch.client.xpack.ml.MlClient
forcemerge() (elasticsearch.client.IndicesClient method), 70
method), 41 get_mapping() (elasticsearch.client.IndicesClient
forecast() (elasticsearch.client.xpack.ml.MlClient method), 43
method), 68 get_model_snapshots() (elastic-
search.client.xpack.ml.MlClient method),
G 70
get() (elasticsearch.client.IndicesClient method), 41 get_overall_buckets() (elastic-
get() (elasticsearch.client.SnapshotClient method), 62 search.client.xpack.ml.MlClient method),
get() (elasticsearch.client.TasksClient method), 63 71
get() (elasticsearch.client.xpack.license.LicenseClient get_pipeline() (elasticsearch.client.IngestClient
method), 65 method), 50
get() (elasticsearch.Elasticsearch method), 26 get_privileges() (elastic-
get_alias() (elasticsearch.client.IndicesClient search.client.xpack.security.SecurityClient
method), 42 method), 76
get_api_key() (elastic- get_records() (elastic-
search.client.xpack.security.SecurityClient search.client.xpack.ml.MlClient method),
method), 76 71
get_basic_status() (elastic- get_repository() (elastic-
search.client.xpack.license.LicenseClient search.client.SnapshotClient method), 62
method), 65 get_role() (elastic-
get_buckets() (elastic- search.client.xpack.security.SecurityClient
search.client.xpack.ml.MlClient method), method), 76
68 get_role_mapping() (elastic-
get_calendar_events() (elastic- search.client.xpack.security.SecurityClient
search.client.xpack.ml.MlClient method), method), 76
69 get_script() (elasticsearch.Elasticsearch method),
get_calendars() (elastic- 26
search.client.xpack.ml.MlClient method), get_settings() (elasticsearch.client.ClusterClient
69 method), 51
get_categories() (elastic- get_settings() (elasticsearch.client.IndicesClient
search.client.xpack.ml.MlClient method), method), 43
69 get_source() (elasticsearch.Elasticsearch method),
get_connection() (elasticsearch.ConnectionPool 27
method), 82 get_template() (elasticsearch.client.IndicesClient
get_connection() (elasticsearch.Transport method), 43
method), 81 get_token() (elastic-
get_datafeed_stats() (elastic- search.client.xpack.security.SecurityClient
search.client.xpack.ml.MlClient method), method), 76
69 get_trial_status() (elastic-
get_datafeeds() (elastic- search.client.xpack.license.LicenseClient
search.client.xpack.ml.MlClient method), method), 65
69 get_upgrade() (elasticsearch.client.IndicesClient
get_field_mapping() (elastic- method), 44
search.client.IndicesClient method), 42 get_user() (elastic-
get_filters() (elastic- search.client.xpack.security.SecurityClient
search.client.xpack.ml.MlClient method), method), 76
Index 105
Elasticsearch Documentation, Release 7.0.0
H N
has_privileges() (elastic- nodeattrs() (elasticsearch.client.CatClient method),
search.client.xpack.security.SecurityClient 57
method), 77 nodes() (elasticsearch.client.CatClient method), 57
health() (elasticsearch.client.CatClient method), 56 NodesClient (class in elasticsearch.client), 53
health() (elasticsearch.client.ClusterClient method), NotFoundError (class in elasticsearch), 80
51
help() (elasticsearch.client.CatClient method), 56 O
hot_threads() (elasticsearch.client.NodesClient open() (elasticsearch.client.IndicesClient method), 44
method), 53 open_job() (elasticsearch.client.xpack.ml.MlClient
method), 72
I
ImproperlyConfigured (class in elasticsearch), 79 P
index() (elasticsearch.Elasticsearch method), 27 parallel_bulk() (in module elasticsearch.helpers),
indices() (elasticsearch.client.CatClient method), 56 89
IndicesClient (class in elasticsearch.client), 37 pending_tasks() (elasticsearch.client.CatClient
info (elasticsearch.TransportError attribute), 79 method), 58
info() (elasticsearch.client.NodesClient method), 53 pending_tasks() (elasticsearch.client.ClusterClient
info() (elasticsearch.client.xpack.ml.MlClient method), 51
method), 71 perform_request() (elasticsearch.Transport
info() (elasticsearch.client.xpack.XPackClient method), 81
method), 64 ping() (elasticsearch.Elasticsearch method), 30
info() (elasticsearch.Elasticsearch method), 28 plugins() (elasticsearch.client.CatClient method), 58
IngestClient (class in elasticsearch.client), 50 post() (elasticsearch.client.xpack.license.LicenseClient
invalidate_api_key() (elastic- method), 65
search.client.xpack.security.SecurityClient post_calendar_events() (elastic-
method), 77 search.client.xpack.ml.MlClient method),
invalidate_token() (elastic- 72
search.client.xpack.security.SecurityClient post_data() (elasticsearch.client.xpack.ml.MlClient
method), 77 method), 72
post_start_basic() (elastic-
L search.client.xpack.license.LicenseClient
LicenseClient (class in elastic- method), 65
search.client.xpack.license), 65 post_start_trial() (elastic-
list() (elasticsearch.client.TasksClient method), 64 search.client.xpack.license.LicenseClient
method), 65
M preview_datafeed() (elastic-
mark_dead() (elasticsearch.ConnectionPool method), search.client.xpack.ml.MlClient method),
83 72
mark_dead() (elasticsearch.Transport method), 81 put_alias() (elasticsearch.client.IndicesClient
mark_live() (elasticsearch.ConnectionPool method), method), 44
83 put_calendar() (elastic-
master() (elasticsearch.client.CatClient method), 57 search.client.xpack.ml.MlClient method),
mget() (elasticsearch.Elasticsearch method), 28 72
106 Index
Elasticsearch Documentation, Release 7.0.0
Index 107
Elasticsearch Documentation, Release 7.0.0
U
update() (elasticsearch.Elasticsearch method), 34
update_aliases() (elastic-
search.client.IndicesClient method), 48
update_by_query() (elasticsearch.Elasticsearch
method), 35
update_datafeed() (elastic-
search.client.xpack.ml.MlClient method),
73
update_filter() (elastic-
search.client.xpack.ml.MlClient method),
74
update_job() (elastic-
search.client.xpack.ml.MlClient method),
74
update_model_snapshot() (elastic-
search.client.xpack.ml.MlClient method),
74
108 Index