{"id":23102,"date":"2022-12-12T16:08:42","date_gmt":"2022-12-12T07:08:42","guid":{"rendered":"https:\/\/www.skyarch.net\/blog\/?p=23102"},"modified":"2022-12-12T16:08:42","modified_gmt":"2022-12-12T07:08:42","slug":"amazon-opensearch-serverless%e3%83%97%e3%83%ac%e3%83%93%e3%83%a5%e3%83%bc-%e3%81%8b%e3%82%89python%e3%82%b9%e3%82%af%e3%83%aa%e3%83%97%e3%83%88%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e5%8f%96%e5%be%97","status":"publish","type":"post","link":"https:\/\/www.skyarch.net\/blog\/amazon-opensearch-serverless%e3%83%97%e3%83%ac%e3%83%93%e3%83%a5%e3%83%bc-%e3%81%8b%e3%82%89python%e3%82%b9%e3%82%af%e3%83%aa%e3%83%97%e3%83%88%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e5%8f%96%e5%be%97\/","title":{"rendered":"Amazon OpenSearch Serverless(\u30d7\u30ec\u30d3\u30e5\u30fc) \u304b\u3089Python\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u30c7\u30fc\u30bf\u53d6\u5f97\u3092\u3057\u3066\u307f\u307e\u3057\u305f"},"content":{"rendered":"<p>AWS re:Invent 2022\u306b\u3066\u767a\u8868\u3055\u308c\u305f\u3001Amazon OpenSearch Serverless \u30d7\u30ec\u30d3\u30e5\u30fc\u7248\u3092\u5229\u7528\u3057Python\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u306e\u30c7\u30fc\u30bf\u6295\u5165\u3001OpenSearch UI\u3067\u306e\u691c\u7d22\u3092\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n\u7d9a\u3005\u3068\u5897\u3048\u308b\u300cServerless\u300d\u3092\u30b5\u30fc\u30d3\u30b9\u540d\u306b\u5165\u308c\u305f\u3088\u308a\u30b9\u30b1\u30fc\u30e9\u30d3\u30ea\u30c6\u30a3\u304c\u9ad8\u304f\u3001\u30de\u30cd\u30fc\u30b8\u30c9\u9818\u57df\u306e\u5e83\u3044\u30b5\u30fc\u30d3\u30b9\u3001\u4eca\u5e74\u4e2d\u9803\u306eAuroraServerless v2\u3082\u597d\u611f\u89e6\u3067\u3057\u305f\u306e\u3067\u3001\u671f\u5f85\u3092\u8fbc\u3081\u3066\u89e6\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<h2>\u76ee\u6b21<\/h2>\n<ul>\n<li><a href=\"#introduction\">\u306f\u3058\u3081\u306b<\/a><\/li>\n<li><a href=\"#preparing\">\u5229\u7528\u6e96\u5099<\/a><\/li>\n<li><a href=\"#query\">\u691c\u7d22\u30fb\u30af\u30a8\u30ea\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c<\/a><\/li>\n<li><a href=\"#summery\">\u307e\u3068\u3081<\/a><\/li>\n<\/ul>\n<h2 id=\"introduction\">\u306f\u3058\u3081\u306b<\/h2>\n<p>\u4e0b\u8a182\u672c\u7acb\u3066\u3067\u304a\u9001\u308a\u3057\u307e\u3059<\/p>\n<ul>\n<li><a href=\"https:\/\/www.skyarch.net\/blog\/?p=23056\">Amazon OpenSearch Serverless(\u30d7\u30ec\u30d3\u30e5\u30fc) \u306bPython\u30b9\u30af\u30ea\u30d7\u30c8\u304b\u3089\u30c7\u30fc\u30bf\u6295\u5165\u3092\u3057\u3066\u307f\u307e\u3057\u305f<\/a><\/li>\n<li>Amazon OpenSearch Serverless(\u30d7\u30ec\u30d3\u30e5\u30fc) \u304b\u3089Python\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u30c7\u30fc\u30bf\u53d6\u5f97\u3092\u3057\u3066\u307f\u307e\u3057\u305f (\u672c\u8a18\u4e8b)<\/li>\n<\/ul>\n<p>\u4eca\u56de\u306e\u30d7\u30ec\u30d3\u30e5\u30fc\u7248\u5229\u7528\u306b\u3042\u305f\u308a\u53c2\u8003\u306b\u3057\u305f\/\u5229\u7528\u3057\u305f\u3082\u306e\u306f\u4e0b\u8a18\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>\u5185\u90e8\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u56f3\u542b\u3080\u5168\u5bb9\u306e\u53c2\u8003<\/h3>\n<ul>\n<li>Getting started with Amazon OpenSearch Serverless (Workshop)<br \/>\n<a href=\"https:\/\/catalog.us-east-1.prod.workshops.aws\/workshops\/f8d2c175-634d-4c5d-94cb-d83bbc656c6a\/en-US\">https:\/\/catalog.us-east-1.prod.workshops.aws\/workshops\/f8d2c175-634d-4c5d-94cb-d83bbc656c6a\/en-US<\/a><\/li>\n<\/ul>\n<h3>Python\u304b\u3089\u306e\u30c7\u30fc\u30bf\u6295\u5165\/\u53c2\u7167\u30d7\u30ed\u30b0\u30e9\u30e0\u53c2\u8003<\/h3>\n<ul>\n<li>Amazon OpenSearch Serverless \u306b\u3088\u308b\u624b\u8efd\u306a\u30ed\u30b0\u5206\u6790<br \/>\n<a href=\"https:\/\/aws.amazon.com\/jp\/blogs\/news\/log-analytics-the-easy-way-with-amazon-opensearch-serverless\/\">https:\/\/aws.amazon.com\/jp\/blogs\/news\/log-analytics-the-easy-way-with-amazon-opensearch-serverless\/<\/a><\/li>\n<\/ul>\n<h3>OpenSearch Python\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/h3>\n<p><a href=\"https:\/\/opensearch.org\/docs\/latest\/clients\/python\/\">https:\/\/opensearch.org\/docs\/latest\/clients\/python\/<\/a><\/p>\n<h2 id=\"preparing\">\u5229\u7528\u6e96\u5099<\/h2>\n<p><a href=\"https:\/\/www.skyarch.net\/blog\/?p=23056\">\u524d\u56de\u8a18\u4e8b<\/a> \u3092\u53c2\u8003\u306bOpenSearch Serverless\u3092\u69cb\u7bc9\u3001Index\u3092\u4f5c\u6210\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<h2 id=\"query\">\u691c\u7d22\u30fb\u30af\u30a8\u30ea\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u5b9f\u884c<\/h2>\n<p>\u8a8d\u8a3c\u60c5\u5831\u306fDefault\u306e\u7269\u3092\u5229\u7528\u3057\u307e\u3059\u306e\u3067\u3001\u3054\u6ce8\u610f\u4e0b\u3055\u3044\u3002<\/p>\n<h5>requirements.txt<\/h5>\n<pre>\nopensearch-py\nrequests_aws4auth\nboto3\n<\/pre>\n<h5>get.py<\/h5>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom opensearchpy import OpenSearch, RequestsHttpConnection\r\nfrom requests_aws4auth import AWS4Auth\r\nimport boto3\r\nimport json\r\n\r\nhost = '&#x5B;\u4f5c\u6210\u3057\u305f\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306b\u5fdc\u3058\u305f\u8b58\u5225\u5b50].ap-northeast-1.aoss.amazonaws.com' # OpenSearch Serverless collection endpoint\r\nregion = 'ap-northeast-1' # e.g. us-west-2\r\n\r\nservice = 'aoss'\r\ncredentials = boto3.Session().get_credentials()\r\nawsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service,\r\nsession_token=credentials.token)\r\n\r\n# Create an OpenSearch client\r\nclient = OpenSearch(\r\n    hosts = &#x5B;{'host': host, 'port': 443}],\r\n    http_auth = awsauth,\r\n    use_ssl = True,\r\n    verify_certs = True,\r\n    connection_class = RequestsHttpConnection\r\n)\r\n# Specify index name\r\nindex_name = 'zip-jp-index'\r\n\r\n# Search for the Documents\r\nquery = {\r\n    &quot;size&quot;: 2,\r\n    &quot;_source&quot;: {\r\n        &quot;includes&quot;: &#x5B;\r\n            &quot;zip7digits&quot;,\r\n            &quot;PrefName&quot;,\r\n            &quot;CityName&quot;,\r\n            &quot;TownName&quot;\r\n        ]\r\n    },\r\n    &quot;query&quot;: {\r\n        &quot;query_string&quot;: {\r\n        &quot;default_field&quot;: &quot;CityName&quot;,\r\n        &quot;query&quot;: &quot;\u5ddd\u5d0e\u5e02\u5e78\u533a&quot;\r\n        }\r\n    }\r\n}\r\n\r\nresponse = client.search(\r\n    body = query,\r\n    index = index_name\r\n)\r\nprint(response)\r\n<\/pre>\n<h4>\u5b9f\u884c\u7d50\u679c<\/h4>\n<p>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u307f\u305f\u307e\u3093\u307e\u3067\u3059\u304c\u3001CityName \u304c \u300c\u5ddd\u5d0e\u5e02\u5e78\u533a\u300d \u306e\u30c7\u30fc\u30bf\u3092 2\u4ef6 \u53d6\u5f97\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\nPretty-Print\u306b\u96e3\u5100\u3057\u305d\u3046\u3067\u3057\u305f\u306e\u3067\u3001\u305d\u306e\u307e\u307e\u306e\u7d50\u679c\u3067\u3059\uff08\u6c57\uff09<\/p>\n<pre>\n$ python3.10 get.py\n{'took': 171, 'timed_out': False, '_shards': {'total': 32, 'successful': 32, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 3183, 'relation': 'eq'}, 'max_score': 10.586664, 'hits': \n[\n{'_index': '428015190804::w18wju22mfs7a6z4czrb::SEARCH::zip-jp-index:0', '_id': 'LJn7BIUB67qpbBvGcUp7', '_score': 10.586664, '_source': {'zip7digits': '2120053', 'TownName': '\u4e0b\u5e73\u9593', 'CityName': '\u5ddd\u5d0e\u5e02\u5e78\u533a', 'PrefName': '\u795e\u5948\u5ddd\u770c'}}, \n{'_index': '428015190804::w18wju22mfs7a6z4czrb::SEARCH::zip-jp-index:0', '_id': 'BKv7BIUBuZbJeQ2ldv8H', '_score': 10.006503, '_source': {'zip7digits': '2120027', 'TownName': '\u65b0\u585a\u8d8a', 'CityName': '\u5ddd\u5d0e\u5e02\u5e78\u533a', 'PrefName': '\u795e\u5948\u5ddd\u770c'}}\n]}}\n...\n<\/pre>\n<p>\u53d6\u5f97\u3057\u305f\u3044\u7269\u3001\u5185\u5bb9\u306e\u4e8b\u524d\u78ba\u8a8d\u306b\u3064\u3044\u3066\u306f\u524d\u56de\u306e\u8a18\u4e8b\u3067\u3082\u5229\u7528\u3057\u305fDev Tools\u3092\u5229\u7528\u3057\u3066\u4e88\u3081\u7d50\u679c\u3092\u78ba\u8a8d\u3057\u306a\u304c\u3089\u5b9f\u88c5\u3059\u308b\u306e\u304c\u826f\u3055\u305d\u3046\u3067\u3059\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2022\/12\/100.Dashboard-UI-1024x706.png\" alt=\"\" width=\"728\" height=\"502\" class=\"aligncenter size-large wp-image-23114\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2022\/12\/100.Dashboard-UI-1024x706.png 1024w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2022\/12\/100.Dashboard-UI-300x207.png 300w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2022\/12\/100.Dashboard-UI-768x530.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2022\/12\/100.Dashboard-UI-1536x1060.png 1536w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2022\/12\/100.Dashboard-UI-2048x1413.png 2048w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2022\/12\/100.Dashboard-UI-728x502.png 728w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/p>\n<h2 id=\"summery\">\u307e\u3068\u3081<\/h2>\n<p>\u3053\u3053\u307e\u3067\u53d6\u5f97\u3067\u304d\u308c\u3070\u3001APIGateway + Lambda\u3067REST API\u5316\u7b49\u3082\u7c21\u5358\u306b\u5b9f\u73fe\u3067\u304d\u305d\u3046\u3067\u3059\u3002<br \/>\n\u3088\u308a\u826f\u3044\u5168\u6587\u691c\u7d22\u30e9\u30a4\u30d5\u3092\u9001\u308b\u305f\u3081\u3001GA\u304c\u5f85\u3061\u9060\u3057\u3044\u30b5\u30fc\u30d3\u30b9\u306e\u4e00\u3064\u3068\u306a\u308a\u307e\u3057\u305f\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AWS re:Invent 2022\u306b\u3066\u767a\u8868\u3055\u308c\u305f\u3001Amazon OpenSearch Serverless \u30d7\u30ec\u30d3\u30e5\u30fc\u7248\u3092\u5229\u7528\u3057Python\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u306e\u30c7\u30fc\u30bf\u6295\u5165\u3001OpenSearch UI\u3067\u306e\u691c\u7d22\u3092\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002&#8230;<\/p>\n","protected":false},"author":2,"featured_media":23087,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"https:\/\/www.skyarch.net\/blog\/?p=23102","footnotes":""},"categories":[20,1188,276],"tags":[],"class_list":{"0":"post-23102","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-aws","8":"category-aws-reinvent-2022","9":"category-serverless","10":"ja"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/23102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/comments?post=23102"}],"version-history":[{"count":3,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/23102\/revisions"}],"predecessor-version":[{"id":23115,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/23102\/revisions\/23115"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media\/23087"}],"wp:attachment":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media?parent=23102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/categories?post=23102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/tags?post=23102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}