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

看下日志,哦啦!

暫時就研究了那么多,更加深入的待續......


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM