選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

Amazon Kendra 自我管理 OpenSearch 的智慧型排名

焦點模式
Amazon Kendra 自我管理 OpenSearch 的智慧型排名 - Amazon Kendra

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

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

您可以利用 Amazon Kendra語意搜尋功能來改善 OpenSearch 的搜尋結果,這是以 Apache 2.0 授權為基礎的自我管理開放原始碼搜尋服務。Intelligent Ranking Amazon Kendra 外掛程式使用 以模擬方式重新排序 OpenSearch 的結果 Amazon Kendra。它透過從預設 OpenSearch 搜尋結果中了解使用特定欄位搜尋查詢的含義和內容,例如文件內文或標題。

例如,以此查詢為例:「主要主題演講地址」。由於「地址」有數個意義,因此 Amazon Kendra 可以推斷查詢背後的意義,以傳回與預期意義一致的相關資訊。在這種情況下,它是會議主題演講地址。例如,更簡單的搜尋服務可能不會考慮意圖,並且可能會傳回 Main Street 上街道地址的結果。

OpenSearch 的 Intelligent Ranking 外掛程式適用於 OpenSearch (自我管理) 2.4.0 版及更新版本。您可以使用快速入門 Bash 指令碼安裝外掛程式,以使用隨附的 Intelligent Ranking 外掛程式建置 OpenSearch 的新 Docker 映像。請參閱設定智慧型搜尋外掛程式:這是讓您快速啟動和執行的設定範例。

智慧型搜尋外掛程式的運作方式

適用於 OpenSearch 的 Intelligent Ranking 外掛程式 (自我管理) 的整體程序如下:

  1. OpenSearch 使用者發出查詢,而 OpenSearch 會提供查詢回應或與查詢相關的文件清單。

  2. Intelligent Ranking 外掛程式會取得查詢回應,並從文件中擷取資訊。

  3. Intelligent Ranking 外掛程式會呼叫 Amazon Kendra Intelligent Ranking 的 Rescore API。

  4. Rescore API 會從文件中擷取資訊,並以語意方式重新排序搜尋結果。

  5. Rescore API 會將重新排序的搜尋結果傳回至外掛程式。外掛程式會重新排列 OpenSearch 搜尋回應中的搜尋結果,以反映新的語意排名。

Intelligent Ranking 外掛程式會使用「body」和「title」欄位來重新排序結果。這些外掛程式欄位可以映射到 OpenSearch 索引中最符合文件內文和標題定義的欄位。例如,如果您的索引包含具有 "chapter_heading" 和 "chapter_contents" 等欄位的書籍章節,您可以將前者對應至 "title",後者對應至 "body",以取得最佳結果。

設定智慧型搜尋外掛程式

以下概述如何使用 Intelligent Ranking 外掛程式快速設定 OpenSearch (自我管理)。

使用 Intelligent Ranking 外掛程式 (快速設定) 設定 OpenSearch (自我管理)

如果您已使用 Docker 映像 opensearch:2.4.0,您可以使用此 Dockerfile 搭配 Intelligent Ranking 外掛程式來建置 OpenSearch 2.4.0 的新映像。您可以在 docker-compose.yml 檔案或 opensearch.yml 檔案中包含新映像的容器。您也可以包含從建立 rescore 執行計畫所產生的 rescore 執行計畫 ID,以及您的區域和端點資訊,請參閱步驟 2 來建立 rescore 執行計畫。

