Low-level Python client

The OpenSearch low-level Python client (opensearch-py) provides wrapper methods for the OpenSearch REST API so that you can interact with your cluster more naturally in Python. Rather than sending raw HTTP requests to a given URL, you can create an OpenSearch client for your cluster and call the client’s built-in functions. For the client’s complete API documentation and additional examples, see the opensearch-py API documentation.

This getting started guide illustrates how to connect to OpenSearch, index documents, and run queries. For the client source code, see the opensearch-py repo.

Setup

To add the client to your project, install it using pip:

  1. pip install opensearch-py

copy

After installing the client, you can import it like any other module:

  1. from opensearchpy import OpenSearch

copy

Connecting to OpenSearch

To connect to the default OpenSearch host, create a client object with SSL enabled if you are using the Security plugin. You can use the default credentials for testing purposes:

  1. host = 'localhost'
  2. port = 9200
  3. auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
  4. ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA.
  5. # Create the client with SSL/TLS enabled, but hostname verification disabled.
  6. client = OpenSearch(
  7. hosts = [{'host': host, 'port': port}],
  8. http_compress = True, # enables gzip compression for request bodies
  9. http_auth = auth,
  10. use_ssl = True,
  11. verify_certs = True,
  12. ssl_assert_hostname = False,
  13. ssl_show_warn = False,
  14. ca_certs = ca_certs_path
  15. )

copy

If you have your own client certificates, specify them in the client_cert_path and client_key_path parameters:

  1. host = 'localhost'
  2. port = 9200
  3. auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
  4. ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA.
  5. # Optional client certificates if you don't want to use HTTP basic authentication.
  6. client_cert_path = '/full/path/to/client.pem'
  7. client_key_path = '/full/path/to/client-key.pem'
  8. # Create the client with SSL/TLS enabled, but hostname verification disabled.
  9. client = OpenSearch(
  10. hosts = [{'host': host, 'port': port}],
  11. http_compress = True, # enables gzip compression for request bodies
  12. http_auth = auth,
  13. client_cert = client_cert_path,
  14. client_key = client_key_path,
  15. use_ssl = True,
  16. verify_certs = True,
  17. ssl_assert_hostname = False,
  18. ssl_show_warn = False,
  19. ca_certs = ca_certs_path
  20. )

copy

If you are not using the Security plugin, create a client object with SSL disabled:

  1. host = 'localhost'
  2. port = 9200
  3. # Create the client with SSL/TLS and hostname verification disabled.
  4. client = OpenSearch(
  5. hosts = [{'host': host, 'port': port}],
  6. http_compress = True, # enables gzip compression for request bodies
  7. use_ssl = False,
  8. verify_certs = False,
  9. ssl_assert_hostname = False,
  10. ssl_show_warn = False
  11. )

copy

Connecting to Amazon OpenSearch Service

The following example illustrates connecting to Amazon OpenSearch Service:

  1. from opensearchpy import OpenSearch, RequestsHttpConnection, AWSV4SignerAuth
  2. import boto3
  3. host = '' # cluster endpoint, for example: my-test-domain.us-east-1.es.amazonaws.com
  4. region = 'us-west-2'
  5. service = 'es'
  6. credentials = boto3.Session().get_credentials()
  7. auth = AWSV4SignerAuth(credentials, region, service)
  8. client = OpenSearch(
  9. hosts = [{'host': host, 'port': 443}],
  10. http_auth = auth,
  11. use_ssl = True,
  12. verify_certs = True,
  13. connection_class = RequestsHttpConnection,
  14. pool_maxsize = 20
  15. )

copy

Connecting to Amazon OpenSearch Serverless

The following example illustrates connecting to Amazon OpenSearch Serverless Service:

  1. from opensearchpy import OpenSearch, RequestsHttpConnection, AWSV4SignerAuth
  2. import boto3
  3. host = '' # cluster endpoint, for example: my-test-domain.us-east-1.aoss.amazonaws.com
  4. region = 'us-west-2'
  5. service = 'aoss'
  6. credentials = boto3.Session().get_credentials()
  7. auth = AWSV4SignerAuth(credentials, region, service)
  8. client = OpenSearch(
  9. hosts = [{'host': host, 'port': 443}],
  10. http_auth = auth,
  11. use_ssl = True,
  12. verify_certs = True,
  13. connection_class = RequestsHttpConnection,
  14. pool_maxsize = 20
  15. )

copy

Creating an index

To create an OpenSearch index, use the client.indices.create() method. You can use the following code to construct a JSON object with custom settings:

  1. index_name = 'python-test-index'
  2. index_body = {
  3. 'settings': {
  4. 'index': {
  5. 'number_of_shards': 4
  6. }
  7. }
  8. }
  9. response = client.indices.create(index_name, body=index_body)

copy

Indexing a document

