{"id":1137,"date":"2020-12-15T16:48:36","date_gmt":"2020-12-15T07:48:36","guid":{"rendered":"http:\/\/leenux.kr\/?p=1137"},"modified":"2021-01-29T14:14:10","modified_gmt":"2021-01-29T05:14:10","slug":"flutter-elasticsearch-%ec%95%b1-%ed%86%b5%ec%8b%a0","status":"publish","type":"post","link":"https:\/\/leenux.kr\/?p=1137","title":{"rendered":"[Flutter] Elasticsearch \uc571 \ud1b5\uc2e0 \ubc29\ubc95\ub4e4"},"content":{"rendered":"\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>Flutter\uc5d0\uc11c Elasticsearch\ub97c \ud1b5\ud574 \ub0b4 \uc8fc\ubcc0\uc5d0 \uc788\ub294 \uc81c\uc77c \uac00\uae4c\uc6b4 \uc5ed\uc774 \uc5b4\ub514\uc778\uc9c0 \uad6c\ud604\ud574\ubcf4\uaca0\ub2e4.<\/strong><\/p>\n\n\n\n<p class=\"has-text-color has-vivid-red-color\">\ud604\uc7ac \ub0b4\uc704\uce58\ub294 \uc2e4\uc81c \uac12\uc774 \uc544\ub2cc \uac00\uc815\ud55c \uac12\uc744 \uc0ac\uc6a9\ud568.<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<p class=\"has-text-color has-large-font-size has-very-dark-gray-color\"><strong>Elasticsearch \ub370\uc774\ud130 \uc900\ube44<\/strong><\/p>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>Mapping<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PUT my_geo\n{\n  \"mappings\": {\n    \"properties\": {\n      \"location\": {\n        \"type\": \"geo_point\"\n      }\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>\ubc8c\ud06c\ub85c \ub370\uc774\ud130 \uc0bd\uc785<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PUT my_geo\/_bulk\n{\"index\":{\"_id\":\"1\"}}\n{\"station\":\"\uac15\ub0a8\",\"location\":{\"lon\":127.027926,\"lat\":37.497175},\"line\":\"2\ud638\uc120\"}\n{\"index\":{\"_id\":\"2\"}}\n{\"station\":\"\uc885\ub85c3\uac00\",\"location\":{\"lon\":126.991806,\"lat\":37.571607},\"line\":\"3\ud638\uc120\"}\n{\"index\":{\"_id\":\"3\"}}\n{\"station\":\"\uc5ec\uc758\ub3c4\",\"location\":{\"lon\":126.924191,\"lat\":37.521624},\"line\":\"5\ud638\uc120\"}\n{\"index\":{\"_id\":\"4\"}}\n{\"station\":\"\uc11c\uc6b8\uc5ed\",\"location\":{\"lon\":126.972559,\"lat\":37.554648},\"line\":\"1\ud638\uc120\"}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>elastic_client Package\ub97c \ud1b5\ud55c elasticsearch \ud1b5\uc2e0 \ubc29\ubc95 <\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/pub.dev\/packages\/elastic_client\">https:\/\/pub.dev\/packages\/elastic_client<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>elastic_client: ^0.2.1<\/code><\/pre>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>\uc2e4\ud589\ud560 \ubd80\ubd84\uc5d0 \ud574\ub2f9 \ucf54\ub4dc<\/strong> <strong>Elasticsearch IP\ubd80\ubd84\ub9cc \uc218\uc815\ud558\uace0 \ucd94\uac00<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@override\n  void initState() {\n      Uri uri = Uri.parse(\"http:\/\/[hostIP]:9200\/\"); \/\/\/ elasticsearch IP\n      final config = HttpTransport(url: uri); \n      final client = Client(config);\n      SearchResult result = await client.search( \n          index: \"my_geo\",\n              query: {\n                 \"geo_distance\": {\n                     \"distance\": \"5km\",\n                     \"location\": {\n                         \"lat\": 37.5358,\n                         \"lon\": 126.9559\n                     }\n                }\n              }\n      );\n     }\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-vivid-red-color\"><strong>\uc571 \uac80\uc0c9 \uacb0\uacfc<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"68\" src=\"https:\/\/leenux.kro.kr\/wp-content\/uploads\/2020\/12\/\u1109\u1173\u110f\u1173\u1105\u1175\u11ab\u1109\u1163\u11ba-2020-12-15-\u110b\u1169\u1112\u116e-4.30.12-1024x68.png\" alt=\"\" class=\"wp-image-1138\" srcset=\"https:\/\/leenux.kr\/wp-content\/uploads\/2020\/12\/\u1109\u1173\u110f\u1173\u1105\u1175\u11ab\u1109\u1163\u11ba-2020-12-15-\u110b\u1169\u1112\u116e-4.30.12-1024x68.png 1024w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/12\/\u1109\u1173\u110f\u1173\u1105\u1175\u11ab\u1109\u1163\u11ba-2020-12-15-\u110b\u1169\u1112\u116e-4.30.12-300x20.png 300w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/12\/\u1109\u1173\u110f\u1173\u1105\u1175\u11ab\u1109\u1163\u11ba-2020-12-15-\u110b\u1169\u1112\u116e-4.30.12-768x51.png 768w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/12\/\u1109\u1173\u110f\u1173\u1105\u1175\u11ab\u1109\u1163\u11ba-2020-12-15-\u110b\u1169\u1112\u116e-4.30.12-830x55.png 830w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/12\/\u1109\u1173\u110f\u1173\u1105\u1175\u11ab\u1109\u1163\u11ba-2020-12-15-\u110b\u1169\u1112\u116e-4.30.12-230x15.png 230w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/12\/\u1109\u1173\u110f\u1173\u1105\u1175\u11ab\u1109\u1163\u11ba-2020-12-15-\u110b\u1169\u1112\u116e-4.30.12-350x23.png 350w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/12\/\u1109\u1173\u110f\u1173\u1105\u1175\u11ab\u1109\u1163\u11ba-2020-12-15-\u110b\u1169\u1112\u116e-4.30.12-480x32.png 480w, https:\/\/leenux.kr\/wp-content\/uploads\/2020\/12\/\u1109\u1173\u110f\u1173\u1105\u1175\u11ab\u1109\u1163\u11ba-2020-12-15-\u110b\u1169\u1112\u116e-4.30.12.png 1132w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption><br><strong>\uac80\uc0c9 \uacb0\uacfc<\/strong><\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<p class=\"has-text-color has-medium-font-size has-very-dark-gray-color\"><strong>Dio \ud328\ud0a4\uc9c0\ub97c \ud1b5\ud55c Elasticsearch \ud1b5\uc2e0 \ubc29\ubc95<\/strong><\/p>\n\n\n\n<p class=\"has-text-color has-very-dark-gray-color\"><strong>dio \ud328\ud0a4\uc9c0 \uc124\uce58<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/pub.dev\/packages\/dio\/install\">https:\/\/pub.dev\/packages\/dio\/install<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dio: ^3.0.10<\/code><\/pre>\n\n\n\n<p class=\"has-text-color has-very-dark-gray-color\"><strong>x-pack \ubcf4\uc548\uc774 \ube44\ud65c\uc131\ud654 \ub418\uc5b4 \uc788\uc744 \uacbd\uc6b0 \ud5e4\ub354 Authorization, \ub9e4\uac1c\ubcc0\uc218 \ubd80\ubd84 \uc8fc\uc11d \ub610\ub294 \uc0ad\uc81c \ud544\uc218<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'dart:convert' as convert;\nimport 'dart:convert';\n\nimport 'package:dio\/dio.dart';\n\nclass ElasticsearchPost{\n\n  Future&lt;Response> post({String username, String password}) async {\n    final up = convert.utf8.encode('$username:$password');\n    final basicAuthorization = 'Basic ${convert.base64Encode(up)}';\n    return await Dio().postUri(\n      Uri.parse(\"http:\/\/[hostIP]\/_sql?format=json\",),\n      data: {\n        \"query\": \"\"\"\n        SELECT * FROM \"hagwon\" limit 1\n        \"\"\"\n      },\n      options: Options(\n        headers: {\n          'Content-Type' : 'application\/json',\n          'Authorization' : basicAuthorization,\n        }\n      )\n    );\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-color has-very-dark-gray-color\"><strong>x-pack \ubcf4\uc548\uc774 \ube44\ud65c\uc131\ud654 \ub418\uc5b4 \uc788\uc744 \uacbd\uc6b0 <\/strong>\ub9e4\uac1c\ubcc0\uc218 \uc0ad\uc81c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'package:blog\/Test\/elasticsearchTest.dart';\nimport 'package:flutter\/material.dart';\nimport 'package:dio\/dio.dart';\n\nclass ElasticSearchTestPage extends StatefulWidget {\n  @override\n  _ElasticSearchTestPageState createState() => _ElasticSearchTestPageState();\n}\n\nclass _ElasticSearchTestPageState extends State&lt;ElasticSearchTestPage> {\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      body: Container(\n        child: FutureBuilder(\n          future: ElasticsearchPost().post(username: \"user\", password: \"password\"),\n          builder: (BuildContext context, AsyncSnapshot&lt;Response> _) {\n            if(!_.hasData){\n              return Center(\n                child: CircularProgressIndicator(),\n              );\n            }\n            var a = _.data.data;\n            print(a);\n            return Container(\n              alignment: Alignment.center,\n              child: Text(\"${a}\", textAlign: TextAlign.center,),\n            );\n          },\n        ),\n      ),\n    );\n  }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>[Flutter] Elasticsearch \uc571 \ud1b5\uc2e0 \uc608\uc81c<\/p>\n","protected":false},"author":1,"featured_media":1001,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,3],"tags":[],"_links":{"self":[{"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/posts\/1137"}],"collection":[{"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1137"}],"version-history":[{"count":3,"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/posts\/1137\/revisions"}],"predecessor-version":[{"id":1314,"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/posts\/1137\/revisions\/1314"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=\/wp\/v2\/media\/1001"}],"wp:attachment":[{"href":"https:\/\/leenux.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/leenux.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}