プログラミング + アカデミック + 何か面白いこと

  1. Programming
  2. 401 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.

Programming recent post

  1. Install sbt 1.0.0 and run sample template

  2. [Machine Learning]Created docker image includ…

  3. [Node.js]How to write batch script with Node.…

  4. [Play][Scala]Develop Request Driven Batch Usi…

  5. [OpenCV][Ruby]Auto check web page design corr…

関連記事

PAGE TOP