Applying the plugin to OpenSearch queries - Amazon Personalize

Applying the plugin to OpenSearch queries

After you configure a search pipeline with a personalized_search_ranking response processor, you're ready to apply the Amazon Personalize Search Ranking plugin to your OpenSearch queries and view the re-ranked results.

As you apply the plugin to OpenSearch queries, you can monitor the plugin by getting metrics for your search pipeline. For more information, see Monitoring the plugin.

Applying the plugin to Amazon OpenSearch Service queries

You can apply the Amazon Personalize Search Ranking plugin to all queries and responses for an index. You can also apply the plugin to individual queries and responses.

  • You can use the following Python code to apply a search pipeline to an index. With this approach, all searches using this index use the plugin to apply personalization to search results.

    import requests from requests_auth_aws_sigv4 import AWSSigV4 domain_endpoint = 'domain endpoint' index = 'index name' url = f'{domain_endpoint}/{index}/_settings/' auth = AWSSigV4('es') headers = {'Content-Type': 'application/json'} body = { "index.search.default_pipeline": "pipeline name" } try: response = requests.put(url, auth=auth, json=body, headers=headers) print(response.text) except Exception as e: print(f"Error: {e}")
  • You can use the following Python code to apply a search pipeline to an individual query for Toyota brand cars.

    Update the code to specify your domain endpoint, your OpenSearch Service index, the name of your pipeline, and your query. For user_id, specify the ID of the user that you're getting search results for. This user must be in the data that you used to create your Amazon Personalize solution version. If the user wasn't present, Amazon Personalize ranks the items based on their popularity.

    For context, if you use contextual metadata, provide the user's contextual metadata, such as their device type. The context field is optional. For more information, see Increasing recommendation relevance with contextual metadata.

    import requests from requests_auth_aws_sigv4 import AWSSigV4 domain_endpoint = 'domain endpoint' index = 'index name' url = f'{domain_endpoint}/{index}/_search/' auth = AWSSigV4('es') headers = {'Content-Type': 'application/json'} params = {"search_pipeline": "pipeline-name"} body = { "query": { "multi_match": { "query": "Toyota", "fields": ["BRAND"] } }, "ext": { "personalize_request_parameters": { "user_id": "USER ID" "context": { "DEVICE" : "mobile phone" } } } } try: response = requests.post(url, auth=auth, params=params, json=body, headers=headers, verify=False) print(response) except Exception as e: print(f"Error: {e}")

Applying the plugin to queries in open source OpenSearch

You can apply the Amazon Personalize Search Ranking plugin to all queries and responses for an OpenSearch index. You can also apply the plugin to individual OpenSearch queries and responses.

  • The following curl command applies a search pipeline to an OpenSearch index in an open source OpenSearch cluster running locally. With this approach, all searches at this index use the plugin to apply personalization to search results.

    curl -XGET "https://localhost:9200/index/_settings" -ku 'admin:admin' --insecure -H 'Content-Type: application/json' -d' { "index.search.default_pipeline" : "pipeline-name" } '
  • The following curl command applies a search pipeline to an individual query for Toyota brand cars on an index in an open source OpenSearch cluster running locally.

    For user_id, specify the ID of the user that you're getting search results for. This user must be in the data that you used to create your Amazon Personalize solution version. If the user wasn't present, Amazon Personalize ranks the items based on their popularity. For context, if you use contextual metadata, provide the user's contextual metadata, such as their device type. The context field is optional. For more information, see Increasing recommendation relevance with contextual metadata.

    curl -XGET "http://localhost:9200/index/_search?search_pipeline=pipeline-name" -ku 'admin:admin' --insecure -H 'Content-Type: application/json' -d' { "query": { "multi_match": { "query": "Toyota", "fields": ["BRAND"] } }, "ext": { "personalize_request_parameters": { "user_id": "user ID", "context": { "DEVICE" : "mobile phone" } } } } '

To understand how results are re-ranked, you can use OpenSearch Dashboards to compare OpenSearch results against re-ranked results with the plugin. For more information, see Comparing OpenSearch results with results from the plugin.

As you apply the plugin to OpenSearch queries, you can monitor the plugin by getting metrics for your OpenSearch pipeline. For more information, see Monitoring the plugin.