如果您先前下載的 Docker opensearch 映像版本早於 2.4.0,則必須使用 Docker 映像opensearch:2.4.0或更新版本,並使用隨附的 Intelligent Ranking 外掛程式建置新的映像。

  1. 為您的作業系統下載並安裝 Docker Desktop。Docker Desktop 包含 Docker Compose 和 Docker Engine。建議您檢查電腦是否符合 Docker 安裝詳細資訊中所述的系統需求。

    您也可以在 Docker 桌面的設定中增加記憶體用量需求。您需負責 Docker 的使用需求,超出 Docker 服務的免費可用使用限制。請參閱 Docker 訂閱

    檢查 Docker 桌面狀態為「執行中」。

  2. 佈建 Amazon Kendra 智慧排名和您的容量需求。佈建 Amazon Kendra Intelligent Ranking 後,會根據您設定的容量單位,每小時向您收費。請參閱免費方案和定價資訊

    您可以使用 CreateRescoreExecutionPlan API 來佈建 Rescore API。如果您不需要比單一單位預設值更多的容量單位,請不要新增更多單位,並只為 rescore 執行計畫提供名稱。您也可以使用 UpdateRescoreExecutionPlan API 來更新您的容量需求。如需詳細資訊,請參閱對搜尋服務的結果進行邏輯排序

    或者,您也可以前往步驟 3,在執行快速啟動 Bash 指令碼時建立預設的重新評分執行計畫。

    記下步驟 4 的重新評分執行計畫 ID,包含在回應中。

    CLI
    aws kendra-ranking create-rescore-execution-plan \ --name MyRescoreExecutionPlan \ --capacity-units '{"RescoreCapacityUnits":<integer number of additional capacity units>}' Response: { "Id": "<rescore execution plan ID>", "Arn": "arn:aws:kendra-ranking:<region>:<account-id>:rescore-execution-plan/<rescore-execution-plan-id>" }
    Python
    import boto3 from botocore.exceptions import ClientError import pprint import time kendra_ranking = boto3.client("kendra-ranking") print("Create a rescore execution plan.") # Provide a name for the rescore execution plan name = "MyRescoreExecutionPlan" # Set your required additional capacity units # Don't set capacity units if you don't require more than 1 unit given by default capacity_units = 1 try: rescore_execution_plan_response = kendra_ranking.create_rescore_execution_plan( Name = name, CapacityUnits = {"RescoreCapacityUnits":capacity_units} ) pprint.pprint(rescore_execution_plan_response) rescore_execution_plan_id = rescore_execution_plan_response["Id"] print("Wait for Amazon Kendra to create the rescore execution plan.") while True: # Get the details of the rescore execution plan, such as the status rescore_execution_plan_description = kendra_ranking.describe_rescore_execution_plan( Id = rescore_execution_plan_id ) # When status is not CREATING quit. status = rescore_execution_plan_description["Status"] print(" Creating rescore execution plan. Status: "+status) time.sleep(60) if status != "CREATING": break except ClientError as e: print("%s" % e) print("Program ends.")
    aws kendra-ranking create-rescore-execution-plan \ --name MyRescoreExecutionPlan \ --capacity-units '{"RescoreCapacityUnits":<integer number of additional capacity units>}' Response: { "Id": "<rescore execution plan ID>", "Arn": "arn:aws:kendra-ranking:<region>:<account-id>:rescore-execution-plan/<rescore-execution-plan-id>" }
  3. 從主分支下拉式清單中選取版本分支,以從 GitHub 下載適用於您 OpenSearch 版本的快速入門 Bash 指令碼

    此指令碼會使用您在 GitHub 儲存庫上為指令碼選取的版本,針對 OpenSearch 和 OpenSearch Dashboards 使用 Docker 映像。它會下載 Intelligent Ranking 外掛程式的 zip 檔案,並產生 Dockerfile來建置包含外掛程式的 OpenSearch 新 Docker 映像。它也會建立 docker-compose.yml 檔案,其中包含具有 Intelligent Ranking 外掛程式和 OpenSearch Dashboards 的 OpenSearch 容器。指令碼會將您的 rescore 執行計畫 ID、區域資訊和端點 (使用區域) 新增至 docker-compose.yml 檔案。然後,指令碼會執行 docker-compose up 來啟動包含智慧型排名的 OpenSearch 容器和 OpenSearch Dashboards。若要停止容器而不將其移除,請執行 docker-compose stop。若要移除容器,請執行 docker-compose down

  4. 開啟終端機,並在 Bash 指令碼的 目錄中執行下列命令。

    bash search_processing_kendra_quickstart.sh -p <execution-plan-id> -r <region>

    當您執行此命令時,請提供您在佈建 Amazon Kendra Intelligent Ranking 時在步驟 2 中記下的 rescore 執行計畫 ID,以及您的區域資訊。或者,您可以使用 --create-execution-plan選項來佈建 Amazon Kendra 智慧排名。這會建立具有預設名稱和預設容量的重新評分執行計劃。

    若要在移除預設暫時性容器時不會遺失索引,您可以使用 --volume-name選項提供資料磁碟區名稱,讓索引在執行期間持續存在。如果您先前已建立索引,您可以在 docker-compose.yml 或 opensearch.yml 檔案中指定磁碟區。若要保持磁碟區完整,請勿執行 docker-compose down -v

    快速入門 Bash 指令碼會在 OpenSearch 金鑰存放區中設定您的 AWS 登入資料,以連線至 Amazon Kendra Intelligent Ranking。若要將 AWS 登入資料提供給指令碼,請使用 --profile選項來指定 AWS 設定檔。如果未指定 --profile選項,則快速啟動 Bash 指令碼會嘗試從環境變數讀取 AWS 登入資料 (存取/私密金鑰、選用工作階段字符),然後從預設 AWS 設定檔讀取登入資料。如果未指定 --profile選項,但找不到登入資料,則指令碼不會將登入資料傳遞至 OpenSearch 金鑰存放區。如果 OpenSearch 金鑰存放區中未指定登入資料,則外掛程式仍會檢查預設登入資料提供者鏈中的登入資料,包括透過 Amazon EC2 中繼資料服務交付的 Amazon ECS 容器登入資料或執行個體設定檔登入資料。

    請確定您已建立具有呼叫 Amazon Kendra Intelligent Ranking 必要許可 IAM 的角色。以下是 IAM 政策範例,以授予特定 rescore 執行計畫使用 Rescore API 的許可:

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "kendra-ranking:Rescore", "Resource": "arn:aws:kendra-ranking:${Region}:${Account}:rescore-execution-plan/${RescoreExecutionPlanId}" } ] }

