diff --git a/CHANGELOG.md b/CHANGELOG.md index d2ccc9b1..426db9b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Added `pool_maxsize` for `Urllib3HttpConnection` ([#535](https://github.com/opensearch-project/opensearch-py/pull/535)) - Added benchmarks ([#537](https://github.com/opensearch-project/opensearch-py/pull/537)) - Added guide on making raw JSON REST requests ([#542](https://github.com/opensearch-project/opensearch-py/pull/542)) -- Added guide on the document lifecycle API(s) ([#545](https://github.com/opensearch-project/opensearch-py/pull/545)) - Added support for AWS SigV4 for urllib3 ([#547](https://github.com/opensearch-project/opensearch-py/pull/547)) +- Added guide on the document lifecycle API(s) ([#545](https://github.com/opensearch-project/opensearch-py/pull/545)) ### Changed - Generate `tasks` client from API specs ([#508](https://github.com/opensearch-project/opensearch-py/pull/508)) - Generate `ingest` client from API specs ([#513](https://github.com/opensearch-project/opensearch-py/pull/513)) diff --git a/USER_GUIDE.md b/USER_GUIDE.md index 2815d14a..fddd1459 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -35,8 +35,6 @@ In general, we recommend using a package manager, such as [poetry](https://pytho 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 more find information on the full document lifecycle in [guides/document_lifecycle.md](guides/document_lifecycle.md). - You can find working versions of the code below that can be run with a local instance of OpenSearch in [samples](samples). ### Creating a Client @@ -159,6 +157,7 @@ print(response) - [Advanced Index Actions](guides/advanced_index_actions.md) - [Making Raw JSON REST Requests](guides/json.md) - [Connection Classes](guides/connection_classes.md) +- [Document Lifcycle](guides/document_lifecycle.md) ## Plugins diff --git a/guides/document_lifecycle.md b/guides/document_lifecycle.md index 99d9d72f..fcad9e8c 100644 --- a/guides/document_lifecycle.md +++ b/guides/document_lifecycle.md @@ -69,7 +69,7 @@ client.index(index=index, id=2, body={'title': 'The Lion King', 'year': 1994}) You can also create a new document with an auto-generated ID by omitting the `id` parameter. The following code creates documents with an auto-generated IDs in the `movies` index: ```python -OR client.index(index=index, body={"title": "The Lion King 2", "year": 1998}) +client.index(index=index, body={"title": "The Lion King 2", "year": 1998}) ``` In this case, the ID of the created document in the `result` field of the response body: diff --git a/samples/document_lifecycle/document_lifecycle_sample.py b/samples/document_lifecycle/document_lifecycle_sample.py index e93993aa..091619d2 100644 --- a/samples/document_lifecycle/document_lifecycle_sample.py +++ b/samples/document_lifecycle/document_lifecycle_sample.py @@ -7,42 +7,46 @@ # Connect to OpenSearch client = OpenSearch( - hosts=['https://localhost:9200'], + hosts=["https://localhost:9200"], use_ssl=True, verify_certs=False, - http_auth=('admin', 'admin') + http_auth=("admin", "admin"), ) # Create an index -index = 'movies' +index = "movies" if not client.indices.exists(index=index): client.indices.create(index=index) # Create documents -client.index(index=index, id=1, body={'title': 'Beauty and the Beast', 'year': 1991}) -client.index(index=index, id=2, body={'title': 'Beauty and the Beast - Live Action', 'year': 2017}) +client.index(index=index, id=1, body={"title": "Beauty and the Beast", "year": 1991}) +client.index( + index=index, + id=2, + body={"title": "Beauty and the Beast - Live Action", "year": 2017}, +) # Index a document -client.index(index=index, id=2, body={'title': 'The Lion King', 'year': 1994}) +client.index(index=index, id=2, body={"title": "The Lion King", "year": 1994}) # Create a document with auto-generated ID -result = client.index(index=index, body={'title': 'The Lion King 2', 'year': 1998}) +result = client.index(index=index, body={"title": "The Lion King 2", "year": 1998}) print(result) # Get a document -result = client.get(index=index, id=1)['_source'] +result = client.get(index=index, id=1)["_source"] print(result) # Get a document with _source includes -result = client.get(index=index, id=1, _source_includes=['title'])['_source'] +result = client.get(index=index, id=1, _source_includes=["title"])["_source"] print(result) # Get a document with _source excludes -result = client.get(index=index, id=1, _source_excludes=['title'])['_source'] +result = client.get(index=index, id=1, _source_excludes=["title"])["_source"] print(result) # Get multiple documents -result = client.mget(index=index, body={ 'docs': [{ '_id': 1 }, { '_id': 2 }] })['docs'] +result = client.mget(index=index, body={"docs": [{"_id": 1}, {"_id": 2}]})["docs"] print(result) # Check if a document exists @@ -50,16 +54,19 @@ print(result) # Update a document -client.update(index=index, id=1, body={'doc': {'year': 1995}}) +client.update(index=index, id=1, body={"doc": {"year": 1995}}) # Update a document using script -client.update(index=index, id=1, body={ 'script': { 'source': 'ctx._source.year += 5' } }) +client.update(index=index, id=1, body={"script": {"source": "ctx._source.year += 5"}}) # Update multiple documents by query -client.update_by_query(index=index, body={ - 'script': { 'source': 'ctx._source.year -= 1' }, - 'query': { 'range': { 'year': { 'gt': 2023 } } } -}) +client.update_by_query( + index=index, + body={ + "script": {"source": "ctx._source.year -= 1"}, + "query": {"range": {"year": {"gt": 2023}}}, + }, +) # Delete a document client.delete(index=index, id=1) @@ -68,10 +75,8 @@ client.delete(index=index, id=1, ignore=404) # Delete multiple documents by query -client.delete_by_query(index=index, body={ - 'query': { 'range': { 'year': { 'gt': 2023 } } } -}) +client.delete_by_query(index=index, body={"query": {"range": {"year": {"gt": 2023}}}}) # Delete the index client.indices.delete(index=index) -print("Deleted index!") \ No newline at end of file +print("Deleted index!")