Tech Tips

  1. プログラミング
  2. 1958 view

ElasticSearch にクライアントサイドjavascriptからクエリを投げてみる

Contents

作業環境

Ubuntu 12.04 LTS

関連情報

インストール方法

概要

20140308-es-outline

テストデータを準備

インデックスの作成

curl -XPOST 'http://localhost:9200/test' -d '{
  "settings": {
    "index": {
      "mapping.allow_type_wrapper": true
    }
  }
}'

> {"ok":true,"acknowledged":true}

タイプの作成

curl -XPUT 'http://localhost:9200/test/barley/_mapping' -d '{
    "barley" : {
        "properties" : {
            "place" : {"type" : "string", "index" : "not_analyzed"},
            "ha" : {"type" : "integer"},
            "kg" : {"type" : "integer"},
            "t" : {"type" : "integer"}
        }
    }
}'

> {"ok":true,"acknowledged":true}

テストデータのインサート

curl -X PUT http://localhost:9200/test/barley/1 -d '{
  "barley" : {
     "place":"北海道",
     "ha":1990,
     "kg":337,
     "t":6710}
  }
}'

> {"ok":true,"_index":"test","_type":"barley","_id":"1","_version":1}

検索のテスト

curl -X GET http://localhost:9200/test/barley/_search -d '
{
  "query": {
    "match" : {"place" : "北海道"}
  }
}'

>{
>    "_shards": {
>        "failed": 0,
>        "successful": 5,
>        "total": 5
>    },
>    "hits": {
>        "hits": [
>            {
>                "_id": "1",
>                "_index": "test",
>                "_score": 0.30685282,
>                "_source": {
>                    "barley": {
>                        "ha": 1990,
>                        "kg": 337,
>                        "place": "北海道",
>                        "t": 6710
>                    }
>                },
>                "_type": "barley"
>            }
>        ],
>        "max_score": 0.30685282,
>        "total": 1
>    },
>    "timed_out": false,
>    "took": 114
>}

JSの作成

ディレクトリ構成

.
├── scripts
│   ├── d3.v3.js
│   ├── elasticsearch.js
│   ├── main.js
│   └── require.js
└── test.html

ライブラリのダウンロード

.
├── scripts
│   ├── d3.v3.js           : wget http://d3js.org/d3.v3.min.js
│   ├── elasticsearch.js :  wget https://download.elasticsearch.org/elasticsearch/elasticsearch-js/elasticsearch-js-1.5.10.zip
│   ├── main.js
│   └── require.js         :  wget  http://requirejs.org/docs/release/2.1.11/minified/require.js
└── test.html

test.htmlの作成

<!DOCTYPE>
<html>
        <head>
                <script data-main="scripts/main" src="scripts/require.js"></script>
                <script>require(["main"], function () {})</script>
        </head>
        <body>
        </body>
</html>

main.jsの作成

define(['d3.v3', 'elasticsearch'], function (d3, elasticsearch) {
    "use strict";
    var client = new elasticsearch.Client({hosts:'sample.com:9200'});

    client.search({
        index: 'test',
        size: 1,
        body: {
            query: {
                bool: {
                    must: { match: { "place": "北海道" }},
                }
            }
        }
    }).then(function (resp) {
        console.log(resp);
        console.log(resp.hits.hits[0]._source.barley);
    });
});
あとはブラウザで作成したHTMLにアクセスすると結果が確認できる。

プログラミングの最近記事

  1. PlatformIO IDE for VSCode を使用して VSCode で Ardu…

  2. ROS Docker イメージで発生した GPG error の解消方法

  3. Streamlit で訪れた国を色づけした世界地図を作成できるアプリケーションを作成してみ…

  4. M5Stack Core2 for AWS – ESP32 IoT開発キットで…

  5. D3.js v7 で点・線・テキスト・ツールチップ・ズームを設定する方法

関連記事

PAGE TOP