

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

# 篩選和面向搜尋
<a name="filtering"></a>

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

您可以使用篩選條件來改善[查詢](https://docs.aws.amazon.com/kendra/latest/APIReference/API_Query.html) API 的搜尋結果或回應。篩選條件會將回應中的文件限制為直接套用至查詢的文件。若要建立面向搜尋建議，請使用布林邏輯，從回應或不符合特定條件的文件篩選出特定文件屬性。您可以使用 `Query` API 中的 `Facets` 參數來指定面向。

若要搜尋您已編製索引的文件 Amazon Lex，請使用 AMAZON Amazon Kendra .KendraSearchIntent。 [KendraSearchIntent](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_KendraConfiguration.html) 如需使用 設定 Amazon Kendra 的範例 Amazon Lex，請參閱[為 Amazon Kendra 索引建立常見問答集機器人](https://docs.aws.amazon.com/lexv2/latest/dg/faq-bot-kendra-search.html)。您也可以使用 [AttributeFilter](https://docs.aws.amazon.com/kendra/latest/APIReference/API_AttributeFilter.html) 提供回應的篩選條件。這是設定 時 JSON 中的查詢篩選條件`AMAZON.KendraSearchIntent`。若要在主控台中設定搜尋意圖時提供屬性篩選條件，請前往意圖編輯器，然後選擇 Amazon Kendra 查詢以在 JSON 中提供查詢篩選條件。如需 的詳細資訊`AMAZON.KendraSearchIntent`，請參閱 [Amazon Lex 文件指南](https://docs.aws.amazon.com/lexv2/latest/dg/built-in-intent-kendra-search.html)。

## 面向
<a name="search-facets"></a>

面向是一組搜尋結果的範圍檢視。例如，您可以提供全球城市的搜尋結果，其中文件會依與其相關聯的特定城市進行篩選。或者，您可以建立面向來顯示特定作者的結果。

您可以使用與文件相關聯的文件屬性或中繼資料欄位做為構面，讓使用者可以依該構面內的類別或值進行搜尋。您也可以在搜尋結果中顯示巢狀面向，讓使用者不僅可以依類別或欄位進行搜尋，還可以依子類別或子欄位進行搜尋。

下列範例顯示如何取得「城市」自訂屬性的面向資訊。

```
response=kendra.query(
        QueryText = query,
        IndexId = index,
        Facets = [
            {
                "DocumentAttributeKey" : "City"
            }
        ]
        )
```

您可以使用巢狀面向來進一步縮小搜尋範圍。例如，文件屬性或構面 "City" 包含稱為 "Seattle" 的值。此外，文件屬性或構面 "CityRegion" 包含指派給 "Seattle" 的文件值 "North" 和 "South"。您可以在搜尋結果中顯示巢狀面向及其計數，以便不僅可以依城市搜尋文件，還可以依城市內的區域搜尋文件。

請注意，巢狀面向可能會影響查詢延遲。一般規則是您使用的更巢狀構面，對延遲的潛在影響更大。影響延遲的其他因素包括編製索引的文件平均大小、索引大小、高度複雜的查詢，以及索引的整體負載 Amazon Kendra 。

下列範例顯示如何取得 "CityRegion" 自訂屬性的構面資訊，做為 "City" 內的巢狀構面。

```
response=kendra.query(
        QueryText = query,
        IndexId = index,
        Facets = [
            {
                "DocumentAttributeKey" : "City",
                "Facets": [
                    {
                        "DocumentAttributeKey" : "CityRegion"
                    }
                ]
            }
        ]
        )
```

`FacetResults` 回應陣列中會傳回面向資訊，例如文件計數。您可以使用內容在應用程式中顯示面向搜尋建議。例如，如果文件屬性 "City" 包含可套用搜尋的城市，請使用該資訊顯示城市搜尋清單。使用者可以選擇城市來篩選搜尋結果。若要進行面向搜尋，請呼叫[查詢](https://docs.aws.amazon.com/kendra/latest/APIReference/API_Query.html) API，並使用選擇的文件屬性來篩選結果。

對於查詢，每個構面最多可以顯示 10 個構面值，而構面內只能顯示一個巢狀構面。如果您想要提高這些限制，請聯絡 [Support](https://aws.amazon.com/contact-us/)。如果您想要將每個構面的構面值數限制為小於 10，您可以在 `Facet` 物件中指定此值。

下列範例 JSON 回應顯示範圍為「城市」文件屬性的面向。回應包含面向值的文件計數。

```
{
    'FacetResults': [
        {
            'DocumentAttributeKey': 'City',
            'DocumentAttributeValueCountPairs': [
                {
                    'Count': 3,
                    'DocumentAttributeValue': {
                        'StringValue': 'Dubai'
                    }
                },
                {
                    'Count': 3,
                    'DocumentAttributeValue': {
                        'StringValue': 'Seattle'
                    }
                },
                {
                    'Count': 1,
                    'DocumentAttributeValue': {
                        'StringValue': 'Paris'
                    }
                }
            ]
        }
    ]
```

您也可以顯示巢狀構面的構面資訊，例如城市內的區域，以進一步篩選搜尋結果。

下列範例 JSON 回應顯示範圍為 "CityRegion" 文件屬性的構面，做為 "City" 內的巢狀構面。回應包含巢狀構面值的文件計數。

```
{
    'FacetResults': [
        {
            'DocumentAttributeKey': 'City',
            'DocumentAttributeValueCountPairs': [
                {
                    'Count': 3,
                    'DocumentAttributeValue': {
                        'StringValue': 'Dubai'
                    },
                    'FacetResults': [
                        {
                            'DocumentAttributeKey': 'CityRegion',
                            'DocumentAttributeValueCountPairs': [
                                 {
                                     'Count': 2,
                                     'DocumentAttributeValue': {
                                         'StringValue': 'Bur Dubai'
                                     }
                                 },
                                 {
                                     'Count': 1,
                                     'DocumentAttributeValue': {
                                         'StringValue': 'Deira'
                                     }
                                 }
                             ]
                        }
                    ]
                },
                {
                    'Count': 3,
                    'DocumentAttributeValue': {
                        'StringValue': 'Seattle'
                    },
                    'FacetResults': [
                        {
                            'DocumentAttributeKey': 'CityRegion',
                            'DocumentAttributeValueCountPairs': [
                                 {
                                     'Count': 1,
                                     'DocumentAttributeValue': {
                                         'StringValue': 'North'
                                     }
                                 },
                                 {
                                     'Count': 2,
                                     'DocumentAttributeValue': {
                                         'StringValue': 'South'
                                     }
                                 }
                             ]
                        }
                    ]
                },
                {
                    'Count': 1,
                    'DocumentAttributeValue': {
                        'StringValue': 'Paris'
                    },
                    'FacetResults': [
                        {
                            'DocumentAttributeKey': 'CityRegion',
                            'DocumentAttributeValueCountPairs': [
                                 {
                                     'Count': 1,
                                     'DocumentAttributeValue': {
                                         'StringValue': 'City center'
                                     }
                                 }
                             ]
                        }
                    ]
                }
        }
    ]
}
```

當您使用字串清單欄位建立面向時，傳回的面向結果會根據字串清單的內容。例如，如果您有一個包含兩個項目的字串清單欄位，一個具有「dachshund」清單、「sausage dog」清單，另一個具有「husky」值，您會`FacetResults`得到三個面向。

如需詳細資訊，請參閱[查詢回應和回應類型](query-responses-types.md)。

## 使用文件屬性篩選搜尋結果
<a name="search-filtering"></a>

根據預設， `Query`會傳回所有搜尋結果。若要篩選回應，您可以對文件屬性執行邏輯操作。例如，如果您只想要特定城市的文件，您可以篩選「城市」和「州」自訂文件屬性。您可以使用 [AttributeFilter](https://docs.aws.amazon.com/kendra/latest/APIReference/API_AttributeFilter.html) 在您提供的篩選條件上建立布林值操作。

大多數屬性可用來篩選所有回應[類型的回應](https://docs.aws.amazon.com/kendra/latest/dg/query-responses-types.html)。不過， `_excerpt_page_number` 屬性僅適用於篩選`ANSWER`回應時的回應類型。

下列範例顯示如何篩選特定城市、*西雅圖*和華盛頓州，以執行邏輯 AND 操作**。

```
response=kendra.query(
        QueryText = query,
        IndexId = index,
        AttributeFilter = {'AndAllFilters': 
            [ 
                {"EqualsTo": {"Key": "City","Value": {"StringValue": "Seattle"}}},
                {"EqualsTo": {"Key": "State","Value": {"StringValue": "Washington"}}}
            ]
            }
        )
```

下列範例顯示當任何 `Fileformat`、 `Author`或 `SourceURI`金鑰符合指定的值時，如何執行 的邏輯 OR 操作。

```
response=kendra.query(
        QueryText = query,
        IndexId = index,
        AttributeFilter = {'OrAllFilters': 
            [ 
                {"EqualsTo": {"Key": "Fileformat","Value": {"StringValue": "AUTO_DETECT"}}},
                {"EqualsTo": {"Key": "Author","Value": {"StringValue": "Ana Carolina"}}},
                {"EqualsTo": {"Key": "SourceURI","Value": {"StringValue": "https://aws.amazonaws.com/234234242342"}}}
            ]
            }
        )
```

對於 `StringList` 欄位，請使用 `ContainsAny`或 `ContainsAll` 屬性篩選條件來傳回具有指定字串的文件。下列範例顯示如何傳回其`Locations`自訂屬性中值為 "Seattle" 或 "Portland" 的所有文件。

```
response=kendra.query(
        QueryText = query,
        IndexId = index,
        AttributeFilter = {
                "ContainsAny": { "Key": "Locations", "Value": { "StringListValue": [ "Seattle", "Portland"] }}
            }
        )
```

## 在搜尋結果中篩選每個文件的屬性
<a name="filtering-document-attributes"></a>

Amazon Kendra 會傳回搜尋結果中每個文件的文件屬性。您可以篩選要包含在回應中的特定文件屬性，做為搜尋結果的一部分。根據預設，指派給文件的所有文件屬性都會在回應中傳回。

在下列範例中，只有 `_source_uri`和 `_author` 文件屬性包含在 文件的回應中。

```
response=kendra.query(
        QueryText = query,
        IndexId = index,
        RequestedDocumentAttributes = ["_source_uri", "_author"]
        )
```