SoFunction
Updated on 2025-03-02

Elasticsearch py client library installation and usage analysis

1. Introduction

elasticsearch-py is an official low-level elasticsearch python client library. Why is it said to be a low-level client library? Because it only provides a simple encapsulation of elasticsearch's rest API interface, it provides maximum flexibility, but it is not very convenient to use at the same time. Compared with this low-level client library, the official also provides a high-level python client library: elasticsearch-dsl, which will be introduced in another article.

For more information, please refer to the official documentation:/en/master/

2. Installation

Different elasticsearch versions require different client versions, so when installing, you need to decide based on your elasticsearch. Here is a simple reference:

# Elasticsearch
elasticsearch>=6.0.0,<7.0.0
# Elasticsearch
elasticsearch>=5.0.0,<6.0.0
# Elasticsearch
elasticsearch>=2.0.0,<3.0.0

Try to select the latest version under compatible large version numbers.

pip install elasticsearch

3. API

3.1 API Documentation

All APIs map the original rest API as tightly as possible.

3.1.1 Global Options

Some parameters added by the client can be used on all APIs.

Some http error status codes are ignored by users.

from elasticsearch import Elasticsearch
es = Elasticsearch()

# ignore 400 cause by IndexAlreadyExistsException when creating an index
(index='test-index', ignore=400)

# ignore 404 and 400
(index='test-index', ignore=[400, 404])

Used to set the timeout time.

# only wait for 1 second, regardless of the client's default
(wait_for_status='yellow', request_timeout=1)

3.filter_path

Used to filter the return value.

(index='test-index', filter_path=['._id', '._type'])

3.1.2 Elasticsearch

Elasticsearch is a low-level client that provides a direct mapping from python to es rest endpoints. This instance has attributes cat, cluster, indices, ingest, nodes, snapshot and tasks, through which they can access instances of CatClient, ClusterClient, IndicesClient, IngestClient, NodesClient, SnapshotClient, and TasksClient.

The elasticsearch class contains many common methods for operating elasticsearch, such as: get, mget, search, index, bulk, create, delete, etc. The specific usage of these methods can be referred to the official documentation of elasticsearch-py.

Before executing the above method, you first need to obtain an instance of elasticsearch. There are two ways to obtain this instance. One is to pass a connection class instance to the initialization function of elasticsearch, and the other is to pass the host and port of the node to be connected to the initialization function of elasticsearch. In fact, these hosts and ports are still passed to the connection class in the end.

# create connection to localhost using the ThriftConnection
es = Elasticsearch(connection_class=ThriftConnection)

# 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},
])

3.1.3 Indices

indices are used to operate and query information about indexes, or can be said to operate and query index-related metadata.

3.1.4 Ingest

ingest is a plug-in for enriching insertion of inserts.

3.1.5 Cluster

cluster is used to obtain information related to the cluster, such as: cluster health status, settings, etc.

3.1.6 Nodes

nodes are used to obtain information related to nodes.

3.1.7 Cat

cat can be used to obtain alias, shard information, document quantity and other information.

3.1.8 Snapshot

snapshot is used to manage snapshots.

3.1.9 Tasks

Tasks are used for task management. The official documentation prompts that the task is a new feature and may change in the future, so be careful.

3.2 X-Pack APIs

X-Pack is an Elastic Stack extension that bundles security, alerts, monitoring, reporting, and graphics capabilities into an easy-to-install package.

  • 3.2.1 Info
  • 3.2.2 Graph Explore
  • 3.3.3 Licensing API
  • 3.3.4 Machine Learning
  • 3.3.5 Security APIS
  • 3.3.6 Watcher APIS
  • 3.3.7 Migration APIS

3.3 Exception

This section shows the exceptions that may be thrown when using elasticsearch-py.

3.4 Connection Layer API

Connection is the class responsible for connecting to the cluster.

3.4.1 Transport

transport encapsulates logic-related transmissions. Processes instantiation of individual connections and creates a connection pool to save them.

3.4.2 Connection Pool

The connection pool is a connection pool that manages connections.

3.4.3 Connection Selector

Connection selector is a connection selector. One of its best examples is zone-aware selection. You can automatically select local connections. Only when the local node cannot be connected is selected, you will choose to connect to other nodes.

3.4.4 Urllib3HttpConnection

Default connection class.

3.5 Transmission Class

The transport module lists the connection class that can be used as the elasticsearch initialization parameter connection_class.

3.5.1 Connection

The connection is responsible for managing connections to elasticsearch nodes.

3.5.2 Urllib3HttpConnection

The connection class based on urllib is the default connection class.

3.5.3 RequestsHttpConnection

The connection class based on requests is not recommended unless you want to use the advanced features related to requests.

3.6 helpers

helpers is a collection of simple helper functions that abstract some details or the original API.

3.6.1 bulk helpers

The specific format requirements of the bulk API make it very complicated to use them directly. Therefore, several helper functions of the bulk API are provided here. For specific usage methods, please refer to the official documentation of elasticsearch-py.

3.6.2 scan

scan is a simple abstraction of scroll API.

3.6.3 reindex

reindex is used to reindex all documents in one index that may satisfy a given query to another index

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.