Low-level Python client

    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.

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

    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

    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:

    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

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

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

    copy

    Performing bulk operations

    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:

    copy

    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

    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. # Add a document to the index.
    34. document = {
    35. 'title': 'Moneyball',
    36. 'director': 'Bennett Miller',
    37. 'year': '2011'
    38. }
    39. id = '1'
    40. response = client.index(
    41. index = index_name,
    42. body = document,
    43. id = id,
    44. refresh = True
    45. )
    46. print('\nAdding document:')
    47. print(response)
    48. # Perform bulk operations
    49. 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"} }'
    50. client.bulk(movies)
    51. # Search for the document.
    52. q = 'miller'
    53. query = {
    54. 'size': 5,
    55. 'query': {
    56. 'multi_match': {
    57. 'query': q,
    58. 'fields': ['title^2', 'director']
    59. }
    60. }
    61. }
    62. response = client.search(
    63. body = query,
    64. index = index_name
    65. )
    66. print('\nSearch results:')
    67. print(response)
    68. # Delete the document.
    69. response = client.delete(
    70. index = index_name,
    71. id = id
    72. )
    73. print('\nDeleting document:')
    74. print(response)
    75. # Delete the index.
    76. response = client.indices.delete(
    77. index = index_name
    78. )

    copy