Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding bulk api docs to getting_started #187

Merged
merged 4 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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