如果你只是听说过es, 那么通过这篇文章, 你可以快速的知道怎么开始使用es

Install

1
2
3
curl -L -O http://download.elasticsearch.org/PATH/TO/VERSION.zip 
unzip elasticsearch-$VERSION.zip
cd  elasticsearch-$VERSION

包含常用插件 服务封装 中文分词 analysis- ik analysis- mmseg analysis- pinyin … 默认做好各种配置

Run

1
    ./elasticsearch
  • http,9200端口是否监听
1
netstat –ano|grep 9200
  • es通讯端口9300端口是否监听
1
netstat –ano|grep 9300
  • 如果使用了thrift,确认9500端口是否监听
1
netstat –ano|grep 9500
  • 查看ES进程
1
ps –aux|grep elasticsearch

Test As A K-V Database

  • CREATE
1
curl -XPUT http://localhost:9200/idx/tp/1 -d '{"name": "fyz", "age": 24}'
{"_index":"idx","_type":"tp","_id":"1","_version":1,"created":true}
  • GET
1
curl -XGET http://localhost:9200/idx/tp/1?pretty
{
  "_index" : "idx",
  "_type" : "tp",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source":{"name": "fyz", "age": 24}
}
  • DELETE
1
curl -XDELETE http://localhost:9200/idx/tp/1
{"found":true,"_index":"idx","_type":"tp","_id":"1","_version":2}
  • UPDATE
1
curl -XPUT http://localhost:9200/idx/tp/1 -d '{"name": "fyz", "age": 25}'

ES的内的数据都是immutable的, update是先delete, 然后create

  • SEARCH
1
2
3
curl -XPUT http://localhost:9200/idx/tp/2 -d '{"name": "testname", "age": 21}'
curl -XPUT http://localhost:9200/idx/tp/3 -d '{"name": "elasticsearch", "age": 28}'
curl -XGET http://localhost:9200/idx/tp/_search?pretty&q=name:fyz
{
  "took" : 19,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.30685282,
    "hits" : [ {
      "_index" : "idx",
      "_type" : "tp",
      "_id" : "1",
      "_score" : 0.30685282,
      "_source":{"name": "fyz", "age": 25}
    } ]
  }
}

Integrate With Mongo

1
bin/plugin --install com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/2.0.5
  • Mongo Replica
1
2
mongod --port 27017 --replSet rs_test --dbpath /data/db0 --oplogSize 100
mongod --port 27018 --replSet rs_test --dbpath /data/db1 --oplogSize 100
    cfg = {  
        _id: 'rs_test',  
        members: [  
            { _id: 0, host: 'localhost:27017' },  
            { _id: 1, host: 'localhost:27018' }
        ]  
    }
    rs.initiate(cfg)
  • Meta Config
1
2
3
4
5
6
7
8
9
10
11
 curl -XPUT 'http://localhost:9200/_river/test_mongo/_meta' -d '{
    "type": "mongodb", 
    "mongodb": { 
      "db": "DATABASE_NAME", 
      "collection": "COLLECTION"
    }, 
    "index": { 
      "name": "ES_INDEX_NAME", 
      "type": "ES_TYPE_NAME" 
    }
  }'

Cluster

  • Auto Discovery

Set same cluster.name

  • Unicast Discovery
cluster.name: mycluster
node.name: "node1"
node.master: true
node.data: true
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping.unicast.hosts: ["node1.example.com"]

Reference