Skip to content

Commit

Permalink
Adding bulk api docs to getting_started (#187)
Browse files Browse the repository at this point in the history
* Adding bulk api docs to getting_started

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* Removing unnecessary file from gitignore

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* Removing local test files from gitignore

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* Separating docs into each use case

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>
  • Loading branch information
harshavamsi authored Aug 26, 2022
1 parent 035b472 commit 382f63e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ cython_debug/
# opensearch files
test_opensearch/cover
test_opensearch/local.py
.ci/output
.ci/output
56 changes: 52 additions & 4 deletions GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ If you prefer to add the client manually or just want to examine the source code

## Sample code

### Creating a client

```python
from opensearchpy import OpenSearch

Expand All @@ -51,6 +53,10 @@ client = OpenSearch(
ca_certs = ca_certs_path
)

```

### Creating an index
```python
# Create an index with non-default settings.
index_name = 'python-test-index3'
index_body = {
Expand All @@ -64,8 +70,10 @@ index_body = {
response = client.indices.create(index_name, body=index_body)
print('\nCreating index:')
print(response)
```

# Add a document to the index.
### Adding a document to an index
```python
document = {
'title': 'Moneyball',
'director': 'Bennett Miller',
Expand All @@ -82,8 +90,44 @@ response = client.index(

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

### Adding documents in bulk
```python
docs = '{"index": {"_index": "index-2022-06-08", "_id": "1"}} \n
{"name": "foo"} \n
{"index": {"_index": "index-2022-06-09", "_id": "2"}} \n
{"name": "bar"} \n
{"index": {"_index": "index-2022-06-10", "_id": "3"}} \n
{"name": "baz"}'

response = client.bulk(docs)

print('\nAdding bulk documents:')
print(response)
```

### Adding documents in bulk using helper functions
```python
docs = []
def generate_data():
mywords = ['foo', 'bar', 'baz']
for index, word in enumerate(mywords):
docs.append({
"_index": "mywords",
"word": word,
"_id": index
})
return docs

response = helpers.bulk(client, generate_data(), max_retries=3)

print('\nAdding bulk documents using helper:')
print(response)
```

# Search for the document.
### Searching for a document
```python
q = 'miller'
query = {
'size': 5,
Expand All @@ -101,17 +145,21 @@ response = client.search(
)
print('\nSearch results:')
print(response)
```

# Delete the document.
### Deleting a document
```python
response = client.delete(
index = index_name,
id = id
)

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

# Delete the index.
### Deleting an index
```python
response = client.indices.delete(
index = index_name
)
Expand Down

0 comments on commit 382f63e

Please sign in to comment.