Convert Figma logo to code with AI

elastic logoelasticsearch-py

Official Python client for Elasticsearch

4,224
1,178
4,224
57

Top Related Projects

High level Python client for Elasticsearch

7,399

Deprecated: Use the official Elasticsearch client for Go at https://github.com/elastic/go-elasticsearch

The official Go client for Elasticsearch

Intelligent search made easy

Quick Overview

Elasticsearch-py is the official low-level Python client for Elasticsearch. It provides a Pythonic way to interact with Elasticsearch, allowing developers to perform various operations such as indexing, searching, and managing Elasticsearch clusters directly from their Python applications.

Pros

  • Officially maintained by Elastic, ensuring compatibility and up-to-date features
  • Supports both synchronous and asynchronous operations
  • Comprehensive documentation and examples
  • Flexible configuration options for connection pooling and retry mechanisms

Cons

  • Lower-level API compared to some higher-level alternatives
  • Requires more boilerplate code for complex operations
  • Learning curve for users new to Elasticsearch concepts
  • Limited built-in query DSL helpers (though this is by design for flexibility)

Code Examples

  1. Connecting to Elasticsearch and indexing a document:
from elasticsearch import Elasticsearch

es = Elasticsearch("http://localhost:9200")
doc = {"title": "Example Document", "content": "This is a test document."}
response = es.index(index="my_index", document=doc)
print(response['result'])
  1. Performing a search query:
from elasticsearch import Elasticsearch

es = Elasticsearch("http://localhost:9200")
query = {
    "query": {
        "match": {
            "content": "test document"
        }
    }
}
response = es.search(index="my_index", body=query)
for hit in response['hits']['hits']:
    print(hit['_source'])
  1. Using the async client:
import asyncio
from elasticsearch import AsyncElasticsearch

async def main():
    es = AsyncElasticsearch("http://localhost:9200")
    response = await es.info()
    print(response)
    await es.close()

asyncio.run(main())

Getting Started

To get started with elasticsearch-py, follow these steps:

  1. Install the library:

    pip install elasticsearch
    
  2. Import and create an Elasticsearch client:

    from elasticsearch import Elasticsearch
    
    es = Elasticsearch("http://localhost:9200")
    
  3. Perform operations:

    # Index a document
    doc = {"title": "Hello", "content": "World"}
    es.index(index="test_index", document=doc)
    
    # Search for documents
    result = es.search(index="test_index", query={"match": {"content": "World"}})
    print(result['hits']['hits'])
    

For more detailed information and advanced usage, refer to the official documentation.

Competitor Comparisons

High level Python client for Elasticsearch

Pros of elasticsearch-dsl-py

  • Provides a high-level, Pythonic API for building and executing Elasticsearch queries
  • Offers object-oriented query construction, making complex queries more readable and maintainable
  • Includes built-in serialization and deserialization of documents

Cons of elasticsearch-dsl-py

  • Steeper learning curve due to additional abstraction layer
  • May have slightly lower performance for simple queries compared to direct API calls
  • Less flexibility for advanced or custom query structures

Code Comparison

elasticsearch-py:

from elasticsearch import Elasticsearch

es = Elasticsearch()
result = es.search(index="my-index", body={
    "query": {"match": {"title": "python"}}
})

elasticsearch-dsl-py:

from elasticsearch_dsl import Search, Q

s = Search(index="my-index").query("match", title="python")
response = s.execute()

The elasticsearch-dsl-py example demonstrates a more Pythonic and readable approach to constructing queries, while the elasticsearch-py example shows a more direct, low-level interaction with the Elasticsearch API. The choice between the two libraries depends on the specific needs of the project, with elasticsearch-dsl-py being particularly useful for complex queries and larger applications.

7,399

Deprecated: Use the official Elasticsearch client for Go at https://github.com/elastic/go-elasticsearch

Pros of elastic

  • Written in Go, offering better performance and concurrency support
  • More comprehensive API coverage, including support for newer Elasticsearch features
  • Actively maintained with frequent updates and releases

Cons of elastic

  • Steeper learning curve due to more complex API structure
  • Larger codebase and dependencies compared to elasticsearch-py
  • May require more setup and configuration for basic use cases

Code Comparison

elasticsearch-py:

from elasticsearch import Elasticsearch

es = Elasticsearch()
result = es.search(index="my-index", body={"query": {"match_all": {}}})

elastic:

import "github.com/olivere/elastic/v7"

client, _ := elastic.NewClient()
result, _ := client.Search().
    Index("my-index").
    Query(elastic.MatchAllQuery{}).
    Do(context.Background())

