步驟 2:將資料上傳到 OpenSearch Service 以編製索引 - Amazon OpenSearch Service

步驟 2:將資料上傳到 OpenSearch Service 以編製索引

重要

這是將少量測試資料上傳到 Amazon OpenSearch Service (OpenSearch Service) 的簡明教程。如需有關在生產網域中上傳資料的詳細資訊,請參閱在 Amazon OpenSearch Service 中建立資料索引

您可以使用命令列或大部分程式設計語言將資料上傳至 OpenSearch Service 網域。

為了簡潔和方便起見,下列範例請求使用 curl (常見的 HTTP 用戶端)。像 curl 一樣的用戶端無法執行請求簽署,但如果您的存取政策指定 IAM 使用者或角色,便需要執行請求簽署。若要成功完成此程序,您必須搭配使用精細存取控制與主要使用者名稱和密碼,如您在步驟 1 中所設定。

您可以在 Windows 中安裝 curl,並在命令提示中使用它,但我們建議使用像 CygwinWindows Subsystem for Linux 之類的工具。macOS 和大部分 Linux 發行版本均預先安裝有 curl。

選項 1:上傳單一文件

執行以下命令,將單一文件加入到 movies 網域:

curl -XPUT -u 'master-user:master-user-password' 'domain-endpoint/movies/_doc/1' -d '{"director": "Burton, Tim", "genre": ["Comedy","Sci-Fi"], "year": 1996, "actor": ["Jack Nicholson","Pierce Brosnan","Sarah Jessica Parker"], "title": "Mars Attacks!"}' -H 'Content-Type: application/json'

在命令中,提供您在步驟 1 中建立的使用者名稱和密碼。

如需有關此命令以及如何向 OpenSearch Service 提出簽署請求的詳細說明,請參閱在 Amazon OpenSearch Service 中建立資料索引

選項 2:上傳多個文件

若要將包含多個文件的 JSON 檔案上傳到 OpenSearch Service 網域

  1. 建立稱為 bulk_movies.json 的本機檔案。將以下內容貼到檔案中,並新增結尾新行:

    { "index" : { "_index": "movies", "_id" : "2" } } {"director": "Frankenheimer, John", "genre": ["Drama", "Mystery", "Thriller", "Crime"], "year": 1962, "actor": ["Lansbury, Angela", "Sinatra, Frank", "Leigh, Janet", "Harvey, Laurence", "Silva, Henry", "Frees, Paul", "Gregory, James", "Bissell, Whit", "McGiver, John", "Parrish, Leslie", "Edwards, James", "Flowers, Bess", "Dhiegh, Khigh", "Payne, Julie", "Kleeb, Helen", "Gray, Joe", "Nalder, Reggie", "Stevens, Bert", "Masters, Michael", "Lowell, Tom"], "title": "The Manchurian Candidate"} { "index" : { "_index": "movies", "_id" : "3" } } {"director": "Baird, Stuart", "genre": ["Action", "Crime", "Thriller"], "year": 1998, "actor": ["Downey Jr., Robert", "Jones, Tommy Lee", "Snipes, Wesley", "Pantoliano, Joe", "Jacob, Ir\u00e8ne", "Nelligan, Kate", "Roebuck, Daniel", "Malahide, Patrick", "Richardson, LaTanya", "Wood, Tom", "Kosik, Thomas", "Stellate, Nick", "Minkoff, Robert", "Brown, Spitfire", "Foster, Reese", "Spielbauer, Bruce", "Mukherji, Kevin", "Cray, Ed", "Fordham, David", "Jett, Charlie"], "title": "U.S. Marshals"} { "index" : { "_index": "movies", "_id" : "4" } } {"director": "Ray, Nicholas", "genre": ["Drama", "Romance"], "year": 1955, "actor": ["Hopper, Dennis", "Wood, Natalie", "Dean, James", "Mineo, Sal", "Backus, Jim", "Platt, Edward", "Ray, Nicholas", "Hopper, William", "Allen, Corey", "Birch, Paul", "Hudson, Rochelle", "Doran, Ann", "Hicks, Chuck", "Leigh, Nelson", "Williams, Robert", "Wessel, Dick", "Bryar, Paul", "Sessions, Almira", "McMahon, David", "Peters Jr., House"], "title": "Rebel Without a Cause"}
  2. 在儲存檔案的本機目錄中執行下列命令,以將其上傳到 movies 網域:

    curl -XPOST -u 'master-user:master-user-password' 'domain-endpoint/_bulk' --data-binary @bulk_movies.json -H 'Content-Type: application/json'

如需有關大量檔案格式的詳細資訊,請參閱在 Amazon OpenSearch Service 中建立資料索引

下一步搜尋文件