docker-compose.yml 的範例

使用 OpenSearch 2.4.0 或更新版本搭配 Intelligent Ranking 外掛程式和 OpenSearch Dashboards 2.4.0 或更新版本的 docker-compose.yml 檔案範例。

version: '3' networks: opensearch-net: volumes: <volume-name>: services: opensearch-node: image: <Docker image tag name of OpenSearch with Intelligent Ranking plugin> container_name: opensearch-node environment: - cluster.name=opensearch-cluster - node.name=opensearch-node - discovery.type=single-node - kendra_intelligent_ranking.service.endpoint=https://kendra-ranking.<region>.api.aws - kendra_intelligent_ranking.service.region=<region> - kendra_intelligent_ranking.service.execution_plan_id=<rescore-execution-plan-id> ulimits: memlock: soft: -1 hard: -1 nofile: soft: 65536 hard: 65536 ports: - 9200:9200 - 9600:9600 networks: - opensearch-net volumes: <docker-volume-name>:/usr/share/opensearch/data opensearch-dashboard: image: opensearchproject/opensearch-dashboards:<your-version> container_name: opensearch-dashboards ports: - 5601:5601 environment: OPENSEARCH_HOSTS: '["https://opensearch-node:9200"]' networks: - opensearch-net

Dockerfile 和建置映像的範例

Dockerfile 使用 OpenSearch 2.4.0 或更新版本搭配 Intelligent Ranking 外掛程式的範例。

FROM opensearchproject/opensearch:<your-version> RUN /usr/share/opensearch/bin/opensearch-plugin install --batch https://github.com/opensearch-project/search-processor/releases/download/<your-version>/search-processor.zip

使用 Intelligent Ranking 外掛程式為 OpenSearch 建置 Docker 映像。

docker build --tag=<Docker image tag name of OpenSearch with Intelligent Ranking plugin>

與智慧型搜尋外掛程式互動

使用 Intelligent Ranking 外掛程式設定 OpenSearch (自我管理) 後,您可以使用 curl 命令或 OpenSearch 用戶端程式庫與外掛程式互動。使用 Intelligent Ranking 外掛程式存取 OpenSearch 的預設登入資料為使用者名稱 'admin' 和密碼 'admin'。

若要將 Intelligent Ranking 外掛程式設定套用至 OpenSearch 索引:

