在 Amazon OpenSearch Service 中搜尋資料 - Amazon OpenSearch Service

在 Amazon OpenSearch Service 中搜尋資料

有幾種在 Amazon OpenSearch Service​ 中搜尋文件的常用方法,包含 URI 搜尋與要求主體搜尋。OpenSearch Service 提供額外的功能,可改善搜尋體驗,例如自訂套件、SQL 支援和非同步搜尋。如需完整 OpenSearch 搜尋 API 參考,請參閱 OpenSearch 文件

注意

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

URI 搜尋

通用資源識別碼 (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

要求主體搜尋

若要執行更複雜的搜尋,查詢時請使用 HTTP 要求主體以及 ​OpenSearch 網域專用語言 (DSL)。查詢 DSL 可讓您指定 OpenSearch 搜尋選項的完整範圍。下列 match​ 查詢類似於最終 ​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 GETPOST 用於請求本文搜尋,但是所有 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"] } } }

增加欄位

您可以透過「增加」特定欄位來提升搜尋相關性。增加功能為運用倍數在一個欄位中加重配對數,使之較其他欄位中的配對數更高。在下面的例子中,匹配 title 欄位中的 John_score 的影響是在 plot 欄位中匹配的兩倍,以及在 actorsdirectors 欄位中匹配的四倍。結果就是 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"] } } }

對搜尋結果進行分頁

如果您需要顯示大量的搜尋結果,您可以使用 from 參數來實作分頁。​ 以下請求傳回結果 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"] } } }

搜尋結果反白呈現

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

如果您對文件的內容不感興趣,且只想知道相符項目的數量,您可以使用 _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 } }