Both libraries provide similar functionality for basic Elasticsearch operations. The Go-based elastic offers more verbose but type-safe code, while elasticsearch-py provides a simpler, more Pythonic interface. The choice between them often depends on the preferred programming language and specific project requirements.

The official Go client for Elasticsearch

Pros of go-elasticsearch

  • Better performance and lower memory footprint due to Go's efficiency
  • Strong type safety and compile-time checks
  • Concurrency-friendly with built-in goroutines and channels

Cons of go-elasticsearch

  • Less mature ecosystem compared to Python
  • Steeper learning curve for developers not familiar with Go
  • Fewer third-party libraries and integrations available

Code Comparison

elasticsearch-py:

from elasticsearch import Elasticsearch

es = Elasticsearch(["http://localhost:9200"])
result = es.search(index="my-index", body={"query": {"match_all": {}}})

go-elasticsearch:

import "github.com/elastic/go-elasticsearch/v8"

es, _ := elasticsearch.NewDefaultClient()
res, _ := es.Search(
    es.Search.WithIndex("my-index"),
    es.Search.WithBody(strings.NewReader(`{"query":{"match_all":{}}}`)),
)

Both libraries provide similar functionality for interacting with Elasticsearch, but go-elasticsearch offers stronger typing and better performance at the cost of a steeper learning curve. The Python version is more concise and easier to read, while the Go version provides more explicit error handling and type safety. Ultimately, the choice between the two depends on the project requirements, team expertise, and performance needs.

Intelligent search made easy

Pros of Searchkick

  • Simpler, more Ruby-like syntax for querying Elasticsearch
  • Built-in support for common search features like autocomplete and faceted search
  • Seamless integration with ActiveRecord and other Ruby ORMs

Cons of Searchkick

  • Limited to Ruby ecosystem, while elasticsearch-py is Python-based
  • May have less granular control over complex Elasticsearch operations
  • Potentially slower performance due to abstraction layer

Code Comparison

Searchkick (Ruby):

Product.search("apple", where: {in_stock: true}, limit: 10)

elasticsearch-py (Python):

es.search(index="products", body={
    "query": {"bool": {"must": [{"match": {"name": "apple"}}, {"term": {"in_stock": True}}]}},
    "size": 10
})

Searchkick provides a more concise and readable syntax for common search operations, while elasticsearch-py offers more direct control over the Elasticsearch query DSL. The choice between the two largely depends on the programming language preference and the level of abstraction desired for interacting with Elasticsearch.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Elastic logo

Elasticsearch Python Client

PyPI Version Python Versions Conda Version Downloads
Build Status on GitHub Buildkite Status on Buildkite Documentation Status

The official Python client for Elasticsearch.

Features

  • Translating basic Python data types to and from JSON
  • Configurable automatic discovery of cluster nodes
  • Persistent connections
  • Load balancing (with pluggable selection strategy) across available nodes
  • Failed connection penalization (time based - failed connections won't be retried until a timeout is reached)
  • Support for TLS and HTTP authentication
  • Thread safety across requests
  • Pluggable architecture
  • Helper functions for idiomatically using APIs together

Installation

Download the latest version of Elasticsearch or sign-up for a free trial of Elastic Cloud.

Refer to the Installation section of the getting started documentation.

Connecting

Refer to the Connecting section of the getting started documentation.

Usage


Compatibility

Language clients are forward compatible; meaning that the clients support communicating with greater or equal minor versions of Elasticsearch without breaking. It does not mean that the clients automatically support new features of newer Elasticsearch versions; it is only possible after a release of a new client version. For example, a 8.12 client version won't automatically support the new features of the 8.13 version of Elasticsearch, the 8.13 client version is required for that. Elasticsearch language clients are only backwards compatible with default distributions and without guarantees made.

Elasticsearch VersionElasticsearch-Python BranchSupported
mainmain
8.x8.x8.x
7.x7.x7.17

If you have a need to have multiple versions installed at the same time older versions are also released as elasticsearch7 and elasticsearch8.

Documentation

Documentation for the client is available on elastic.co and Read the Docs.

Feedback 🗣️

The engineering team here at Elastic is looking for developers to participate in research and feedback sessions to learn more about how you use our Python client and what improvements we can make to their design and your workflow. If you're interested in sharing your insights into developer experience and language client design, please fill out this short form. Depending on the number of responses we get, we may either contact you for a 1:1 conversation or a focus group with other developers who use the same client. Thank you in advance - your feedback is crucial to improving the user experience for all Elasticsearch developers!

License

This software is licensed under the Apache License 2.0. See NOTICE.