Skip to main content

Python low-level client for OpenSearch

Project description

Release CI Integration Chat PRs welcome!

OpenSearch logo

OpenSearch Python Client

Welcome!

opensearch-py is a community-driven, open source fork of elasticsearch-py licensed under the Apache v2.0 License. For more information, see opensearch.org.

This is the low-level client. A high-level Python client is in the works, and will be available soon.

Setup

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

pip install git+https://github.com/opensearch-project/opensearch-py.git

Then import it like any other module:

from opensearchpy import OpenSearch

If you prefer to add the client manually or just want to examine the source code, see opensearch-py on GitHub.

Sample code

from opensearchpy import OpenSearch

host = 'localhost'
port = 9200
auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA.

# Optional client certificates if you don't want to use HTTP basic authentication.
# client_cert_path = '/full/path/to/client.pem'
# client_key_path = '/full/path/to/client-key.pem'

# Create the client with SSL/TLS enabled, but hostname verification disabled.
client = OpenSearch(
    hosts = [{'host': host, 'port': port}],
    http_compress = True, # enables gzip compression for request bodies
    http_auth = auth,
    # client_cert = client_cert_path,
    # client_key = client_key_path,
    use_ssl = True,
    verify_certs = True,
    ssl_assert_hostname = False,
    ssl_show_warn = False,
    ca_certs = ca_certs_path
)

# Create an index with non-default settings.
index_name = 'python-test-index3'
index_body = {
  'settings': {
    'index': {
      'number_of_shards': 4
    }
  }
}

response = client.indices.create(index_name, body=index_body)
print('\nCreating index:')
print(response)

# Add a document to the index.
document = {
  'title': 'Moneyball',
  'director': 'Bennett Miller',
  'year': '2011'
}
id = '1'

response = client.index(
    index = index_name,
    body = document,
    id = id,
    refresh = True
)

print('\nAdding document:')
print(response)

# Search for the document.
q = 'miller'
query = {
  'size': 5,
  'query': {
    'multi_match': {
      'query': q,
      'fields': ['title^2', 'director']
    }
  }
}

response = client.search(
    body = query,
    index = index_name
)
print('\nSearch results:')
print(response)

# Delete the document.
response = client.delete(
    index = index_name,
    id = id
)

print('\nDeleting document:')
print(response)

# Delete the index.
response = client.indices.delete(
    index = index_name
)

print('\nDeleting index:')
print(response)

Project Resources

Code of Conduct

This project has adopted the Amazon Open Source Code of Conduct. For more information see the Code of Conduct FAQ, or contact opensource-codeofconduct@amazon.com with any additional questions or comments.

License

This project is licensed under the Apache v2.0 License.

Copyright

Copyright OpenSearch Contributors. See NOTICE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opensearch-py-1.0.0.tar.gz (137.0 kB view details)

Uploaded Source

Built Distribution

opensearch_py-1.0.0-py2.py3-none-any.whl (207.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file opensearch-py-1.0.0.tar.gz.

File metadata

  • Download URL: opensearch-py-1.0.0.tar.gz
  • Upload date:
  • Size: 137.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

File hashes

Hashes for opensearch-py-1.0.0.tar.gz
Algorithm Hash digest
SHA256 fa952836cabfa1b2fb05f852edc1a373342494345e89fd52b7124daf4d296bb4
MD5 1c79d7ecca29088ee41bfd93ed87a7fa
BLAKE2b-256 2989b22f263ec7f79c443535f60762a8731847e84b983cf48d07b07a9e778113

See more details on using hashes here.

File details

Details for the file opensearch_py-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: opensearch_py-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 207.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

File hashes

Hashes for opensearch_py-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 17afebc25dc890b96c4e9ec8692dcfdb6842c028ce8c2d252e8f55c587960177
MD5 2d6855cca4a108946ed3900ef778850c
BLAKE2b-256 d031cae23208dedb71ee0141412a2e4149c6ef38627a02e5824913af6071c91a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page