generated from nationalarchives/da-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from nationalarchives/AYR-421/flask-app-search…
…-integration Ayr 421/flask app search integration
- Loading branch information
Showing
20 changed files
with
907 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,4 @@ repos: | |
rev: 23.9.1 | ||
hooks: | ||
- id: black | ||
language_version: python3.11 | ||
language_version: python3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from typing import Tuple | ||
|
||
import boto3 | ||
from opensearchpy import AWSV4SignerAuth, OpenSearch | ||
|
||
from app.main.aws.parameter import ( | ||
get_aws_environment_prefix, | ||
get_parameter_store_key_value, | ||
) | ||
|
||
|
||
def get_open_search_index_from_aws_params() -> str: | ||
return get_parameter_store_key_value( | ||
get_aws_environment_prefix() + "AWS_OPEN_SEARCH_INDEX" | ||
) | ||
|
||
|
||
def generate_open_search_client_from_aws_params() -> OpenSearch: | ||
host = get_parameter_store_key_value( | ||
get_aws_environment_prefix() + "AWS_OPEN_SEARCH_HOST" | ||
) | ||
http_auth = _get_open_search_http_auth() | ||
|
||
open_search_client = OpenSearch( | ||
hosts=[{"host": host, "port": 443}], | ||
http_auth=http_auth, | ||
use_ssl=True, | ||
verify_certs=True, | ||
http_compress=True, | ||
ssl_assert_hostname=False, | ||
ssl_show_warn=True, | ||
) | ||
return open_search_client | ||
|
||
|
||
def _get_open_search_http_auth( | ||
auth_method: str = "username_password", | ||
) -> Tuple[str, str] | AWSV4SignerAuth: | ||
if auth_method == "username_password": | ||
return _get_open_search_username_password_auth() | ||
return _get_open_search_iam_auth() | ||
|
||
|
||
def _get_open_search_username_password_auth() -> Tuple[str, str]: | ||
username = get_parameter_store_key_value( | ||
get_aws_environment_prefix() + "AWS_OPEN_SEARCH_USERNAME" | ||
) | ||
password = get_parameter_store_key_value( | ||
get_aws_environment_prefix() + "AWS_OPEN_SEARCH_PASSWORD" | ||
) | ||
return (username, password) | ||
|
||
|
||
def _get_open_search_iam_auth() -> AWSV4SignerAuth: | ||
credentials = boto3.Session().get_credentials() | ||
aws_region = get_parameter_store_key_value( | ||
get_aws_environment_prefix() + "AWS_REGION" | ||
) | ||
service = "es" | ||
aws_auth = AWSV4SignerAuth(credentials, aws_region, service) | ||
return aws_auth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import boto3 | ||
from botocore.exceptions import ClientError | ||
import logging | ||
|
||
|
||
def get_aws_environment_prefix() -> str: | ||
environment_name = get_parameter_store_key_value("ENVIRONMENT_NAME") | ||
print(environment_name) | ||
return "/" + environment_name + "/" | ||
|
||
|
||
def get_parameter_store_key_value(key: str) -> str: | ||
""" | ||
Get string value of `key` in Parameter Store. | ||
:param key: Name of key whose value will be returned. | ||
:return: String value of requested Parameter Store key. | ||
""" | ||
ssm_client = boto3.client("ssm") | ||
parameter_value = "" | ||
try: | ||
parameter_value = ssm_client.get_parameter(Name=key)["Parameter"]["Value"] | ||
logging.info("Parameter value retrieved successfully") | ||
except ClientError as e: | ||
logging.error( | ||
"Failed to get parameter value, Error : %s", e.response["Error"]["Code"] | ||
) | ||
return parameter_value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import logging | ||
from typing import Any | ||
|
||
from opensearchpy import ImproperlyConfigured | ||
from app.main.aws.open_search import ( | ||
generate_open_search_client_from_aws_params, | ||
get_open_search_index_from_aws_params, | ||
) | ||
|
||
|
||
def generate_open_search_client_and_make_poc_search(query: str) -> Any: | ||
fields = [ | ||
"legal_status", | ||
"description", | ||
"closure_type", | ||
"Internal-Sender_Identifier", | ||
"id", | ||
"Contact_Email", | ||
"Source_Organization", | ||
"Consignment_Series.keyword", | ||
"Consignment_Series", | ||
"Contact_Name", | ||
] | ||
open_search_client = generate_open_search_client_from_aws_params() | ||
|
||
try: | ||
open_search_client.ping() | ||
except ImproperlyConfigured as e: | ||
logging.error("OpenSearch client improperly configured: " + str(e)) | ||
raise e | ||
|
||
logging.info("OpenSearch client has been connected successfully") | ||
|
||
open_search_index = get_open_search_index_from_aws_params() | ||
open_search_query = { | ||
"query": { | ||
"multi_match": { | ||
"query": query, | ||
"fields": fields, | ||
"fuzziness": "AUTO", | ||
"type": "best_fields", | ||
} | ||
} | ||
} | ||
search_results = open_search_client.search( | ||
body=open_search_query, index=open_search_index | ||
) | ||
return search_results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.