第 2 步:将数据上传到 OpenSearch Service 以便编制索引 - Amazon Opensearch Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

第 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 中编制数据索引

下一步搜索文档