

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

# 查詢建議
<a name="query-suggestions"></a>

**注意**  
功能支援會因索引類型和正在使用的搜尋 API 而有所不同。若要查看您正在使用的索引類型和搜尋 API 是否支援此功能，請參閱[索引類型](https://docs.aws.amazon.com/kendra/latest/dg/hiw-index-types.html)。

Amazon Kendra *查詢建議*可協助您的使用者更快輸入搜尋查詢，並引導其搜尋。

Amazon Kendra 根據下列其中一項建議與使用者相關的查詢：
+ 查詢歷史記錄或查詢日誌中的熱門查詢
+ 文件欄位/屬性的內容

您可以將 設定為 `SuggestionTypes``QUERY`或 `DOCUMENT_ATTRIBUTES`並呼叫 ，以設定使用查詢歷史記錄或文件欄位的偏好設定[https://docs.aws.amazon.com/kendra/latest/APIReference/API_GetQuerySuggestions.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_GetQuerySuggestions.html)。根據預設， Amazon Kendra 會使用查詢歷史記錄來提供建議。如果您呼叫 時同時啟用查詢歷史記錄和文件欄位，[https://docs.aws.amazon.com/kendra/latest/APIReference/API_UpdateQuerySuggestionsConfig.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_UpdateQuerySuggestionsConfig.html)而且您尚未將`SuggestionTypes`偏好設定設定為使用文件欄位，則 Amazon Kendra 會使用查詢歷史記錄。

如果您使用 主控台，您可以根據查詢歷史記錄或文件欄位來提供查詢建議。您首先選取您的索引，然後在導覽功能表中**的擴充**下選取**查詢建議**。然後選取**設定查詢建議**。設定查詢建議後，系統會將您導向搜尋主控台，您可以在右側面板中選取**查詢歷史記錄**或**文件欄位**，然後在搜尋列中輸入搜尋查詢。

根據預設，使用查詢歷史記錄和文件欄位的查詢建議都會啟用，無需額外費用。您可以使用 `UpdateQuerySuggestionsConfig` API 隨時停用這些類型的查詢建議。若要根據查詢歷史記錄停用查詢建議，請在呼叫 `DISABLED`時將 `Mode`設定為 `UpdateQuerySuggestionsConfig`。若要根據文件欄位停用查詢建議，請在文件欄位組態`INACTIVE`中將 `AttributeSuggestionsMode`設定為 ，然後呼叫 `UpdateQuerySuggestionsConfig>`。如果您使用 主控台，您可以在查詢建議**設定中停用查詢建議**。

查詢建議不區分大小寫。 會將查詢字首和建議的查詢 Amazon Kendra 轉換為小寫、忽略所有單引號和雙引號，並以單一空格取代多個空格字元。 Amazon Kendra 會與所有其他特殊字元相同。如果使用者類型少於兩個字元或超過 60 個字元， Amazon Kendra 則不會顯示任何建議。

**Topics**
+ [使用查詢歷史記錄查詢建議](#query-suggestions-history)
+ [使用文件欄位查詢建議](#query-suggestions-doc-fields)
+ [從建議中封鎖特定查詢或文件欄位內容](#query-suggestions-blocklist)

## 使用查詢歷史記錄查詢建議
<a name="query-suggestions-history"></a>

**注意**  
功能支援會因索引類型和正在使用的搜尋 API 而有所不同。若要查看您正在使用的索引類型和搜尋 API 是否支援此功能，請參閱[索引類型](https://docs.aws.amazon.com/kendra/latest/dg/hiw-index-types.html)。

**Topics**
+ [選取建議查詢的設定](#query-suggestions-history-settings)
+ [保留查詢歷史記錄時清除建議](#query-suggestions-history-clear)
+ [沒有可用的建議](#query-suggestions-history-none)

您可以選擇根據查詢歷史記錄或查詢日誌中的熱門查詢來建議與使用者相關的查詢。 Amazon Kendra 會使用使用者搜尋並從這些查詢中學習的所有查詢，以向您的使用者提出建議。當使用者開始輸入查詢時， Amazon Kendra 會建議使用者熱門查詢。如果查詢的字首或前幾個字元符合使用者開始輸入為查詢的內容， Amazon Kendra 則建議查詢。

例如，使用者開始輸入查詢「即將發生的事件」。 Amazon Kendra 已從查詢歷史記錄得知許多使用者多次搜尋「即將發生的事件」。使用者看到「近期事件 2050」直接顯示在搜尋列下方，自動完成其搜尋查詢。使用者選取此查詢建議，搜尋結果中會傳回文件「新事件：2050 年發生的情況」。

您可以指定 如何 Amazon Kendra 選取符合資格的查詢來建議使用者。例如，您可以指定查詢建議必須已由至少 10 個唯一使用者 （預設為 3 個） 搜尋、在過去 30 天內搜尋，而且不包含[區塊清單中](https://docs.aws.amazon.com/kendra/latest/dg/query-suggestions.html#query-suggestions-blocklist)的任何單字或片語。 Amazon Kendra 要求查詢至少有一個搜尋結果，且至少包含一個超過四個字元的單字。

### 選取建議查詢的設定
<a name="query-suggestions-history-settings"></a>

您可以使用 [https://docs.aws.amazon.com/kendra/latest/APIReference/API_UpdateQuerySuggestionsConfig.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_UpdateQuerySuggestionsConfig.html) API 設定下列設定來選取建議查詢：
+ **模式** - 使用查詢歷史記錄的查詢建議為 `ENABLED`或 `LEARN_ONLY`。 Amazon Kendra 預設會啟用查詢建議。 `LEARN_ONLY` 會關閉查詢建議。如果關閉， Amazon Kendra 請繼續學習建議，但不會向使用者提出查詢建議。
+ **查詢日誌時段** - 查詢在查詢日誌時段中的最近時間。時間範圍是從當日到過去天數的整數值。
+ **沒有使用者資訊的查詢** — 設定為 `TRUE` 以包含所有查詢，或設定為 `FALSE` 以僅包含具有使用者資訊的查詢。如果您的搜尋應用程式包含使用者資訊，例如使用者 ID，當使用者發出查詢時，您可以使用此設定。根據預設，如果沒有與查詢相關聯的特定使用者資訊，則此設定不會篩選掉查詢。不過，您可以使用此設定，僅根據包含使用者資訊的查詢提出建議。
+ **唯一使用者** — 必須搜尋查詢才有資格向您的使用者建議的唯一使用者數量下限。此數字是整數值。
+ **查詢計數** - 必須搜尋查詢才有資格向您的使用者建議之查詢的最小次數。此數字是整數值。

這些設定會影響將查詢選取為熱門查詢以向您的使用者提供建議的方式。如何調整設定將取決於您的特定需求，例如：
+ 如果您的使用者通常每月平均搜尋一次，則可以將查詢日誌時段中的天數設定為 30 天。透過使用該設定，您可以在使用者於時段過期之前擷取使用者最近的大多數查詢。
+ 如果只有少量的查詢包含使用者資訊，而且您不想根據較小的樣本大小建議查詢，則可以設定查詢以包含所有使用者。
+ 如果您將熱門查詢定義為由至少 10 個唯一使用者搜尋，且至少搜尋 100 次，則您將唯一使用者設定為 10，而查詢計數為 100。

**警告**  
您對設定所做的變更可能不會立即生效。您可以使用 [https://docs.aws.amazon.com/kendra/latest/APIReference/API_DescribeQuerySuggestionsConfig.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_DescribeQuerySuggestionsConfig.html) API 追蹤設定變更。更新設定生效的時間取決於您所做的更新，以及索引中的搜尋查詢數量。 Amazon Kendra 會在您變更設定後或套用[封鎖清單](https://docs.aws.amazon.com/kendra/latest/dg/query-suggestions.html#query-suggestions-blocklist)後，每 24 小時自動更新建議。

------
#### [ CLI ]

**擷取查詢建議**

```
aws kendra get-query-suggestions \
 --index-id index-id \
 --query-text "query-text" \
 --suggestion-types '["QUERY"]' \
 --max-suggestions-count 1 // If you want to limit the number of suggestions
```

**更新查詢建議**

例如，若要變更查詢日誌時間範圍和查詢必須搜尋的最小次數：

```
aws kendra update-query-suggestions-config \
 --index-id index-id \
 --query-log-look-back-window-in-days 30 \
 --minimum-query-count 100
```

------
#### [ Python ]

**擷取查詢建議**

```
import boto3
from botocore.exceptions import ClientError

kendra = boto3.client("kendra")

print("Get query suggestions.")

# Provide the index ID
index_id = "index-id"

# Provide the query text
query_text = "query"

# Provide the query suggestions type
query_suggestions_type = "QUERY"


# If you want to limit the number of suggestions
num_suggestions = 1
 
try:
    query_suggestions_response = kendra.get_query_suggestions(
        IndexId = index_id,
        QueryText = query_text,
        SuggestionTypes = query_suggestions_type,
        MaxSuggestionsCount = num_suggestions
    )

    # Print out the suggestions you received
    if ("Suggestions" in query_suggestions_response.keys()) {
        for (suggestion: query_suggestions_response["Suggestions"]) {
            print(suggestion["Value"]["Text"]["Text"]);
        }
    }
   
except ClientError as e:
        print("%s" % e)

print("Program ends.")
```

**更新查詢建議**

例如，若要變更查詢日誌時間範圍和查詢必須搜尋的最小次數：

```
import boto3
from botocore.exceptions import ClientError
import pprint
import time

kendra = boto3.client("kendra")

print("Updating query suggestions settings/configuration for an index.")

# Provide the index ID
index_id = "index-id"

# Configure the settings you want to update
minimum_query_count = 100
query_log_look_back_window_in_days = 30

try:
    kendra.update_query_suggestions_config(
        IndexId = index_id,
        MinimumQueryCount = minimum_query_count,
        QueryLogLookBackWindowInDays = query_log_look_back_window_in_days
    )

    print("Wait for Amazon Kendra to update the query suggestions.")

    while True:
        # Get query suggestions description of settings/configuration
        query_sugg_config_response = kendra.describe_query_suggestions_config(
            IndexId = index_id
        )
        
        # If status is not UPDATING, then quit
        status = query_sugg_config_response["Status"]
        print(" Updating query suggestions config. Status: " + status)
        if status != "UPDATING":
            break
        time.sleep(60)

except ClientError as e:
        print("%s" % e)

print("Program ends.")
```

------

### 保留查詢歷史記錄時清除建議
<a name="query-suggestions-history-clear"></a>

**注意**  
功能支援會因索引類型和正在使用的搜尋 API 而有所不同。若要查看您正在使用的索引類型和搜尋 API 是否支援此功能，請參閱[索引類型](https://docs.aws.amazon.com/kendra/latest/dg/hiw-index-types.html)。

您可以使用 [https://docs.aws.amazon.com/kendra/latest/APIReference/API_DescribeQuerySuggestionsConfig.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_DescribeQuerySuggestionsConfig.html) API 清除查詢建議。清除建議只會刪除現有的查詢建議，不會刪除查詢歷史記錄中的查詢。當您清除建議時， 會根據從您清除建議時新增至查詢日誌的新查詢 Amazon Kendra 來學習新建議。

------
#### [ CLI ]

**清除查詢建議**

```
aws  kendra clear-query-suggestions \
 --index-id index-id
```

------
#### [ Python ]

**清除查詢建議**

```
import boto3
from botocore.exceptions import ClientError

kendra = boto3.client("kendra")

print("Clearing out query suggestions for an index.")

# Provide the index ID
index_id = "index-id"

try:
    kendra.clear_query_suggestions(
        IndexId = index_id
    )

    # Confirm last cleared date-time and that there are no suggestions
    query_sugg_config_response = kendra.describe_query_suggestions_config(
        IndexId = index_id
    )
    print("Query Suggestions last cleared at: " + str(query_sugg_config_response["LastClearTime"]));
    print("Number of suggestions available from the time of clearing: " + str(query_sugg_config_response["TotalSuggestionsCount"]));
        
except ClientError as e:
        print("%s" % e)

print("Program ends.")
```

------

### 沒有可用的建議
<a name="query-suggestions-history-none"></a>

如果您沒有看到查詢的建議，可能是下列其中一個原因：
+ 您的索引中沒有足夠的查詢 Amazon Kendra 可供 學習。
+ 您的查詢建議設定太嚴格，導致大多數查詢從建議中篩選掉。
+ 您最近已清除建議， Amazon Kendra 但仍需要時間來累積新查詢，才能學習新建議。

您可以使用 [https://docs.aws.amazon.com/kendra/latest/APIReference/API_DescribeQuerySuggestionsConfig.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_DescribeQuerySuggestionsConfig.html) API 檢查目前的設定。

## 使用文件欄位查詢建議
<a name="query-suggestions-doc-fields"></a>

**Topics**
+ [選取建議欄位的設定](#query-suggestions-doc-fields-settings)
+ [文件欄位中的使用者控制](#query-suggestions-doc-fields-user-control)

您可以選擇根據文件欄位的內容，建議與使用者相關的查詢。與其使用查詢歷史記錄來建議其他熱門的相關查詢，您可以使用文件欄位中包含的資訊，這些資訊有助於自動完成 query. Amazon Kendra look 以取得設定為 `Suggestable`且與使用者查詢密切相符的相關內容。然後， Amazon Kendra 當使用者開始輸入查詢時，建議他們使用此內容。

例如，如果您指定要作為建議基礎的標題欄位，而使用者開始輸入查詢 'How amazon ken...'，則可能會建議最相關的標題 'How Amazon Kendra works' 自動完成搜尋。使用者看到「如何 Amazon Kendra 運作」直接出現在搜尋列下方，自動完成搜尋查詢。使用者選取此查詢建議，並在搜尋結果中傳回文件「如何 Amazon Kendra 運作」。

您可以使用 `String`和 `StringList`類型的任何文件欄位內容來建議查詢，方法是將欄位設定為 ，`Suggestable`做為查詢建議的欄位組態的一部分。您也可以使用[封鎖清單](https://docs.aws.amazon.com/kendra/latest/dg/query-suggestions.html#query-suggestions-blocklist)，讓包含特定單字或片語的建議文件欄位不會向您的使用者顯示。您可以使用一個區塊清單。無論您將查詢建議設定為使用查詢歷史記錄或文件欄位，都會套用封鎖清單。

### 選取建議欄位的設定
<a name="query-suggestions-doc-fields-settings"></a>

您可以使用 設定下列設定，以選取建議的文件欄位，[https://docs.aws.amazon.com/kendra/latest/APIReference/API_AttributeSuggestionsConfig.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_AttributeSuggestionsConfig.html)並呼叫 [https://docs.aws.amazon.com/kendra/latest/APIReference/API_UpdateQuerySuggestionsConfig.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_UpdateQuerySuggestionsConfig.html) API 在索引層級更新設定：
+ **欄位/屬性建議模式** - 使用文件欄位的查詢建議為 `ACTIVE`或 `INACTIVE`。 Amazon Kendra 預設會啟用查詢建議。
+ **建議欄位/屬性** - 要作為建議基礎的欄位名稱或欄位索引鍵。這些欄位必須設定為`TRUE`適用於 `Suggestable`，作為欄位組態的一部分。您可以在查詢層級覆寫欄位組態，同時在索引層級維護組態。使用 [GetQuerySuggestions](https://docs.aws.amazon.com/kendra/latest/APIReference/API_GetQuerySuggestions.html) API 在查詢層級`AttributeSuggestionConfig`進行變更。查詢層級的此組態對於快速實驗使用不同文件欄位非常有用，而無需在索引層級更新組態。
+ **其他欄位/屬性** - 您要包含在查詢建議回應中的其他欄位。這些欄位用於在回應中提供額外資訊；但是，它們不會用來作為建議的基礎。

**警告**  
您對設定所做的變更可能不會立即生效。您可以使用 [https://docs.aws.amazon.com/kendra/latest/APIReference/API_DescribeQuerySuggestionsConfig.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_DescribeQuerySuggestionsConfig.html) API 追蹤設定變更。更新設定生效的時間取決於您所做的更新。變更設定後或套用[封鎖清單](https://docs.aws.amazon.com/kendra/latest/dg/query-suggestions.html#query-suggestions-blocklist)後， 會每 24 小時 Amazon Kendra 自動更新建議。

------
#### [ CLI ]

若要擷取查詢建議並覆寫查詢層級的文件欄位組態，而不必變更索引層級的組態。

```
aws kendra get-query-suggestions \
 --index-id index-id \
 --query-text "query-text" \
 --suggestion-types '["DOCUMENT_ATTRIBUTES"]' \
 --attribute-suggestions-config '{"SuggestionAttributes":'["field/attribute key 1", "field/attribute key 2"]', "AdditionalResponseAttributes":'["response field/attribute key 1", "response field/attribute key 2"]'}' \
 --max-suggestions-count 1 // If you want to limit the number of suggestions
```

**更新查詢建議**

例如，若要變更索引層級的文件欄位組態：

```
aws kendra update-query-suggestions-config \
 --index-id index-id \
 --attribute-suggestions-config '{"SuggestableConfigList": '[{"SuggestableConfig": "_document_title", "Suggestable": true}]', "AttributeSuggestionsMode": "ACTIVE"}'
```

------
#### [ Python ]

若要擷取查詢建議並覆寫查詢層級的文件欄位組態，而不必變更索引層級的組態。

```
import boto3
from botocore.exceptions import ClientError

kendra = boto3.client("kendra")

print("Get query suggestions.")

# Provide the index ID
index_id = "index-id"

# Provide the query text
query_text = "query"

# Provide the query suggestions type
query_suggestions_type = "DOCUMENT_ATTRIBUTES"

# Override fields/attributes configuration at query level
configuration = {"SuggestionAttributes":
    '["field/attribute key 1", "field/attribute key 2"]', 
      "AdditionalResponseAttributes":
          '["response field/attribute key 1", "response field/attribute key 2"]'
          }

# If you want to limit the number of suggestions
num_suggestions = 1

try:
    query_suggestions_response = kendra.get_query_suggestions(
        IndexId = index_id,
        QueryText = query_text,
        SuggestionTypes = [query_suggestions_type],
        AttributeSuggestionsConfig = configuration,
        MaxSuggestionsCount = num_suggestions
    )

    # Print out the suggestions you received
    if ("Suggestions" in query_suggestions_response.keys()) {
        for (suggestion: query_suggestions_response["Suggestions"]) {
            print(suggestion["Value"]["Text"]["Text"]);
        }
    }
   
except ClientError as e:
        print("%s" % e)

print("Program ends.")
```

**更新查詢建議**

例如，若要變更索引層級的文件欄位組態：

```
import boto3
from botocore.exceptions import ClientError
import pprint
import time

kendra = boto3.client("kendra")

print("Updating query suggestions settings/configuration for an index.")

# Provide the index ID
index_id = "index-id"

# Configure the settings you want to update at the index level
configuration = {"SuggestableConfigList": 
    '[{"SuggestableConfig": "_document_title", "Suggestable": true}]', 
       "AttributeSuggestionsMode": "ACTIVE"
       }

try:
    kendra.update_query_suggestions_config(
        IndexId = index_id,
        AttributeSuggestionsConfig = configuration
    )

    print("Wait for Amazon Kendra to update the query suggestions.")

    while True:
        # Get query suggestions description of settings/configuration
        query_sugg_config_response = kendra.describe_query_suggestions_config(
            IndexId = index_id
        )
        
        # If status is not UPDATING, then quit
        status = query_sugg_config_response["Status"]
        print(" Updating query suggestions config. Status: " + status)
        if status != "UPDATING":
            break
        time.sleep(60)

except ClientError as e:
        print("%s" % e)

print("Program ends.")
```

------

### 文件欄位中的使用者控制
<a name="query-suggestions-doc-fields-user-control"></a>

您可以將使用者內容篩選套用至要作為查詢建議基礎的文件欄位。此篩選條件會根據使用者或其群組對文件的存取來篩選文件欄位資訊。例如， 端點會搜尋公司的入口網站，而且無法存取最機密的公司文件。因此，根據最機密文件的標題或任何其他建議欄位的建議查詢，不會向整數顯示。

您可以使用存取控制清單 (ACL) 為文件編製索引，定義哪些使用者和群組被指派存取哪些文件。然後，您可以將使用者內容篩選套用至文件欄位，以取得查詢建議。目前為您的索引設定的使用者內容篩選，與套用至文件欄位組態的查詢建議相同的使用者內容篩選。使用者內容篩選是文件欄位組態的一部分。您可以使用 [https://docs.aws.amazon.com/kendra/latest/APIReference/API_AttributeSuggestionsConfig.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_AttributeSuggestionsConfig.html)並呼叫 [https://docs.aws.amazon.com/kendra/latest/APIReference/API_GetQuerySuggestions.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_GetQuerySuggestions.html)。

## 從建議中封鎖特定查詢或文件欄位內容
<a name="query-suggestions-blocklist"></a>

*封鎖清單* Amazon Kendra 會停止向您的使用者建議特定查詢。封鎖清單是您要從查詢建議中排除的單字或片語清單。 Amazon Kendra 排除包含完全符合封鎖清單中單字或片語的查詢。

您可以使用封鎖清單來防止攻擊性單字或片語，這些單字或片語通常出現在您的查詢歷史記錄或文件欄位中， Amazon Kendra 並且可以選取 做為建議。封鎖清單也可以 Amazon Kendra 防止 建議包含尚未準備好公開發佈或宣布之資訊的查詢。例如，您的使用者經常查詢潛在新產品即將發行的版本。不過，您不想建議產品，因為您尚未準備好發佈。您可以從建議封鎖包含產品名稱和產品資訊的查詢。

您可以使用 [https://docs.aws.amazon.com/kendra/latest/APIReference/API_CreateQuerySuggestionsBlockList.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_CreateQuerySuggestionsBlockList.html) API 建立查詢的封鎖清單。您可以將每個區塊字或片語放在文字檔案中的個別行上。然後，您將文字檔案上傳至 Amazon S3 儲存貯體，並提供檔案的路徑或位置 Amazon S3 in. Amazon Kendra currently 僅支援建立一個封鎖清單。

您可以取代儲存 Amazon S3 貯體中已封鎖單字和片語的文字檔案。若要更新 中的封鎖清單 Amazon Kendra，請使用 [https://docs.aws.amazon.com/kendra/latest/APIReference/API_UpdateQuerySuggestionsBlockList.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_UpdateQuerySuggestionsBlockList.html) API。

使用 [https://docs.aws.amazon.com/kendra/latest/APIReference/API_DescribeQuerySuggestionsBlockList.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_DescribeQuerySuggestionsBlockList.html) API 取得封鎖清單的狀態。 `DescribeQuerySuggestionsBlockList`也可以為您提供其他有用的資訊，例如：
+ 您的封鎖清單上次更新的時間
+ 您目前的封鎖清單中有多少個單字或片語
+ 建立封鎖清單時的實用錯誤訊息

您也可以使用 [https://docs.aws.amazon.com/kendra/latest/APIReference/API_ListQuerySuggestionsBlockLists.html](https://docs.aws.amazon.com/kendra/latest/APIReference/API_ListQuerySuggestionsBlockLists.html) API 來取得索引的封鎖清單摘要清單。

若要刪除封鎖清單，請使用 [DeleteQuerySuggestionsBlockList](https://docs.aws.amazon.com/kendra/latest/APIReference/API_DeleteQuerySuggestionsBlockList.html) API。

您對封鎖清單的更新可能不會立即生效。您可以使用 `DescribeQuerySuggestionsBlockList` API 追蹤更新。

------
#### [ CLI ]

**建立封鎖清單**

```
aws kendra create-query-suggestions-block-list \
 --index-id index-id \
 --name "block-list-name" \
 --description "block-list-description" \
 --source-s3-path "Bucket=bucket-name,Key=query-suggestions/block_list.txt" \
 --role-arn role-arn
```

**更新封鎖清單**

```
aws kendra update-query-suggestions-block-list \
 --index-id index-id \
 --name "new-block-list-name" \
 --description "new-block-list-description" \
 --source-s3-path "Bucket=bucket-name,Key=query-suggestions/new_block_list.txt" \
 --role-arn role-arn
```

**刪除封鎖清單**

```
aws kendra delete-query-suggestions-block-list \
 --index-id index-id \
 --id block-list-id
```

------
#### [ Python ]

**建立封鎖清單**

```
import boto3
from botocore.exceptions import ClientError
import pprint
import time

kendra = boto3.client("kendra")

print("Create a query suggestions block list.")

# Provide a name for the block list
block_list_name = "block-list-name"
# Provide an optional description for the block list
block_list_description = "block-list-description"
# Provide the IAM role ARN required for query suggestions block lists
block_list_role_arn = "role-arn"

# Provide the index ID
index_id = "index-id"

s3_bucket_name = "bucket-name"
s3_key = "query-suggestions/block_list.txt"
source_s3_path = {
    'Bucket': s3_bucket_name,
    'Key': s3_key
}

try:
    block_list_response = kendra.create_query_suggestions_block_list(
        Description = block_list_description,
        Name = block_list_name,
        RoleArn = block_list_role_arn,
        IndexId = index_id,
        SourceS3Path = source_s3_path
    )

    print(block_list_response)

    block_list_id = block_list_response["Id"]

    print("Wait for Amazon Kendra to create the block list.")

    while True:
        # Get block list description
        block_list_description = kendra.describe_query_suggestions_block_list(
            Id = block_list_id,
            IndexId = index_id
        )
        # If status is not CREATING, then quit
        status = block_list_description["Status"]
        print("Creating block list. Status: " + status)
        if status != "CREATING":
            break
        time.sleep(60)
        
except ClientError as e:
        print("%s" % e)

print("Program ends.")
```

**更新封鎖清單**

```
import boto3
from botocore.exceptions import ClientError
import pprint
import time
                        
kendra = boto3.client("kendra")
                        
print("Update a block list for query suggestions.")

# Provide the block list name you want to update
block_list_name = "new-block-list-name"
# Provide the block list description you want to update
block_list_description = "new-block-list-description"
# Provide the IAM role ARN required for query suggestions block lists
block_list_role_arn = "role-arn"

# Provide the block list ID
block_list_id = "block-list-id"
# Provide the index ID
index_id = "index-id"
                        
s3_bucket_name = "bucket-name"
s3_key = "query-suggestions/new_block_list.txt"
source_s3_path = {
'Bucket': s3_bucket_name,
'Key': s3_key
}
                        
try:
    kendra.update_query_suggestions_block_list(
        Id = block_list_id,
        IndexId = index_id,
        Description = block_list_description,
        Name = block_list_name,
        RoleArn = block_list_role_arn,
        SourceS3Path = source_s3_path
    )
                        
    print("Wait for Amazon Kendra to update the block list.")
                        
    while True:
        # Get block list description
        block_list_description = kendra.describe_query_suggestions_block_list(
            Id = block_list_id,
            IndexId = index_id
        )
        # If status is not UPDATING, then the update has finished 
        status = block_list_description["Status"]
        print("Updating block list. Status: " + status)
        if status != "UPDATING":
            break
        time.sleep(60)
                        
except ClientError as e:
print("%s" % e)
                        
print("Program ends.")
```

**刪除封鎖清單**

```
import boto3
from botocore.exceptions import ClientError

kendra = boto3.client("kendra")

print("Delete a block list for query suggestions.")

# provide the block list ID
query_suggestions_block_list_id = "query-suggestions-block-list-id"
# Provide the index ID
index_id = "index-id"

try:
    kendra.delete_query_suggestions_block_list(
        Id = query_suggestions_block_list_id,
        IndexId = index_id
    )

except ClientError as e:
        print("%s" % e)

print("Program ends.")
```

------