SDKs overview
Elasticsearch offers a range of official client libraries, often referred to as SDKs (Software Development Kits), designed to interact with its RESTful API. These client libraries aim to simplify development by providing idiomatic interfaces for various programming languages, abstracting the direct HTTP requests and JSON parsing. The official clients support a wide array of operations, from indexing and searching documents to managing indices and cluster settings, as detailed in the Elasticsearch documentation.
Integrating these SDKs allows developers to build search functionalities, real-time analytics, and data processing pipelines directly within their applications, without needing to manually construct and parse HTTP requests. This approach can improve development speed and reduce the potential for integration errors. Developers can choose the client that best fits their application's technology stack.
Official SDKs by language
Elasticsearch maintains official client libraries for several popular programming languages. These clients are developed and supported by Elastic, ensuring compatibility with the latest Elasticsearch features and versions. They are generally recommended for most applications due to their ongoing maintenance and comprehensive feature support. The official Elasticsearch clients page provides the most current list and detailed information.
Below is a summary of the officially supported client libraries:
| Language | Package/Module | Installation Command | Maturity |
|---|---|---|---|
| Java | org.elasticsearch.client:elasticsearch-rest-high-level-client (legacy) / co.elastic.clients:elasticsearch-java (new client) |
Maven: Add to pom.xml dependenciesGradle: Add to build.gradle dependencies |
Stable (new client recommended) |
| JavaScript | @elastic/elasticsearch |
npm install @elastic/elasticsearch |
Stable |
| Python | elasticsearch |
pip install elasticsearch |
Stable |
| Ruby | elasticsearch |
gem install elasticsearch |
Stable |
| Go | github.com/elastic/go-elasticsearch/v8 |
go get github.com/elastic/go-elasticsearch/v8 |
Stable |
| PHP | elasticsearch/elasticsearch |
composer require elasticsearch/elasticsearch |
Stable |
| .NET | Elasticsearch.Net / NEST |
dotnet add package NEST |
Stable |
| Rust | elasticsearch |
cargo add elasticsearch |
Stable |
Installation
Installation procedures for Elasticsearch SDKs typically follow the standard package management practices of their respective programming languages. For most languages, this involves using a command-line tool to add the client library as a dependency to your project.
JavaScript (Node.js)
npm install @elastic/elasticsearch
For more detailed instructions, refer to the Elasticsearch Node.js client installation guide.
Python
pip install elasticsearch
Additional details regarding the Python client can be found in the Elasticsearch Python client documentation.
Java
For Maven projects, add the following to your pom.xml dependencies (for the new Java client):
<dependencies>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.x.y</version> <!-- Use the latest stable version -->
</dependency>
</dependencies>
For Gradle projects, add to your build.gradle dependencies:
dependencies {
implementation 'co.elastic.clients:elasticsearch-java:8.x.y' // Use the latest stable version
}
Ensure you also include a JSON processing library like Jackson or Gson. More information is available in the Elasticsearch Java client getting started guide.
Go
go get github.com/elastic/go-elasticsearch/v8
Instructions for the Go client are provided in the Elasticsearch Go client documentation.
Ruby
gem install elasticsearch
Refer to the Elasticsearch Ruby client documentation for complete setup details.
PHP
composer require elasticsearch/elasticsearch
The Elasticsearch PHP client getting started guide offers further installation and usage information.
.NET
dotnet add package NEST
The Elasticsearch .NET NEST client getting started guide provides comprehensive installation instructions.
Rust
cargo add elasticsearch
For more details on the Rust client, consult the Elasticsearch Rust client documentation.
Quickstart example
This quickstart demonstrates indexing a document and performing a basic search using the Python client. This assumes an Elasticsearch instance is running and accessible (e.g., at http://localhost:9200).
Python Quickstart
- Install the client:
pip install elasticsearch - Run the Python code:
from elasticsearch import Elasticsearch # Connect to Elasticsearch # Replace 'http://localhost:9200' with your Elasticsearch host if different client = Elasticsearch( "http://localhost:9200", # Uncomment and replace with actual credentials if using authentication # api_key=("YOUR_API_KEY_ID", "YOUR_API_KEY_SECRET"), # basic_auth=("username", "password"), ) # Ping the cluster to check connection if client.ping(): print("Successfully connected to Elasticsearch!") else: print("Could not connect to Elasticsearch!") exit() # 1. Index a document index_name = "my_documents" doc_id = "1" document = { "title": "The Ultimate Guide to Elasticsearch", "author": "Elastic Engineer", "content": "Elasticsearch is a distributed, RESTful search and analytics engine. It allows you to store, search, and analyze big volumes of data very quickly.", "timestamp": "2023-01-15T10:00:00Z" } try: response = client.index(index=index_name, id=doc_id, document=document) print(f"Indexed document {doc_id}: {response['result']}") except Exception as e: print(f"Error indexing document: {e}") # 2. Refresh the index to make the document searchable immediately client.indices.refresh(index=index_name) # 3. Search for a document search_query = { "match": { "content": "search engine" } } try: search_results = client.search(index=index_name, query=search_query) print("\nSearch results:") for hit in search_results['hits']['hits']: print(f" ID: {hit['_id']}, Score: {hit['_score']}, Source: {hit['_source']['title']}") except Exception as e: print(f"Error searching: {e}") # Optional: Delete the index if you want to clean up # try: # client.indices.delete(index=index_name, ignore=[400, 404]) # print(f"\nIndex '{index_name}' deleted.") # except Exception as e: # print(f"Error deleting index: {e}")
This example connects to an Elasticsearch instance, indexes a single document, and then performs a basic full-text search. The client.ping() method verifies connectivity, while client.index() adds data, and client.search() executes a query. Remember to handle potential exceptions like network issues or indexing failures in production code, as recommended by general HTTP status code guidelines.
Community libraries
While Elastic provides official client libraries for a wide range of languages, the open-source nature of Elasticsearch has also fostered a vibrant community that develops and maintains additional tools and libraries. These community-contributed projects can vary widely in scope, from alternative client implementations to specialized integration tools, ORMs, and data synchronization utilities.
Examples of community contributions include:
- ODM/ORM Integrations: Libraries that integrate Elasticsearch with Object Document Mappers or Object-Relational Mappers in languages like Python (e.g., Django, SQLAlchemy plugins) or Ruby (e.g., ActiveRecord integrations), allowing developers to interact with Elasticsearch using data models familiar from their primary application databases.
- Specialized Clients: Clients for less common languages or those offering different architectural approaches (e.g., reactive clients, asynchronous clients).
- Data Connectors: Tools for moving data between Elasticsearch and other systems, such as Kafka, various databases, or enterprise data warehouses, beyond what Logstash or Beats might cover natively. For example, some community-driven Kafka Connectors specifically target Elasticsearch integration.
- Testing Utilities: Libraries that facilitate testing Elasticsearch interactions within application test suites, often by providing embedded Elasticsearch instances or mock clients.
When considering community libraries, it is important to evaluate their maintenance status, community support, and compatibility with your version of Elasticsearch. Resources like GitHub and language-specific package repositories (e.g., PyPI for Python, Maven Central for Java) are good places to discover these projects. The Elastic community page often highlights active projects and discussion forums where such libraries are discussed.
For critical production systems, prioritizing official clients is generally advisable due to their comprehensive testing, direct support from Elastic, and guaranteed compatibility with new Elasticsearch releases. Community libraries can be valuable for specific niche requirements or when official support is not available for a particular pattern or language.