Tech Tips

  1. Uncategorized
  2. 126 view

Issue query with client side javascript to ElasticSearch

Environment

Ubuntu 12.04 LTS

関連情報

How to install

Outline

20140308-es-outline-eng

Prepare data

Make index

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

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

Make type

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}

Insert test data

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}

Search test

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
>}

Development

Directory organization

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

Download libraries

.
├── 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

Develop test.html

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

Develop 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);
    });
});
You can see the result when you access to the test.html with browser.

Uncategorized recent post

  1. Run Amazon FreeRTOS on M5Stack Core2 for AWS …

  2. Udacity Self-Driving Car Engineer Nanodegree …

  3. Install sbt 1.0.0 and run sample template

  4. Visualization of Neural Network and its Train…

  5. [Machine Learning]Created docker image includ…

関連記事

PAGE TOP