Skip to content

Commit

Permalink
Added samples for nox format.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Oct 26, 2023
1 parent 0da60b2 commit 68c89de
Show file tree
Hide file tree
Showing 22 changed files with 703 additions and 727 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` 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
1 change: 1 addition & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"opensearchpy/",
"test_opensearchpy/",
"utils/",
"samples/",
)


Expand Down
91 changes: 56 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,16 @@
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 +20,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()
62 changes: 30 additions & 32 deletions samples/aws/search-requests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: Apache-2.0
#
Expand All @@ -9,62 +10,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)
62 changes: 30 additions & 32 deletions samples/aws/search-urllib3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: Apache-2.0
#
Expand All @@ -9,62 +10,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 Urllib3AWSV4SignerAuth, OpenSearch, Urllib3HttpConnection

from opensearchpy import OpenSearch, Urllib3AWSV4SignerAuth, Urllib3HttpConnection

# 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 = Urllib3AWSV4SignerAuth(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=Urllib3HttpConnection,
timeout=30
hosts=[{"host": url.netloc, "port": url.port or 443}],
http_auth=auth,
use_ssl=True,
verify_certs=True,
connection_class=Urllib3HttpConnection,
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 68c89de

Please sign in to comment.