zoukankan      html  css  js  c++  java
  • Docker + ElasticSearch + Node.js

    最近有空就想研究下ElasticSearch。

    此篇文章用来记录研究过程。备注:需要有一定的docker基础,ElasticSearch的基本概念

    Docker安装ElasticSearch

    首先,就是安装ElasticSearch。 因为有了docker,所以就不用按部就班的安装,直接通过下载ElasticSearch的镜像源就搞定。

    理想是美好的,现实是残酷的。因为从国外拉取镜像太慢,我选择了国内的时速云。结果搜索ElasticSearch排名第一的镜像把我坑惨了,死活连不上。

    只能慢慢找对应官网的镜像,我只是想吐槽下国内...国内...

    1.下载镜像

    docker pull index.tenxcloud.com/docker_library/elasticsearch:1.6

    2. 开启镜像并映射端口9200(ElasticSearch的默认端口为9200)

    docker run -p 9200:9200 -d index.tenxcloud.com/docker_library/elasticsearch:1.6

    备注:如果是mac的话,还需要多在此之前多做一个端口映射的动作,具体可参照http://unmi.cc/mac-os-x-experience-docker/中的端口映射,

    里面的流程图也说明了为何mac需要再多做一步。

    3.测试是否安装成功,并且能够连通

    curl 127.0.0.1:9200

    这时候,会看到正常返回:

    {
      "status" : 200,
      "name" : "James "Jimmy" Marks",
      "cluster_name" : "elasticsearch",
      "version" : {
        "number" : "1.6.2",
        "build_hash" : "622039121e53e5f520b5ff8720fdbd3d0cb5326b",
        "build_timestamp" : "2015-07-29T09:24:47Z",
        "build_snapshot" : false,
        "lucene_version" : "4.10.4"
      },
      "tagline" : "You Know, for Search"
    }

    Elasticsearch 

    与Elasticsearch交互

    任何其他语言都可以使用你喜欢的网页客户端可以访问的RESTful API通过9200端口和 Elasticsearch通信。实际上,你甚至可以从命令行通过curl命令(当然你要去了解一下curl命令)和Elasticsearch通信。

    curl -XGET 'http://localhost:9200/_count?pretty' 
    -d '
    {  
       "query": {
            "match_all": {}  
        }
    }
    '

    说明:

    -XGET适当的HTTP方法或者动作 : GET、POST、PUT、HEAD或者DELETE;

    http:.........:9200表示集群任意节点的协议、主机名和端口;

    _count表示请求的路径;

    pretty任意可选的查询字符串参数,比如pretty将会漂亮的打印JSON格式的响应使它更容易阅读;

     -d表示 HTTP POST方式传输数据;

     {}中的部分表示JSON格式的请求包体(我们后面会常用这种形式);

     query表示JSON格式的请求包体中的查询关键字;

     match_all表示JSON格式的请求包体中的要查询的字段。Elasticsearch返回一个像 200 OK 的状态码和一个JSON格式的响应(HEAD请求除外)。 上面的curl请求将返回一个如下的JSON格式的响应:

    {
        "count" : 0,
        "_shards" : {
            "total" : 5,
            "successful" : 5,
            "failed" : 0
        }
    }

    建立索引

    Elasticsearch中建立一个索引来存储数据。比如我创建的索引是article。

    curl -XPUT 'http://localhost:9200/article/'  

    返回结果为:

    {"acknowledged":true}

    建立mapping

    我们已经建立索引名为article的索引,我们在这将对article中的内容进行约束,进行验证。从而在存取数据时按照我们预定的规则进行存储。也就是我们在这里要建立article的mapping。下面代码是建立索引为article,索引类型为detail的mapping

    curl -XPUT 'http://localhost:9200/article/_mapping/detail' -d ' 
    {
            "detail" : {
                "dynamic" : true,
                "properties" : {
                    "title" : { "type" :  "string"  },
                    "url"   : { "type" :  "string" },
                    "content"   : { "type" :  "string" }
                }
            }
        }
    '

    返回结果为:

    {"acknowledged":true}

    数据保存

    curl -XPOST  'http://localhost:9200/article/detail' -d '{
        "title":"hello world!",
        "url": "http://xxxx.com",
        "content":"this is a test"
    }'

    返回结果:

    {"_index":"article","_type":"detail","_id":"AVYrA6DFR3LFkvR34Ega","_version":1,"created":true}

    ES数据检索

    curl -XGET 'http://localhost:9200/article/detail/_search' -d '
     {
        "query":
           {
           "match":
             {"content":"test"}
           }
      }'

    返回结果:

    {"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.15342641,"hits":[{"_index":"article","_type":"detail","_id":"AVYrA6DFR3LFkvR34Ega","_score":0.15342641,"_source":{
        "title":"hello world!",
        "url": "http://xxxx.com",
        "content":"this is a test"
    }}]}}

     Node.js

    node.js也有相关的elasticsearch包。

    首先安装下:

    npm install elasticsearch

    初始化:

    var elasticsearch = require('elasticsearch');
    var client = new elasticsearch.Client({
      host: 'localhost:9200',
      log: 'trace'
    });

    试试下,我们通过node查找刚刚插入的数据

    client.search({
      index: 'article',
      type: 'detail',
      body: {
        query: {
          match: {
            content: 'test'
          }
        }
      }
    }).then(function (resp) {
        var hits = resp.hits.hits;
    }, function (err) {
        console.trace(err.message);
    });

    看下日志,哦啦!

    暂时就研究了那么多,更加深入的待续......

  • 相关阅读:
    crontab定时任务写法记录
    Django与Vue语法冲突问题完美解决方法
    Django下MEDIA_ROOT, MEDIA_URL, STATIC_ROOT, STATIC_URL解惑
    解决python2.x用urllib2证书验证错误, _create_unverified_context
    django 异步任务实现及Celery beat实现定时/轮询任务
    用Python写WebService接口并且调用
    django(权限、认证)系统——第三方组件实现Object级别权限控制
    django(权限、认证)系统—— 基于Authentication backends定制
    django(权限、认证)系统—— Permissions和Group
    [jmeter]linux下自动测试环境+持续集成ant+jmeter+Apache(httpd)环境搭建与使用
  • 原文地址:https://www.cnblogs.com/showtime813/p/5710829.html
Copyright © 2011-2022 走看看