本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
將個人化 Amazon OpenSearch 服務結果與結果進行比較,
Amazon Personalize 化搜索排名插件根據來自 Amazon Personalize 化的排名和排名重新排列搜索結果。 OpenSearch外掛程式重新排名結果的方式取決於您在管道中設定personalized_search_ranking
回應處理器的方式。
若要瞭解結果的排名方式,您可以使用或不使用個人化來執行查詢,並比較結果。您可以使用下面的 Python 代碼來運行兩個不同的查詢,並將結果輸出到兩個JSON文件。第一種方法運行使用插件重新排名結果的查詢。第二個執行的方法會產生結果,而不需要個人化。
import json import requests from requests_auth_aws_sigv4 import AWSSigV4 # Returns re-ranked OpenSearch results using the Amazon Personalize Search Ranking plugin. def get_personalized_results(pipeline_name): url = f'{domain}/{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": "1" } } } try: response = requests.post(url, auth=auth, params=params, json=body, headers=headers, verify=False) except Exception as e: return f"Error: {e}" return response.text # Returns OpenSearch results without personalization. def get_opensearch_results(): url = f'{domain}/{index}/_search/' auth = AWSSigV4('es') headers = {'Content-Type': 'application/json'} body = { "query": { "multi_match": { "query": "Toyota", "fields": ["BRAND"] } } } try: response = requests.post(url, auth=auth, json=body, headers=headers, verify=False) except Exception as e: return f"Error: {e}" return response.text def print_results(file_name, results): results_file = open(file_name, 'w') results_file.write(json.dumps(results, indent=4)) results_file.close() # specify domain endpoint domain = "
DOMAIN_ENDPOINT
" # specify the region where you created your Amazon Personalize resources and Amazon OpenSearch domain aws_region = "REGION
" # specify the name of the pipeline that uses the Amazon Personalize plugin pipeline_name = "PIPELINE_NAME
" # specify your Amazon OpenSearch index index = "INDEX
" # specify names for json files for comparison personalized_results_file = "personalized_results.json" opensearch_results_file = "opensearch_results.json" # get personalized results personalized_results = json.loads(get_personalized_results(pipeline_name)) # get OpenSearch results without personalization opensearch_results = json.loads(get_opensearch_results()) # print results to files print_results(personalized_results_file, personalized_results) print_results(opensearch_results_file, opensearch_results)