Introduction#
Github this shows how to setup opensearch serverless to build a note bookmark application. Frontend updated later on.
- setup aws opensearch
- interact via curl
- using python sdk
- basic dsl language
Architecture#
Configure Permission#
from opensearch aws console, need grant permission to user, role to access collection and index by polcies, example
[{"Rules": [{"Resource": ["collection/test"],"Permission": ["aoss:*"],"ResourceType": "collection"},{"Resource": ["index/test/*"],"Permission": ["aoss:*"],"ResourceType": "index"}],"Principal": ["arn:aws:iam::$ACCOUNT_ID:role/RoleForLambdaIndexOpenSearch"],"Description": "lambdaindexopensearch"}]
OpenSearch Client#
auth and create a client
# opensearch domainif "OPENSEARCH_DOMAIN" in os.environ:passelse:os.environ["OPENSEARCH_DOMAIN"] = ""os.environ["REGION"] = "us-east-1"# host and opensearch clienthost = os.environ["OPENSEARCH_DOMAIN"]client = boto3.client("opensearchserverless")service = "aoss"region = os.environ["REGION"]credentials = boto3.Session().get_credentials()# authawsauth = AWS4Auth(credentials.access_key,credentials.secret_key,region,service,session_token=credentials.token,)
create a client
client = OpenSearch(hosts=[{"host": host, "port": 443}],http_auth=awsauth,use_ssl=True,verify_certs=True,connection_class=RequestsHttpConnection,timeout=300,)
Basic Operation#
create an index
resp = client.indices.create(index_name)
delete an index
resp = client.indices.delete(index=index_name)
index a document
def test_index_data(index_name):"""test index a document into the opensearch serverless"""#response = client.index(index=index_name,# body={"title": "pricing", "creator": "Larry David", "year": 1989},body={"DocumentTitle": "pricing","DocumentExcerpt": "100","DocumentURI":"URL"},id=str(uuid.uuid4()),)print(response)
query data
def test_query_index(query):"""test query from opensearch serverless"""response = client.search(index="cdk-entest", body={"query": {"query_string": {"query": query}}})print(response)
or test another query
def test_query(query_request):"""query opensearch"""resp = client.search(index="cdk-entest",body={"query": {"multi_match": {"query": query_request,"fields": ["DocumentTitle","DocumentExcerpt","DocumentURI",],}}},)print(resp)
delete a document given the document id
client.delete(index=index_name, id=id)
Troubleshooting#
update boto3
python3 -m pip install boto3 --upgrade