0cb345db6e
* Added a guide & sample for a custom logger client implementation. Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Black formatter Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> * Changes from PR review Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Fixed import formatting in sample code for gudie. Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Fixed nox formatting of log collection sample module. Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Added types to log_collection_sample.py Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Added type ignore to StramHandler class Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Added formatting change Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> * Added PR review changes. Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Fixed typo in CHANGELOG. Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Requested changes. Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Requested changes again. Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Added link in USER_GUIDE.md. Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> --------- Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com>
3.8 KiB
3.8 KiB
OpenSearch Python Client User Guide
Setup
To add the client to your project, install it using pip:
pip install opensearch-py
Then import it like any other module:
from opensearchpy import OpenSearch
For better performance we recommend the async client. See Asynchronous I/O for more information.
In general, we recommend using a package manager, such as poetry, for your projects. This is the package manager used for samples.
Basic Features
In the example below, we create a client, create an index with non-default settings, insert a document into the index, search for the document, delete the document, and finally delete the index.
You can find working versions of the code below that can be run with a local instance of OpenSearch in samples.
Creating a Client
from opensearchpy import OpenSearch
host = 'localhost'
port = 9200
auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
client = OpenSearch(
hosts = [{'host': host, 'port': port}],
http_auth = auth,
use_ssl = True,
verify_certs = False
)
info = client.info()
print(f"Welcome to {info['version']['distribution']} {info['version']['number']}!")
See hello.py for a working synchronous sample, and guides/ssl for how to setup SSL certificates.
Creating an Index
index_name = 'test-index'
index_body = {
'settings': {
'index': {
'number_of_shards': 4
}
}
}
response = client.indices.create(
index_name,
body=index_body
)
print(response)
Adding a Document to an Index
document = {
'title': 'Moneyball',
'director': 'Bennett Miller',
'year': '2011'
}
id = '1'
response = client.index(
index = index_name,
body = document,
id = id,
refresh = True
)
print(response)
Searching for a Document
q = 'miller'
query = {
'size': 5,
'query': {
'multi_match': {
'query': q,
'fields': ['title^2', 'director']
}
}
}
response = client.search(
body = query,
index = index_name
)
print(response)
Deleting a Document
response = client.delete(
index = index_name,
id = id
)
print(response)
Deleting an Index
response = client.indices.delete(
index = index_name
)
print(response)
Advanced Features
- Asynchronous I/O
- Authentication (IAM, SigV4)
- Configuring SSL
- Bulk Indexing
- High Level DSL
- Index Lifecycle
- Search
- Point in Time
- Using a Proxy
- Index Templates
- Advanced Index Actions
- Making Raw JSON REST Requests
- Connection Classes
- Document Lifecycle
- Collecting Logs