Skip to content

Commit

Permalink
Fixed black formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Djcarrillo6 <[email protected]>
  • Loading branch information
Djcarrillo6 committed Oct 27, 2023
2 parents 200545c + 627e717 commit 15ea292
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion guides/document_lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
47 changes: 26 additions & 21 deletions samples/document_lifecycle/document_lifecycle_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,66 @@

# 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
result = client.exists(index=index, id=1)
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)
Expand All @@ -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!")
print("Deleted index!")

0 comments on commit 15ea292

Please sign in to comment.