Adding bulk api docs to getting_started (#187)
* Adding bulk api docs to getting_started Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com> * Removing unnecessary file from gitignore Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com> * Removing local test files from gitignore Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com> * Separating docs into each use case Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com> Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com>
This commit is contained in:
committed by
GitHub
parent
035b472650
commit
382f63e033
+1
-1
@@ -141,4 +141,4 @@ cython_debug/
|
||||
# opensearch files
|
||||
test_opensearch/cover
|
||||
test_opensearch/local.py
|
||||
.ci/output
|
||||
.ci/output
|
||||
+52
-4
@@ -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
|
||||
|
||||
@@ -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 = {
|
||||
@@ -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',
|
||||
@@ -82,8 +90,44 @@ response = client.index(
|
||||
|
||||
print('\nAdding document:')
|
||||
print(response)
|
||||
```
|
||||
|
||||
# Search for the document.
|
||||
### 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)
|
||||
```
|
||||
|
||||
### Searching for a document
|
||||
```python
|
||||
q = 'miller'
|
||||
query = {
|
||||
'size': 5,
|
||||
@@ -101,8 +145,10 @@ response = client.search(
|
||||
)
|
||||
print('\nSearch results:')
|
||||
print(response)
|
||||
```
|
||||
|
||||
# Delete the document.
|
||||
### Deleting a document
|
||||
```python
|
||||
response = client.delete(
|
||||
index = index_name,
|
||||
id = id
|
||||
@@ -110,8 +156,10 @@ response = client.delete(
|
||||
|
||||
print('\nDeleting document:')
|
||||
print(response)
|
||||
```
|
||||
|
||||
# Delete the index.
|
||||
### Deleting an index
|
||||
```python
|
||||
response = client.indices.delete(
|
||||
index = index_name
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user