AI Hints
Home›Elasticsearch Tutorial›Elasticsearch from Python
Elasticsearch · Lesson 88 of 120

The Python Client

Search from code.

⏱ 2 min read▶ Video at 1:22:42🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Kibana is great for exploring. But your app talks to Elasticsearch from code. The official Python client turns every request you've learned into a Python method call.

  • Kibana to explore
  • Apps use code
  • Python client

Install it

Install the client with pip. Use the same major version as your cluster, here version nine.

Terminalbash
pip install "elasticsearch>=9,<10"

Connect and say hello

We create a client with the cluster's address. Info returns details about the cluster. Then we index a document, and get it back. Run it. The version, and our document, from Python.

hello.pypython
from elasticsearch import Elasticsearch

es = Elasticsearch("http://localhost:9200")

info = es.info()
print("Version:", info["version"]["number"])

es.index(index="products", id=1, document={"name": "Trail Mug"})
doc = es.get(index="products", id=1)
print(doc["_source"])
Output
Version: 9.5.5
{'name': 'Trail Mug'}

Console to Python

Every console request maps to a method. Get becomes es dot get. Search becomes es dot search. The index name and ID become arguments, and the body becomes keyword arguments.

keyvalue
GET products/_doc/1es.get(index=, id=)
PUT products/_doc/1es.index(...)
GET _searches.search(...)
Bodykeyword arguments

Summary

The Python client connects to your cluster, and every console request becomes a method call. Next, indexing data from Python.

Key takeaways
  • pip install
  • Elasticsearch()
  • es.index / es.get
Next up: Indexing from Python — create, update, delete