You can index a document using the client.index() method:

  1. document = {
  2. 'title': 'Moneyball',
  3. 'director': 'Bennett Miller',
  4. 'year': '2011'
  5. }
  6. response = client.index(
  7. index = 'python-test-index',
  8. body = document,
  9. id = '1',
  10. refresh = True
  11. )

copy

Performing bulk operations

You can perform several operations at the same time by using the bulk() method of the client. The operations may be of the same type or of different types. Note that the operations must be separated by a \n and the entire string must be a single line:

  1. movies = '{ "index" : { "_index" : "my-dsl-index", "_id" : "2" } } \n { "title" : "Interstellar", "director" : "Christopher Nolan", "year" : "2014"} \n { "create" : { "_index" : "my-dsl-index", "_id" : "3" } } \n { "title" : "Star Trek Beyond", "director" : "Justin Lin", "year" : "2015"} \n { "update" : {"_id" : "3", "_index" : "my-dsl-index" } } \n { "doc" : {"year" : "2016"} }'
  2. client.bulk(movies)

copy

Searching for documents

The easiest way to search for documents is to construct a query string. The following code uses a multi-match query to search for “miller” in the title and director fields. It boosts the documents that have “miller” in the title field:

  1. q = 'miller'
  2. query = {
  3. 'size': 5,
  4. 'query': {
  5. 'multi_match': {
  6. 'query': q,
  7. 'fields': ['title^2', 'director']
  8. }
  9. }
  10. }
  11. response = client.search(
  12. body = query,
  13. index = 'python-test-index'
  14. )

copy

Deleting a document

You can delete a document using the client.delete() method:

  1. response = client.delete(
  2. index = 'python-test-index',
  3. id = '1'
  4. )

copy

Deleting an index

You can delete an index using the client.indices.delete() method:

  1. response = client.indices.delete(
  2. index = 'python-test-index'
  3. )

copy

Sample program

The following sample program creates a client, adds an index with non-default settings, inserts a document, performs bulk operations, searches for the document, deletes the document, and then deletes the index:

  1. from opensearchpy import OpenSearch
  2. host = 'localhost'
  3. port = 9200
  4. auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
  5. ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA.
  6. # Optional client certificates if you don't want to use HTTP basic authentication.
  7. # client_cert_path = '/full/path/to/client.pem'
  8. # client_key_path = '/full/path/to/client-key.pem'
  9. # Create the client with SSL/TLS enabled, but hostname verification disabled.
  10. client = OpenSearch(
  11. hosts = [{'host': host, 'port': port}],
  12. http_compress = True, # enables gzip compression for request bodies
  13. http_auth = auth,
  14. # client_cert = client_cert_path,
  15. # client_key = client_key_path,
  16. use_ssl = True,
  17. verify_certs = True,
  18. ssl_assert_hostname = False,
  19. ssl_show_warn = False,
  20. ca_certs = ca_certs_path
  21. )
  22. # Create an index with non-default settings.
  23. index_name = 'python-test-index'
  24. index_body = {
  25. 'settings': {
  26. 'index': {
  27. 'number_of_shards': 4
  28. }
  29. }
  30. }
  31. response = client.indices.create(index_name, body=index_body)
  32. print('\nCreating index:')
  33. print(response)
  34. # Add a document to the index.
  35. document = {
  36. 'title': 'Moneyball',
  37. 'director': 'Bennett Miller',
  38. 'year': '2011'
  39. }
  40. id = '1'
  41. response = client.index(
  42. index = index_name,
  43. body = document,
  44. id = id,
  45. refresh = True
  46. )
  47. print('\nAdding document:')
  48. print(response)
  49. # Perform bulk operations
  50. movies = '{ "index" : { "_index" : "my-dsl-index", "_id" : "2" } } \n { "title" : "Interstellar", "director" : "Christopher Nolan", "year" : "2014"} \n { "create" : { "_index" : "my-dsl-index", "_id" : "3" } } \n { "title" : "Star Trek Beyond", "director" : "Justin Lin", "year" : "2015"} \n { "update" : {"_id" : "3", "_index" : "my-dsl-index" } } \n { "doc" : {"year" : "2016"} }'
  51. client.bulk(movies)
  52. # Search for the document.
  53. q = 'miller'
  54. query = {
  55. 'size': 5,
  56. 'query': {
  57. 'multi_match': {
  58. 'query': q,
  59. 'fields': ['title^2', 'director']
  60. }
  61. }
  62. }
  63. response = client.search(
  64. body = query,
  65. index = index_name
  66. )
  67. print('\nSearch results:')
  68. print(response)
  69. # Delete the document.
  70. response = client.delete(
  71. index = index_name,
  72. id = id
  73. )
  74. print('\nDeleting document:')
  75. print(response)
  76. # Delete the index.
  77. response = client.indices.delete(
  78. index = index_name
  79. )
  80. print('\nDeleting index:')
  81. print(response)

copy