Overview of the Elasticsearch Python client

Elasticsearch provides two types of clients for Python. The elasticsearch-py package provides an official low-level client. The elasticsearch-dsl-py package provides a high-level library that is built on top of the low-level client. The high-level library provides a more convenient way for you to manipulate queries since it is close to the Elasticsearch JSON DSL. Both clients support versions from 2.x to 7.x. Assuming that you have a working environment for Python 3.6, use the following step-by-step instructions:

  1. To install the Python Elasticsearch client, you can use the pip command, as shown in the following code block:
pip install elasticsearch==7.0.0
  1. To install the Python Elasticsearch DSL high-level library, you can refer to the following code block:
pip install elasticsearch-dsl==7.0.0
  1. If you are working on a project, the recommended way is to set the version range in the setup.py file or in requirements.txt, as shown in the following code block:
# Elasticsearch 7.x
elasticsearch-dsl>=7.0.0,<8.0.0
  1. After you install the Elasticsearch client and the Elasticsearch server is running, we can use a Python script to test the connection. Use the commands shown in the following code block. We are using Python version 3.6 in our working environment:
$ python3.6
Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 03:02:14)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from elasticsearch import Elasticsearch
>>> es = Elasticsearch()
>>> es.info()
{'name': 'WTW.local', 'cluster_name': 'elasticsearch', 'cluster_uuid': 'R9PuCbcCREK0XtPImODaVQ', 'version': {'number': '7.0.0-beta1', 'build_flavor': 'default', 'build_type': 'tar', 'build_hash': '15bb494', 'build_date': '2019-02-13T12:30:14.432234Z', 'build_snapshot': False, 'lucene_version': '8.0.0', 'minimum_wire_compatibility_version': '6.7.0', 'minimum_index_compatibility_version': '6.0.0-beta1'}, 'tagline': 'You Know, for Search'}
>>> exit()

As you can see, the es.info() command has retrieved the information of the running Elasticsearch instance. In the following section, we will introduce the low-level client first.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.73.146