

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 將外掛程式套用至 Amazon OpenSearch Service 查詢
<a name="managed-apply-plugin"></a>

[建立管道](managed-opensearch-plugin-pipeline-example.md)之後，您就可以將 Amazon Personalize Search Ranking 外掛程式套用至查詢。您可以將 Amazon Personalize Search Ranking 外掛程式套用至索引的所有查詢和回應。您也可以將外掛程式套用至個別的查詢和回應。
+  您可以使用下列 Python 程式碼，將搜尋管道套用至索引。使用此方法，使用此索引的所有搜尋都會使用 外掛程式將個人化套用至搜尋結果。

  ```
  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}")
  ```
+ 您可以使用下列 Python 程式碼，將搜尋管道套用至 Toyota 品牌車的個別查詢。

  更新程式碼以指定您的網域端點、OpenSearch Service 索引、管道名稱和查詢。針對 `user_id`，指定您要取得搜尋結果的使用者 ID。此使用者必須位於您用來建立 Amazon Personalize 解決方案版本的資料中。如果使用者不存在，Amazon Personalize 會根據其熱門程度對項目進行排名。

  對於 `context`，如果您使用內容中繼資料，請提供使用者的內容中繼資料，例如其裝置類型。此 `context` 欄位為選用。如需詳細資訊，請參閱[提高與內容中繼資料的建議相關性](contextual-metadata.md)。

  ```
  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}")
  ```