Skip to content

Commit

Permalink
Added samples, benchmarks and docs for nox format. (#556)
Browse files Browse the repository at this point in the history
* Added samples for nox format.

Signed-off-by: dblock <[email protected]>

* Added space after #!/usr/bin/env python.

Signed-off-by: dblock <[email protected]>

* Added benchmarks and docs.

Signed-off-by: dblock <[email protected]>

---------

Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock authored Oct 27, 2023
1 parent 0da60b2 commit bcfef11
Show file tree
Hide file tree
Showing 28 changed files with 740 additions and 721 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Added `remote store` client APIs ([#552](https://github.com/opensearch-project/opensearch-py/pull/552))
- Added `nox -rs generate` ([#554](https://github.com/opensearch-project/opensearch-py/pull/554))
- Added a utf-8 header to all .py files ([#557](https://github.com/opensearch-project/opensearch-py/pull/557))
- Added `samples`, `benchmarks` and `docs` to `nox -rs format` ([#556](https://github.com/opensearch-project/opensearch-py/pull/556))
### 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
5 changes: 4 additions & 1 deletion benchmarks/bench_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.

import asyncio
import uuid
Expand All @@ -25,7 +28,7 @@ async def index_records(client, item_count):
client.index(
index=index_name,
body={
"title": f"Moneyball",
"title": "Moneyball",
"director": "Bennett Miller",
"year": "2011",
},
Expand Down
6 changes: 5 additions & 1 deletion benchmarks/bench_info_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.


import logging
import sys
Expand Down Expand Up @@ -36,7 +40,7 @@ def get_info(client, request_count):
tt = 0
for n in range(request_count):
start = time.time() * 1000
rc = client.info()
client.info()
total_time = time.time() * 1000 - start
tt += total_time
return tt
Expand Down
8 changes: 5 additions & 3 deletions benchmarks/bench_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.

import json
import logging
import sys
import time
import uuid

Expand All @@ -21,9 +26,6 @@
index_name = "test-index-sync"
item_count = 1000

import logging
import sys

root = logging.getLogger()
# root.setLevel(logging.DEBUG)
# logging.getLogger("urllib3.connectionpool").setLevel(logging.DEBUG)
Expand Down
4 changes: 4 additions & 0 deletions benchmarks/bench_sync_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.


import bench_async
import bench_sync
Expand Down
6 changes: 4 additions & 2 deletions benchmarks/thread_with_return_value.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/usr/bin/env python

# -*- coding: utf-8 -*-
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.


from threading import Thread

Expand Down
9 changes: 9 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.

# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
Expand Down
3 changes: 3 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"opensearchpy/",
"test_opensearchpy/",
"utils/",
"samples/",
"benchmarks/",
"docs/",
)


Expand Down
92 changes: 57 additions & 35 deletions samples/advanced_index_actions/advanced_index_actions_sample.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
from opensearchpy import OpenSearch
#!/usr/bin/env python

# -*- coding: utf-8 -*-
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
import time

from opensearchpy import OpenSearch

# For cleaner output, comment in the two lines below to disable warnings and informational messages
# import urllib3
Expand All @@ -10,73 +21,84 @@
def test_opensearch_examples():
# Set up
client = OpenSearch(
hosts=['https://localhost:9200'],
hosts=["https://localhost:9200"],
use_ssl=True,
verify_certs=False,
http_auth=('admin', 'admin')
http_auth=("admin", "admin"),
)
client.indices.create(index='movies')
client.indices.create(index="movies")
print("'movies' index created!")

# Test Clear Index Cache
client.indices.clear_cache(index='movies')
client.indices.clear_cache(index="movies")
print("Cache for 'movies' index cleared!")
client.indices.clear_cache(index='movies', query=True)
client.indices.clear_cache(index="movies", query=True)
print("Query cache for 'movies' index cleared!")
client.indices.clear_cache(index='movies', fielddata=True, request=True)
client.indices.clear_cache(index="movies", fielddata=True, request=True)
print("Field data and request cache for 'movies' index cleared!")

# Test Flush Index
client.indices.flush(index='movies')
client.indices.flush(index="movies")
print("'movies' index flushed!")

# Test Refresh Index
client.indices.refresh(index='movies')
client.indices.refresh(index="movies")
print("'movies' index refreshed!")

# Test Close or Open Index
client.indices.close(index='movies')
client.indices.close(index="movies")
print("'movies' index closed!")
time.sleep(2) # add sleep to ensure the index has time to close
client.indices.open(index='movies')
client.indices.open(index="movies")
print("'movies' index opened!")

# Test Force Merge Index
client.indices.forcemerge(index='movies')
client.indices.forcemerge(index="movies")
print("'movies' index force merged!")

# Test Clone
client.indices.put_settings(index='movies', body={'index': {'blocks': {'write': True}}})
client.indices.put_settings(
index="movies", body={"index": {"blocks": {"write": True}}}
)
print("Write operations blocked for 'movies' index!")
time.sleep(2)
client.indices.clone(index='movies', target='movies_clone')
client.indices.clone(index="movies", target="movies_clone")
print("'movies' index cloned to 'movies_clone'!")
client.indices.put_settings(index='movies', body={'index': {'blocks': {'write': False}}})
client.indices.put_settings(
index="movies", body={"index": {"blocks": {"write": False}}}
)
print("Write operations enabled for 'movies' index!")
# Test Split

# Test Split
client.indices.create(
index='books',
body={'settings': {
'index': {'number_of_shards': 5, 'number_of_routing_shards': 30, 'blocks': {'write': True}}}}
index="books",
body={
"settings": {
"index": {
"number_of_shards": 5,
"number_of_routing_shards": 30,
"blocks": {"write": True},
}
}
},
)
print("'books' index created!")
time.sleep(2) # add sleep to ensure the index has time to become read-only
client.indices.split(
index='books',
target='bigger_books',
body={'settings': {'index': {'number_of_shards': 10 }}}
index="books",
target="bigger_books",
body={"settings": {"index": {"number_of_shards": 10}}},
)
print("'books' index split into 'bigger_books'!")
client.indices.put_settings(index='books', body={'index': {'blocks': {'write': False}}})
client.indices.put_settings(
index="books", body={"index": {"blocks": {"write": False}}}
)
print("Write operations enabled for 'books' index!")

# Cleanup
client.indices.delete(index=['movies', 'books', 'movies_clone', 'bigger_books'])
client.indices.delete(index=["movies", "books", "movies_clone", "bigger_books"])
print("All indices deleted!")




if __name__ == "__main__":
test_opensearch_examples()
test_opensearch_examples()
63 changes: 31 additions & 32 deletions samples/aws/search-requests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python

# -*- coding: utf-8 -*-
# SPDX-License-Identifier: Apache-2.0
#
Expand All @@ -9,62 +11,59 @@
# GitHub history for details.

import logging

from os import environ
from time import sleep
from urllib.parse import urlparse

from boto3 import Session
from opensearchpy import RequestsAWSV4SignerAuth, OpenSearch, RequestsHttpConnection

from opensearchpy import OpenSearch, RequestsAWSV4SignerAuth, RequestsHttpConnection

# verbose logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)

# cluster endpoint, for example: my-test-domain.us-east-1.es.amazonaws.com
url = urlparse(environ['ENDPOINT'])
region = environ.get('AWS_REGION', 'us-east-1')
service = environ.get('SERVICE', 'es')
url = urlparse(environ["ENDPOINT"])
region = environ.get("AWS_REGION", "us-east-1")
service = environ.get("SERVICE", "es")

credentials = Session().get_credentials()

auth = RequestsAWSV4SignerAuth(credentials, region, service)

client = OpenSearch(
hosts=[{
'host': url.netloc,
'port': url.port or 443
}],
http_auth=auth,
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection,
timeout=30
hosts=[{"host": url.netloc, "port": url.port or 443}],
http_auth=auth,
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection,
timeout=30,
)

# TODO: remove when OpenSearch Serverless adds support for /
if service == 'es':
info = client.info()
print(f"{info['version']['distribution']}: {info['version']['number']}")
if service == "es":
info = client.info()
print(f"{info['version']['distribution']}: {info['version']['number']}")

# create an index
index = 'movies'
index = "movies"
client.indices.create(index=index)

try:
# index data
document = {'director': 'Bennett Miller', 'title': 'Moneyball', 'year': 2011}
client.index(index=index, body=document, id='1')
# index data
document = {"director": "Bennett Miller", "title": "Moneyball", "year": 2011}
client.index(index=index, body=document, id="1")

# wait for the document to index
sleep(1)
# wait for the document to index
sleep(1)

# search for the document
results = client.search(body={'query': {'match': {'director': 'miller'}}})
for hit in results['hits']['hits']:
print(hit['_source'])
# search for the document
results = client.search(body={"query": {"match": {"director": "miller"}}})
for hit in results["hits"]["hits"]:
print(hit["_source"])

# delete the document
client.delete(index=index, id='1')
# delete the document
client.delete(index=index, id="1")
finally:
# delete the index
client.indices.delete(index=index)
# delete the index
client.indices.delete(index=index)
Loading

0 comments on commit bcfef11

Please sign in to comment.