From c6f11ac155a4ef7801321ec9dd9d6d08b594a5d3 Mon Sep 17 00:00:00 2001 From: Harsha Vamsi Kalluri Date: Thu, 18 Aug 2022 18:32:32 +0000 Subject: [PATCH 1/4] Adding bulk api docs to getting_started Signed-off-by: Harsha Vamsi Kalluri --- .gitignore | 7 +++++++ GETTING_STARTED.md | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/.gitignore b/.gitignore index cf8ad8bc..cfee30a7 100644 --- a/.gitignore +++ b/.gitignore @@ -142,3 +142,10 @@ cython_debug/ test_opensearch/cover test_opensearch/local.py .ci/output + +# Local test file +test_opensearchpy/local.py +test_opensearchpy/custom-opensearch.yml + +# Certificates +root-ca.pem \ No newline at end of file diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index 98ce5a35..26d61317 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -118,6 +118,34 @@ response = client.indices.delete( print('\nDeleting index:') print(response) + + +# Bulk index documents + +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"}' + +client.bulk(docs) + + +# Bulk index documents using the helper function + +docs = [] +def generate_data(): + mywords = ['foo', 'bar', 'baz'] + for index, word in enumerate(mywords): + docs.append({ + "_index": "mywords", + "word": word, + "_id": index + }) + return docs + +helpers.bulk(client, generate_data(), max_retries=3) ``` ## Using IAM credentials for authentication From 7f2a2df4fc4a1763c2e1079b9e822c937dc60cb7 Mon Sep 17 00:00:00 2001 From: Harsha Vamsi Kalluri Date: Thu, 18 Aug 2022 20:51:05 +0000 Subject: [PATCH 2/4] Removing unnecessary file from gitignore Signed-off-by: Harsha Vamsi Kalluri --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index cfee30a7..3cb74ea9 100644 --- a/.gitignore +++ b/.gitignore @@ -145,7 +145,6 @@ test_opensearch/local.py # Local test file test_opensearchpy/local.py -test_opensearchpy/custom-opensearch.yml # Certificates root-ca.pem \ No newline at end of file From 83d295fe1d04d4a8bda4078c51a784bec296f001 Mon Sep 17 00:00:00 2001 From: Harsha Vamsi Kalluri Date: Thu, 18 Aug 2022 22:33:49 +0000 Subject: [PATCH 3/4] Removing local test files from gitignore Signed-off-by: Harsha Vamsi Kalluri --- .gitignore | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 3cb74ea9..9c942d0e 100644 --- a/.gitignore +++ b/.gitignore @@ -141,10 +141,4 @@ cython_debug/ # opensearch files test_opensearch/cover test_opensearch/local.py -.ci/output - -# Local test file -test_opensearchpy/local.py - -# Certificates -root-ca.pem \ No newline at end of file +.ci/output \ No newline at end of file From 4ca574dc5e59e1a8eec3c64abbac35d271153d7d Mon Sep 17 00:00:00 2001 From: Harsha Vamsi Kalluri Date: Fri, 26 Aug 2022 17:04:55 +0000 Subject: [PATCH 4/4] Separating docs into each use case Signed-off-by: Harsha Vamsi Kalluri --- GETTING_STARTED.md | 84 ++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index 26d61317..a2b5c3c9 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -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) +``` + +### 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, @@ -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,42 +156,16 @@ response = client.delete( print('\nDeleting document:') print(response) +``` -# Delete the index. +### Deleting an index +```python response = client.indices.delete( index = index_name ) print('\nDeleting index:') print(response) - - -# Bulk index documents - -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"}' - -client.bulk(docs) - - -# Bulk index documents using the helper function - -docs = [] -def generate_data(): - mywords = ['foo', 'bar', 'baz'] - for index, word in enumerate(mywords): - docs.append({ - "_index": "mywords", - "word": word, - "_id": index - }) - return docs - -helpers.bulk(client, generate_data(), max_retries=3) ``` ## Using IAM credentials for authentication