- Elastic Cloud Serverless
- Elasticsearch
- Elastic Observability
- Get started
- Observability overview
- Elastic Observability Serverless billing dimensions
- Create an Observability project
- Quickstart: Monitor hosts with Elastic Agent
- Quickstart: Monitor your Kubernetes cluster with Elastic Agent
- Quickstart: Monitor hosts with OpenTelemetry
- Quickstart: Unified Kubernetes Observability with Elastic Distributions of OpenTelemetry (EDOT)
- Quickstart: Collect data with AWS Firehose
- Get started with dashboards
- Applications and services
- Application performance monitoring (APM)
- Get started with traces and APM
- Learn about data types
- Collect application data
- View and analyze data
- Act on data
- Use APM securely
- Reduce storage
- Managed intake service event API
- Troubleshooting
- Synthetic monitoring
- Get started
- Scripting browser monitors
- Configure lightweight monitors
- Manage monitors
- Work with params and secrets
- Analyze monitor data
- Monitor resources on private networks
- Use the CLI
- Configure a Synthetics project
- Multifactor Authentication for browser monitors
- Configure Synthetics settings
- Grant users access to secured resources
- Manage data retention
- Scale and architect a deployment
- Synthetics Encryption and Security
- Troubleshooting
- Application performance monitoring (APM)
- Infrastructure and hosts
- Logs
- Inventory
- Incident management
- Data set quality
- Observability AI Assistant
- Machine learning
- Reference
- Get started
- Elastic Security
- Elastic Security overview
- Security billing dimensions
- Create a Security project
- Elastic Security requirements
- Elastic Security UI
- AI for Security
- Ingest data
- Configure endpoint protection with Elastic Defend
- Manage Elastic Defend
- Endpoints
- Policies
- Trusted applications
- Event filters
- Host isolation exceptions
- Blocklist
- Optimize Elastic Defend
- Event capture and Elastic Defend
- Endpoint protection rules
- Identify antivirus software on your hosts
- Allowlist Elastic Endpoint in third-party antivirus apps
- Elastic Endpoint self-protection features
- Elastic Endpoint command reference
- Endpoint response actions
- Cloud Security
- Explore your data
- Dashboards
- Detection engine overview
- Rules
- Alerts
- Advanced Entity Analytics
- Investigation tools
- Asset management
- Manage settings
- Troubleshooting
- Manage your project
- Changelog
Get started with the Python client
editGet started with the Python client
editThis page guides you through the installation process of the Python client, shows you how to initialize the client, and how to perform basic Elasticsearch operations with it.
See the Python client documentation for more detailed usage instructions.
The same client is used for Elasticsearch Serverless, on-premise and managed Elasticsearch. Some API endpoints are however not available in Elasticsearch Serverless.
Requirements
edit- Python 3.9 or higher
-
pip
Documentation
editFind the full documentation for the Python client on readthedocs.
Installation
editUsing the command line
editYou can install the Python client with the following commands:
python -m pip install elasticsearch
Initialize the client
editInitialize the client using your API key and Elasticsearch endpoint:
from elasticsearch import Elasticsearch client = Elasticsearch( "https://...", # Your project's Elasticsearch endpoint api_key='api-key', # API key for your project )
To get API keys for the Elasticsearch endpoint for a project, see Get started.
Using the API
editAfter you’ve initialized the client, you can start ingesting documents. You can use
the bulk
API for this. This API enables you to index, update, and delete several
documents in one request.
Creating an index and ingesting documents
editYou can call the bulk
API with a body parameter, an array of hashes that
define the action, and a document.
The following is an example of indexing some classic books into the books
index:
from datetime import datetime client.bulk( body=[ {"index": {"_index": "books", "_id": "1"}}, {"title": "Infinite Jest", "author": "David Foster Wallace", "published_on": datetime(1996, 2, 1)}, {"index": {"_index": "books", "_id": "2"}}, {"title": "Ulysses", "author": "James Joyce", "published_on": datetime(1922, 2, 2)}, {"index": {"_index": "books", "_id": "3"}}, {"title": "Just Kids", "author": "Patti Smith", "published_on": datetime(2010, 1, 19)}, ], )
Getting documents
editYou can get documents by using the following code:
response = client.get(index="books", id="1") print(response.body)
Searching
editNow that some data is available, you can search your documents using the
search
API:
response = client.search(index="books", query={ "match": { "title": "infinite" } }) for hit in response["hits"]["hits"]: print(hit["_source"])
Updating a document
editYou can call the update
API to update a document:
client.update(index="books", id="2", doc={ "author": "James Augustine Aloysius Joyce", "pages": 732, })
Deleting a document
editYou can call the delete
API to delete a document:
client.delete(index="books", id="3")
Deleting an index
editclient.indices.delete(index="books")
On this page