Curl
curl -XPUT "https://localhost:9200/<your-docs-index>/_settings" -u 'admin:admin' --insecure -H 'Content-Type: application/json' -d' { "index": { "plugin" : { "searchrelevance" : { "result_transformer" : { "kendra_intelligent_ranking": { "order": 1, "properties": { "title_field": "title_field_name_here", "body_field": "body_field_name_here" } } } } } } } '
Python
pip install opensearch-py from opensearchpy import OpenSearch host = 'localhost' port = 9200 auth = ('admin', 'admin') client = OpenSearch( hosts = [{'host': host, 'port': port}], http_compress = True, # enables gzip compression for request bodies http_auth = auth, # client_cert = client_cert_path, # client_key = client_key_path, use_ssl = True, verify_certs = False, ssl_assert_hostname = False, ssl_show_warn = False, ca_certs = ca_certs_path ) setting_body = { "index": { "plugin" : { "searchrelevance" : { "result_transformer" : { "kendra_intelligent_ranking": { "order": 1, "properties": { "title_field": "title_field_name_here", "body_field": "body_field_name_here" } } } } } } } response = client.indices.put_settings(index_name, body=setting_body)
curl -XPUT "https://localhost:9200/<your-docs-index>/_settings" -u 'admin:admin' --insecure -H 'Content-Type: application/json' -d' { "index": { "plugin" : { "searchrelevance" : { "result_transformer" : { "kendra_intelligent_ranking": { "order": 1, "properties": { "title_field": "title_field_name_here", "body_field": "body_field_name_here" } } } } } } } '

您必須包含要用來重新排序的主要文字欄位名稱,例如文件內文或文件內容欄位。您也可以包含其他文字欄位,例如文件標題或文件摘要。

現在,您可以發出任何查詢,並使用 Intelligent Ranking 外掛程式對結果進行排名。

Curl
curl -XGET "https://localhost:9200/<your-docs-index>/_search?pretty" -u 'admin:admin' --insecure -H 'Content-Type: application/json' -d' { "query" : { "match" : { "body_field_name_here": "intelligent systems" } } } '
Python
from opensearchpy import OpenSearch host = 'localhost' port = 9200 auth = ('admin', 'admin') client = OpenSearch( hosts = [{'host': host, 'port': port}], http_compress = True, # enables gzip compression for request bodies http_auth = auth, # client_cert = client_cert_path, # client_key = client_key_path, use_ssl = True, verify_certs = False, ssl_assert_hostname = False, ssl_show_warn = False, ca_certs = ca_certs_path ) query = { 'size': 10, "query" : { "match" : { "body_field_name_here": "intelligent systems" } } } response = client.search( body = query, index = index_name ) print('\nSearch results:') print(response)
curl -XGET "https://localhost:9200/<your-docs-index>/_search?pretty" -u 'admin:admin' --insecure -H 'Content-Type: application/json' -d' { "query" : { "match" : { "body_field_name_here": "intelligent systems" } } } '

若要移除 OpenSearch 索引的 Intelligent Ranking 外掛程式設定:

Curl
curl -XPUT "http://localhost:9200/<your-docs-index>/_settings" -H 'Content-Type: application/json' -d' { "index": { "plugin": { "searchrelevance": { "result_transformer": { "kendra_intelligent_ranking.*": null } } } } } '
Python
from opensearchpy import OpenSearch host = 'localhost' port = 9200 auth = ('admin', 'admin') client = OpenSearch( hosts = [{'host': host, 'port': port}], http_compress = True, # enables gzip compression for request bodies http_auth = auth, # client_cert = client_cert_path, # client_key = client_key_path, use_ssl = True, verify_certs = False, ssl_assert_hostname = False, ssl_show_warn = False, ca_certs = ca_certs_path ) setting_body = { "index": { "plugin": { "searchrelevance": { "result_transformer": { "kendra_intelligent_ranking.*": null } } } } } response = client.indices.put_settings(index_name, body=setting_body)
curl -XPUT "http://localhost:9200/<your-docs-index>/_settings" -H 'Content-Type: application/json' -d' { "index": { "plugin": { "searchrelevance": { "result_transformer": { "kendra_intelligent_ranking.*": null } } } } } '

若要在特定查詢上測試 Intelligent Ranking 外掛程式,或在特定內文和標題欄位上測試:

Curl
curl -XGET "https://localhost:9200/<your-docs-index>/_search?pretty" -u 'admin:admin' --insecure -H 'Content-Type: application/json' -d' { "query": { "multi-match": { "query": "intelligent systems", "fields": ["body_field_name_here", "title_field_name_here"] } }, "size": 25, "ext": { "search_configuration": { "result_transformer": { "kendra_intelligent_ranking": { "order": 1, "properties": { "title_field": "title_field_name_here", "body_field": "body_field_name_here" } } } } } } '
Python
from opensearchpy import OpenSearch host = 'localhost' port = 9200 auth = ('admin', 'admin') client = OpenSearch( hosts = [{'host': host, 'port': port}], http_compress = True, # enables gzip compression for request bodies http_auth = auth, # client_cert = client_cert_path, # client_key = client_key_path, use_ssl = True, verify_certs = False, ssl_assert_hostname = False, ssl_show_warn = False, ca_certs = ca_certs_path ) # Index settings null for kendra_intelligent_ranking query = { "query": { "multi_match": { "query": "intelligent systems", "fields": ["body_field_name_here", "title_field_name_here"] } }, "size": 25, "ext": { "search_configuration": { "result_transformer": { "kendra_intelligent_ranking": { "order": 1, "properties": { "title_field": "title_field_name_here", "body_field": "body_field_name_here" } } } } } } response = client.search( body = query, index = index_name ) print('\nSearch results:') print(response)
curl -XGET "https://localhost:9200/<your-docs-index>/_search?pretty" -u 'admin:admin' --insecure -H 'Content-Type: application/json' -d' { "query": { "multi-match": { "query": "intelligent systems", "fields": ["body_field_name_here", "title_field_name_here"] } }, "size": 25, "ext": { "search_configuration": { "result_transformer": { "kendra_intelligent_ranking": { "order": 1, "properties": { "title_field": "title_field_name_here", "body_field": "body_field_name_here" } } } } } } '

比較 OpenSearch 結果與 Amazon Kendra 結果

您可以將side-by-side OpenSearch (自我管理) 排名結果與 Amazon Kendra重新排名的結果進行比較。OpenSearch Dashboards 2.4.0 版和更新版本提供side-by-side的結果,以便您可以比較 OpenSearch 如何對文件進行排名,以及 Amazon Kendra 或外掛程式如何對文件進行排名以進行搜尋查詢。

在您可以將 OpenSearch 排名結果與 Amazon Kendra 重新排名的結果進行比較之前,請確定您的 OpenSearch Dashboards 由 OpenSearch 伺服器與 Intelligent Ranking 外掛程式提供支援。您可以使用 Docker 和快速啟動 Bash 指令碼來設定。請參閱 設定智慧型搜尋外掛程式

以下概述如何在 OpenSearch Dashboards 中比較 OpenSearch 和 Amazon Kendra 搜尋結果。如需詳細資訊,請參閱 OpenSearch 文件

在 OpenSearch Dashboards 中比較搜尋結果
  1. 開啟 http://localhost:5601 並登入 OpenSearch Dashboards。預設登入資料是使用者名稱 'admin' 和密碼 'admin'。

  2. 從導覽功能表中的 OpenSearch 外掛程式選取搜尋關聯

  3. 在搜尋列中輸入搜尋文字。

  4. 選取查詢 1 的索引,然後在 OpenSearch Query DSL 中輸入查詢。您可以使用 %SearchText%變數來參考您在搜尋列中輸入的搜尋文字。如需此查詢的範例,請參閱 OpenSearch 文件。此查詢傳回的結果是未使用 Intelligent Ranking 外掛程式的 OpenSearch 結果。

  5. 選取查詢 2 的相同索引,然後在 OpenSearch Query DSL 中輸入相同的查詢。此外,使用 加入 延伸模組,kendra_intelligent_ranking並指定body_field要排名的強制項目。您也可以指定標題欄位,但內文欄位為必要欄位。如需此查詢的範例,請參閱 OpenSearch 文件。此查詢傳回的結果是使用 Intelligent Ranking 外掛程式 Amazon Kendra 重新排序的結果。外掛程式最多可排名 25 個結果。

  6. 選取搜尋以傳回和比較結果。

隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。