elasticsearch集群安装与使用

来源:互联网 发布:马东眼袋在哪割的 知乎 编辑:程序博客网 时间:2024/06/05 04:45

说明

版本:2.3.4
Elasticsearch requires at least Java 7. Specifically as of this writing, it is recommended that you use the Oracle JDK version 1.8.0_73. Java installation varies from platform to platform so we won’t go into those details here. Oracle’s recommended installation documentation can be found on Oracle’s website. Suffice to say, before you install Elasticsearch, please check your Java version first by running (and then install/upgrade accordingly if needed)
见官网

es启动时需要使用非root用户,本文以bingo用户安装
不依赖zookeeper, 有自己的选举机制

安装

在master01节点上
解压

tar -zxvf elasticsearch-2.3.4.tar.gz或tar -zxvf elasticsearch-2.3.4.tar.gz -C /home/bingo/software/

修改配置

vim config/elasticsearch.yml#集群用户名称,通过组播的方式通信,通过名称判断属于哪个集群cluster.name: bingo#节点名称,要唯一node.name: es-1#数据存放位置path.data: /home/bingo/data/es/data#日志存放位置path.logs: /home/bingo/data/es/logs#es绑定的ip地址network.host: master01#初始化时可进行选举的节点discovery.zen.ping.unicast.hosts: ["master01", "worker01", "worker01"]

拷贝到其他节点
scp -r elasticsearch-2.3.4/ worker01:PWDscprelasticsearch2.3.1/worker02:PWD

在其他节点(worker01,worker02)上修改es配置
需要修改:node.name和network.host

#worker01node.name: es-2network.host: worker01#worker02node.name: es-3network.host: worker02

启动

分别启动 3 个节点

bin/elasticsearch -d

浏览器访问

es所在机器的9200端口

http://master01:9200/#浏览器输出{  "name" : "es-1",  "cluster_name" : "bingo",  "version" : {    "number" : "2.3.4",    "build_hash" : "e455fd0c13dceca8dbbdbb1665d068ae55dabe3f",    "build_timestamp" : "2016-06-30T11:24:31Z",    "build_snapshot" : false,    "lucene_version" : "5.5.0"  },  "tagline" : "You Know, for Search"}

到此,安装成功

使用

RESTful接口URL的格式:
http://localhost:9200/<index>/<type>/[<id>]
其中index、type是必须提供的。
id是可选的,不提供es会自动生成。
index、type将信息进行分层,利于管理。
index可以理解为数据库;type理解为数据表;id相当于数据库表中记录的主键,是唯一的。

添加

向store索引中添加一些书籍

#在linux中执行curl -XPUT 'http://master01:9200/store/books/1' -d '{  "title": "spark: include scala and java",  "name" : {    "first" : "spark",    "last" : "scala"  },  "publish_date":"2017-03-26",  "price":"19.99"}'#再添加一本书curl -XPUT 'http://master01:9200/store/books/2' -d '{  "title": "hadoop:no scala support",  "name" : {    "first" : "hadoop",    "last" : "java"  },  "publish_date":"2014-03-26",  "price":"9.99"}'

查询

#浏览器访问http://master01:9200/store/books/1#在linux中通过curl的方式查询curl -XGET 'http://master01:9200/store/books/1'#输出{"_index":"store","_type":"books","_id":"1","_version":1,"found":true,"_source":{  "title": "spark: include scala and java",  "name" : {    "first" : "spark",    "last" : "scala"  },  "publish_date":"2017-03-26",  "price":"19.99"}}

通过_source获取指定的字段

curl -XGET 'http://master01:9200/store/books/1?_source=title'#输出{"_index":"store","_type":"books","_id":"1","_version":2,"found":true,"_source":{"title":"spark: include scala and java"}}curl -XGET 'http://master01:9200/store/books/1?_source=title,price'#输出{"_index":"store","_type":"books","_id":"1","_version":2,"found":true,"_source":{"price":"19.99","title":"spark: include scala and java"}}curl -XGET 'http://master01:9200/store/books/1?_source'#输出{"_index":"store","_type":"books","_id":"1","_version":2,"found":true,"_source":{  "title": "spark: include scala and java",  "name" : {    "first" : "spark",    "last" : "scala"  },  "publish_date":"2017-03-26",  "price":"19.99"}}

更新

通过覆盖的方式更新
写法跟添加一样,覆盖原来的值

通过 _update API的方式更新

curl -XPOST 'http://master01:9200/store/books/2/_update' -d '{  "doc": {     "price" : 88.88  }}'

删除

curl -XDELETE 'http://master01:9200/store/books/1'#再查询会返回{"_index":"store","_type":"books","_id":"1","found":false}

filter查询

#等值 where price=""curl -XGET 'http://master01:9200/store/books/_search' -d '{    "query" : {        "filtered" : {            "query" : {                "match_all" : {}            },            "filter" : {                "term" : {                    "price" : 19.99                  }              }        }    }}'#指定多个值 where price in (9.99, 19.99)curl -XGET 'http://master01:9200/store/books/_search' -d '{    "query" : {        "filtered" : {            "filter" : {                "terms" : {                    "price" : [9.99, 19.99]                  }              }        }    }}'# bool过滤查询,可以做组合过滤查询# SELECT * FROM books WHERE (price = 9.99 OR price = 19.99) AND (publish_date != "2014-03-26")# 类似的,Elasticsearch也有 and, or, not这样的组合条件的查询方式# 格式如下:#  {#    "bool" : {#    "must" :     [],#    "should" :   [],#    "must_not" : [],#    }#  }## must: 条件必须满足,相当于 and# should: 条件可以满足也可以不满足,相当于 or# must_not: 条件不需要满足,相当于 notcurl -XGET 'http://master01:9200/store/books/_search' -d '{  "query" : {    "filtered" : {      "filter" : {        "bool" : {          "should" : [            { "term" : {"price" : 9.99}},            { "term" : {"price" : 19.99}}          ],          "must_not" : [            { "term" : {"publish_date" : "2016-03-26"}}          ]        }      }    }  }}'# 嵌套查询# SELECT * FROM books WHERE price = 9.99 OR ( publish_date = "2017-03-26" AND price = 19.99 )curl -XGET 'http://master01:9200/store/books/_search' -d '{  "query" : {    "filtered" : {      "filter" : {        "bool" : {          "should" : [              { "term" : {"price" : 9.99}},              { "bool" : {              "must" : [                {"term" : {"publish_date" : "2017-02-06"}},                {"term" : {"price" : 19.99}}              ]            }}          ]        }      }    }  }}'# range范围过滤# SELECT * FROM books WHERE price >= 10 AND price < 100# gt :  > 大于# lt :  < 小于# gte :  >= 大于等于# lte :  <= 小于等于curl -XGET 'http://master01:9200/store/books/_search' -d '{  "query" : {    "filtered" : {      "filter" : {        "range" : {          "price" : {            "gt" : 10,            "lt" : 100          }        }      }    }  }}'# 另外一种 and, or, not查询# 没有bool, 直接使用and , or , not# 注意: 不带bool的这种查询不能利用缓存# 查询价格既是9.99,publish_date又为"2014-03-26"的结果curl -XGET 'http://master01:9200/store/books/_search' -d '{  "query": {    "filtered": {      "filter": {        "and": [        {          "term": {            "price":9.99          }        },        {          "term": {            "publish_date":"2014-03-26"          }        }       ]     },     "query": {      "match_all": {}      }    }  }}'

插件

本地方式安装head插件

bin/plugin install file:///home/bingo/elasticsearch-head-master.zip

访问head管理页面

http://master01:9200/_plugin/head
0 0
原创粉丝点击