

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

# 在 Amazon OpenSearch Service 中搜尋資料
<a name="searching"></a>

有幾種在 Amazon OpenSearch Service​ 中搜尋文件的常用方法，包含 URI 搜尋與要求主體搜尋。OpenSearch Service 提供額外的功能，可改善搜尋體驗，例如自訂套件、SQL 支援和非同步搜尋。如需完整 OpenSearch 搜尋 API 參考，請參閱 [OpenSearch 文件](https://docs.opensearch.org/latest/opensearch/query-dsl/full-text/)。

**注意**  
下列範例請求可與 OpenSearch API 搭配使用。部分請求可能無法與較舊的 ​Elasticsearch 版本搭配使用。

**Topics**
+ [URI 搜尋](#searching-uri)
+ [要求主體搜尋](#searching-dsl)
+ [對搜尋結果進行分頁](#searching-paginating)
+ [儀表板查詢語言](#DashboardsQueryLanguages)
+ [在 Amazon OpenSearch Service 中匯入和管理套件](custom-packages.md)
+ [使用 SQL 查詢您的 Amazon OpenSearch Service 資料](sql-support.md)
+ [Amazon OpenSearch Service 中的跨叢集搜尋](cross-cluster-search.md)
+ [適用於 Amazon OpenSearch Service 的 Learning to Rank](learning-to-rank.md)
+ [Amazon OpenSearch Service 中的非同步搜尋](asynchronous-search.md)
+ [Amazon OpenSearch Service 中的時間點搜尋](pit.md)
+ [Amazon OpenSearch Service 中的代理程式搜尋](agentic-search.md)
+ [Amazon OpenSearch Service 中的語意搜尋](semantic-search.md)
+ [Amazon OpenSearch Service 中的並行區段搜尋](concurrent-segment-search.md)
+ [在 Amazon OpenSearch Service 中產生自然語言查詢](natural-language-query.md)

## URI 搜尋
<a name="searching-uri"></a>

通用資源識別碼 (URI) 搜尋是最簡易的搜尋方式。在 URI 搜尋中，您將查詢指定為 HTTP 請求參數：

```
GET https://search-{{my-domain}}.{{us-west-1.}}es.amazonaws.com/_search?q=house
```

範例回應看起來類似如下：

```
{
  "took": 25,
  "timed_out": false,
  "_shards": {
    "total": 10,
    "successful": 10,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 85,
      "relation": "eq",
    },
    "max_score": 6.6137657,
    "hits": [
      {
        "_index": "movies",
        "_type": "movie",
        "_id": "tt0077975",
        "_score": 6.6137657,
        "_source": {
          "directors": [
            "John Landis"
          ],
          "release_date": "1978-07-27T00:00:00Z",
          "rating": 7.5,
          "genres": [
            "Comedy",
            "Romance"
          ],
          "image_url": "http://ia.media-imdb.com/images/M/MV5BMTY2OTQxNTc1OF5BMl5BanBnXkFtZTYwNjA3NjI5._V1_SX400_.jpg",
          "plot": "At a 1962 College, Dean Vernon Wormer is determined to expel the entire Delta Tau Chi Fraternity, but those troublemakers have other plans for him.",
          "title": "Animal House",
          "rank": 527,
          "running_time_secs": 6540,
          "actors": [
            "John Belushi",
            "Karen Allen",
            "Tom Hulce"
          ],
          "year": 1978,
          "id": "tt0077975"
        }
      },
      {{...}}
    ]
  }
}
```

根據預設，此查詢在所有索引中的所有欄位裡搜尋含有 *house* 的詞語。​ 若要縮小搜尋，請在 URI 中指定索引 (`movies`) 與文件欄位 (`title`)。

```
GET https://search-{{my-domain}}.{{us-west-1.}}es.amazonaws.com/movies/_search?q=title:house
```

您可以在請求中加入其他參數，但是支援的參數只會提供 OpenSearch​ 搜尋選項的小型子集。下列請求傳回 20 個結果 (而非預設的 10 個)，並根據年份排序 (而非依據 `_score`)：

```
GET https://search-{{my-domain}}.{{us-west-1.}}es.amazonaws.com/movies/_search?q=title:house&size=20&sort=year:desc
```

## 要求主體搜尋
<a name="searching-dsl"></a>

若要執行更複雜的搜尋，查詢時請使用 HTTP 要求主體以及 ​OpenSearch 網域專用語言 (DSL)。查詢 DSL 可讓您指定 OpenSearch 搜尋選項的完整範圍。

**注意**  
您不能在文字欄位值中包含 Unicode 特殊字元，否則會將值剖析為由特殊字元分隔的多個值。這種不正確的剖析可能會導致意外對文件進行篩選，並可能影響對其存取權的控制。如需詳細資訊，請參閱 OpenSearch 文件中[文字欄位中 Unicode 特殊字元的注意事項](https://opensearch.org/docs/latest/opensearch/query-dsl/index/#a-note-on-unicode-special-characters-in-text-fields)。

下列 `match`​ 查詢類似於最終 [​URI 搜尋](#searching-uri)範例：

```
POST https://search-{{my-domain}}.{{us-west-1.}}es.amazonaws.com/movies/_search
{
  "size": 20,
  "sort": {
    "year": {
      "order": "desc"
    }
  },
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "house"
    }
  }
}
```

**注意**  
`_search` API 接受 HTTP `GET` 與`POST` 用於請求本文搜尋，但是所有 HTTP 用戶端支援新增請求本文到 `GET` 請求。`POST` 是更為通用的選擇。

在許多情況下，您可能想要搜尋多個欄位，但並非所有欄位。使用 `multi_match`​ 查詢：

```
POST https://search-{{my-domain}}.{{us-west-1.}}es.amazonaws.com/movies/_search
{
  "size": 20,
  "query": {
    "multi_match": {
      "query": "house",
      "fields": ["title", "plot", "actors", "directors"]
    }
  }
}
```

### 增加欄位
<a name="searching-dsl-boost"></a>

您可以透過「增加」特定欄位來提升搜尋相關性。增加功能為運用倍數在一個欄位中加重配對數，使之較其他欄位中的配對數更高。在下面的例子中，匹配 `title` 欄位中的 *John* 對`_score` 的影響是在 `plot` 欄位中匹配的兩倍，以及在 `actors` 或 `directors` 欄位中匹配的四倍。結果就是 *John Wick*​ 和 *John Carter*​ 之類的影片會在搜尋結果的頂部，且 John Travolta 主演的影片會出現在底部。

```
POST https://search-{{my-domain}}.{{us-west-1.}}es.amazonaws.com/movies/_search
{
  "size": 20,
  "query": {
    "multi_match": {
      "query": "john",
      "fields": ["title^4", "plot^2", "actors", "directors"]
    }
  }
}
```

### 搜尋結果反白呈現
<a name="searching-dsl-highlighting"></a>

`highlight`​ 選項會通知 ​OpenSearch 在 `hits`​ 陣列內傳回額外的物件 (如果查詢符合一個或多個欄位)。

```
POST https://search-{{my-domain}}.{{us-west-1.}}es.amazonaws.com/movies/_search
{
  "size": 20,
  "query": {
    "multi_match": {
      "query": "house",
      "fields": ["title^4", "plot^2", "actors", "directors"]
    }
  },
  "highlight": {
    "fields": {
      "plot": {}
    }
  }
}
```

如果查詢符合 `plot`​ 欄位的內容，則命中看起來會如下：

```
{
  "_index": "movies",
  "_type": "movie",
  "_id": "tt0091541",
  "_score": 11.276199,
  "_source": {
    "directors": [
      "Richard Benjamin"
    ],
    "release_date": "1986-03-26T00:00:00Z",
    "rating": 6,
    "genres": [
      "Comedy",
      "Music"
    ],
    "image_url": "http://ia.media-imdb.com/images/M/MV5BMTIzODEzODE2OF5BMl5BanBnXkFtZTcwNjQ3ODcyMQ@@._V1_SX400_.jpg",
    "plot": "A young couple struggles to repair a hopelessly dilapidated house.",
    "title": "The Money Pit",
    "rank": 4095,
    "running_time_secs": 5460,
    "actors": [
      "Tom Hanks",
      "Shelley Long",
      "Alexander Godunov"
    ],
    "year": 1986,
    "id": "tt0091541"
  },
  "highlight": {
    "plot": [
      "A young couple struggles to repair a hopelessly dilapidated <em>house</em>."
    ]
  }
}
```

依預設，OpenSearch ​會在 ​`<em>` 標籤中包裝相符的字串，提供高達 100 個相符項目細節的字元，並透過辨識標點符號、空格、標籤和換行來將內容分為多個句子。所有設定皆可自訂：

```
POST https://search-{{my-domain}}.{{us-west-1.}}es.amazonaws.com/movies/_search
{
  "size": 20,
  "query": {
    "multi_match": {
      "query": "house",
      "fields": ["title^4", "plot^2", "actors", "directors"]
    }
  },
  "highlight": {
    "fields": {
      "plot": {}
    },
    "pre_tags": "<strong>",
    "post_tags": "</strong>",
    "fragment_size": 200,
    "boundary_chars": ".,!? "
  }
}
```

### 計數 API
<a name="searching-dsl-count"></a>

如果您對文件的內容不感興趣，且只想知道相符項目的數量，您可以使用 `_count`​ API (而不是 `_search`​ API)。下列請求使用 `query_string`​ 查詢來識別浪漫喜劇：

```
POST https://search-{{my-domain}}.{{us-west-1.}}es.amazonaws.com/movies/_count
{
  "query": {
    "query_string": {
      "default_field": "genres",
      "query": "romance AND comedy"
    }
  }
}
```

範例回應看起來類似如下：

```
{
  "count": 564,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  }
}
```

## 對搜尋結果進行分頁
<a name="searching-paginating"></a>

如果您需要顯示大量搜尋結果，您可以使用數種不同的方法來實作分頁。

### 時間點
<a name="pag-pit"></a>

時間點 (PIT) 功能是一種搜尋類型，可讓您針對固定時間的資料集執行不同的查詢。這是 OpenSearch 中偏好的分頁方法，特別是針對深度分頁。您可以搭配 OpenSearch Service 2.5 版及更新版本使用 PIT。如需 PIT 的詳細資訊，請參閱 [Amazon OpenSearch Service 中的時間點搜尋](pit.md)。

### `from` 和 `size` 參數
<a name="pag-from-size"></a>

分頁最簡單的方法是使用 `from`和 `size` 參數。以下請求傳回結果 20-39 項以零為索引的搜尋結果清單：

```
POST https://search-{{my-domain}}.{{us-west-1.}}es.amazonaws.com/movies/_search
{
  "from": 20,
  "size": 20,
  "query": {
    "multi_match": {
      "query": "house",
      "fields": ["title^4", "plot^2", "actors", "directors"]
    }
  }
}
```

如需搜尋分頁的詳細資訊，請參閱 OpenSearch 文件中的[分頁結果](https://opensearch.org/docs/latest/opensearch/search/paginate/)。

## 儀表板查詢語言
<a name="DashboardsQueryLanguages"></a>

您可以使用[儀表板查詢語言 (DQL)](https://opensearch.org/docs/latest/dashboards/dql/#terms-query)，在 OpenSearch Dashboards 中搜尋資料和視覺化效果。DQL 使用四種主要查詢類型：*術語*、*布林值*、*日期和範圍*、以及*巢狀欄位*。

**術語查詢**

術語查詢要求您指定要搜尋的術語。

若要執行術語查詢，請輸入下列內容：

```
host:www.example.com
```

**布林值查詢**

您可以使用布林運算子 `AND`、`or` 以及 `not` 以合併多個查詢。

要執行布林值查詢，請貼上以下內容：

```
host.keyword:www.example.com and response.keyword:200
```

**日期和範圍查詢**

您可以使用日期和範圍查詢來尋找查詢之前或之後的日期。
+ `>` 表示搜尋指定日期之後的日期。
+ `<` 表示搜尋指定日期之前的日期。

`@timestamp > "2020-12-14T09:35:33"`

**巢狀欄位查詢**

如果您擁有一個帶巢狀欄位的文件，則必須指定要擷取文件的哪些部分。以下是包含巢狀欄位的範例文件：

```
{"NBA players":[
    {"player-name": "Lebron James",
      "player-position": "Power forward",
      "points-per-game": "30.3"
    },
    {"player-name": "Kevin Durant",
      "player-position": "Power forward",
      "points-per-game": "27.1"
    },
    {"player-name": "Anthony Davis",
      "player-position": "Power forward",
      "points-per-game": "23.2"
    },
    {"player-name": "Giannis Antetokounmpo",
      "player-position": "Power forward",
      "points-per-game":"29.9"
    }
  ]
}
```

若要使用 DQL 擷取特定欄位，請貼上下列內容：

```
NBA players: {player-name: Lebron James}
```

若要從巢狀文件中擷取多個物件，請貼入下列內容：

```
NBA players: {player-name: Lebron James} and NBA players: {player-name: Giannis Antetokounmpo}
```

若要在某個範圍內搜尋，請貼上以下內容：

```
NBA players: {player-name: Lebron James} and NBA players: {player-name: Giannis Antetokounmpo and < 30}
```

如果您的文件在另一個物件中有巢狀物件，您仍然可以透過指定所有層級來擷取資料。如要執行此操作，請貼上以下內容：

```
Top-Power-forwards.NBA players: {player-name:Lebron James}
```