OpenSearch Protobufs Python Client
The `opensearch-protobufs` library provides Protocol Buffer definitions and generated Python code for interacting with OpenSearch's gRPC APIs. It serves as a downstream consumer of the `opensearch-api-specification`, packaging pre-generated code to simplify client-server communication for various OpenSearch projects. The library is actively maintained with frequent releases, offering a high-performance alternative to traditional REST APIs through binary serialization and gRPC.
Warnings
- breaking The gRPC Search API is currently experimental, and its protobuf structure is subject to changes in future OpenSearch versions. This means that API structures and generated code might change in backward-incompatible ways between minor or major releases, requiring client-side code updates.
- gotcha When sending document data (e.g., for Bulk API operations), documents must be provided as Base64 encoded bytes within the gRPC request. Directly providing plain JSON strings or Python dictionaries will result in errors.
- gotcha This library (`opensearch-protobufs`) only provides the *generated Python code* from `.proto` files. To actually make gRPC calls, you must explicitly install and use the `grpcio` library. If you need to generate protobuf code yourself from `.proto` definitions, `grpcio-tools` is also required.
- gotcha Compatibility between `opensearch-protobufs` client versions and OpenSearch server versions is crucial. Using an incompatible client-server pair can lead to unexpected behavior or gRPC errors.
Install
-
pip install opensearch-protobufs
Imports
- SearchRequest
from opensearch.protobufs.schemas import SearchRequest
- BulkRequest
from opensearch.protobufs.schemas import BulkRequest
- SearchServiceStub
from opensearch.protobufs.services import SearchServiceStub
- DocumentServiceStub
from opensearch.protobufs.services import DocumentServiceStub
Quickstart
import grpc
import os
import base64
from opensearch.protobufs.schemas import SearchRequest, Query, MatchQuery
from opensearch.protobufs.services import SearchServiceStub
# Configure your OpenSearch gRPC endpoint
# In a real application, you'd replace this with your actual OpenSearch gRPC host and port
OPENSEARCH_GRPC_TARGET = os.environ.get('OPENSEARCH_GRPC_TARGET', 'localhost:9200') # Example
def run_search_query():
try:
# Establish an insecure gRPC channel (use secure channels for production!)
with grpc.insecure_channel(OPENSEARCH_GRPC_TARGET) as channel:
stub = SearchServiceStub(channel)
# Create a simple match query
match_query = MatchQuery(field='title', query='OpenSearch')
query = Query(match=match_query)
# Create the search request
search_request = SearchRequest(
query=query,
size=5
)
print(f"Sending SearchRequest to {OPENSEARCH_GRPC_TARGET}:")
print(search_request)
# Make the gRPC call
response = stub.Search(search_request)
print("\nSearch Response:")
for hit in response.hits.hits:
# _source is returned as bytes, decode if it's JSON-like
source_data = hit.source.value.decode('utf-8') if hit.source.value else '{}'
print(f" ID: {hit.id}, Score: {hit.score}, Source: {source_data}")
except grpc.RpcError as e:
print(f"gRPC Error: {e.code()} - {e.details()}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == '__main__':
print("Note: This quickstart requires a running OpenSearch instance with gRPC enabled.")
print(f"Attempting to connect to gRPC target: {OPENSEARCH_GRPC_TARGET}\n")
run_search_query()