Connecting to the Elasticsearch (ES) server is a common operation for data search and analysis.Elasticsearch is a Lucene-based search engine that provides a RESTful API for indexing, searching, and managing data.
Below is a detailed Python code example showing how to connect to an Elasticsearch server and perform some basic operations. This example uses the officialelasticsearch-py
Client libraries.
1. Install the Elasticsearch client library
First, you need to install theelasticsearch
Library. If you haven't installed it yet, you can use pip to do so:
bash copy code
pip install elasticsearch
2. Connect to the Elasticsearch server
Below is a complete Python script showing how to connect to an Elasticsearch server, create an index, add documents, and perform a search.
from elasticsearch import Elasticsearch, helpers
# configureElasticsearchgrout
es = Elasticsearch(
['http://localhost:9200'], # Elasticsearchserver address and port
http_auth=('username', 'password'), # If certification is required,Fill in your username and password
use_ssl=False, # If you use theHTTPS,set toTrue
verify_certs=False # If you use theHTTPSself-signed certificate,set toFalse
)
# 检查grout是否成功
if ():
print("Successfully connected to Elasticsearch!")
else:
print("Could not connect to Elasticsearch")
exit()
# Creating Indexes
index_name = 'my_index'
if not (index=index_name):
# Defining the mapping of an index(Schema)
mappings = {
'properties': {
'title': {'type': 'text'},
'content': {'type': 'text'},
'author': {'type': 'keyword'}
}
}
# Creating Indexes
(index=index_name, body={'mappings': mappings})
print(f"Index '{index_name}' created successfully.")
else:
print(f"Index '{index_name}' already exists.")
# Adding Documents
documents = [
{"_id": 1, "title": "Elasticsearch Basics", "content": "Learn the basics of Elasticsearch.", "author": "John Doe"},
{"_id": 2, "title": "Advanced Elasticsearch", "content": "Go deeper into Elasticsearch features.", "author": "Jane Smith"},
{"_id": 3, "title": "Elasticsearch Performance", "content": "Optimize Elasticsearch for performance.", "author": "Alice Johnson"}
]
# utilizationbulk API批量Adding Documents
actions = [
{
"_index": index_name,
"_id": doc['_id'],
"_source": doc
}
for doc in documents
]
(es, actions)
print("Documents added successfully.")
# Search Documents
search_body = {
"query": {
"match": {
"content": "Elasticsearch"
}
}
}
response = (index=index_name, body=search_body)
print("Search results:")
for hit in response['hits']['hits']:
print(hit['_source'])
# clear up(selectable):Delete Index
# (index=index_name)
# print(f"Index '{index_name}' deleted successfully.")
3. Code Interpretation
- Connection Configuration:
-
Elasticsearch(['http://localhost:9200'])
: Connects to the Elasticsearch server running on the localhost on port 9200 by default. -
http_auth=('username', 'password')
: If the Elasticsearch server requires authentication, fill in the username and password. -
use_ssl
cap (a poem)verify_certs
: These options can be enabled if the connection uses HTTPS.
-
- Check the connection:
- utilization
()
method to check if the connection was successful.
- utilization
- Create an index:
- utilization
(index=index_name)
Check if the index exists. - utilization
(index=index_name, body={'mappings': mappings})
Creates an index and defines the mapping of documents.
- utilization
- Add Documentation:
- utilization
(es, actions)
Batch add documents to the index.
- utilization
- Search for documents:
- utilization
(index=index_name, body=search_body)
Performs a search and prints the results.
- utilization
- Cleanup (optional):
- utilization
(index=index_name)
Delete the index.
- utilization
4. Cautions
- server address: Ensure that the Elasticsearch server is running and that the address and port are configured correctly.
- accreditation: If the Elasticsearch server requires authentication, make sure to provide the correct username and password.
-
SSL: If the connection uses HTTPS, configure it correctly
use_ssl
cap (a poem)verify_certs
Options.