elasticsearch5.5.1单机和集群安装2合1及elasticsearch的使用

特点:
1.倒排索引(也就是分词后做索引)
2.安装过程非常简单,解压缩就能用
3.没有明确主节点,搭建集群增加节点即可(集群最好3个或3个以上节点)
4.查询非常快速
安装过程如下:
说明:
1建议不要使用注册用户root,如果用root会启动不起来(除非进行配置)
2如果是搭建集群先同步时间 date -s "2018-09-30 13:57:00"同时发送到所有节点上
安装步骤
安装jdk1.7.0_55版本以上
下载elasticsearch5.5.1 (https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.tar.gz)
创建用户 bigdata(集群状态下需要多台设备创建用户,并且创建文件夹)

useradd bigdata

为用户添加密码

echo 123456 | passwd --stdin bigdata

将bigdata添加到sudoers

echo "bigdata ALL = (root) NOPASSWD:ALL" | tee /etc/sudoers.d/bigdata
chmod 0440 /etc/sudoers.d/bigdata

#解决sudo: sorry, you must have a tty to run sudo问题,在/etc/sudoer注释掉 Default requiretty 一行

sudo sed -i 's/Defaults    requiretty/Defaults:bigdata !requiretty/' /etc/sudoers

#创建一个bigdata目录

mkdir /{bigdata,data}

#给相应的目录添加权限

chown -R bigdata:bigdata /{bigdata,data}

切换到bigdata进行安装

su bigdata

解压缩(集群需要各个节点都解压缩)

tar zxf elasticsearch-5.5.1.tar.gz -C /bigdata/

创建日志文件夹

mkdir /bigdata/elasticsearch-5.5.1/logs

修改配置文件

vi /bigdata/elasticsearch-5.5.1/config/elasticsearch.yml

修改cluster.name:bigdata(说明cluster.name相同的会建立组播,形成集群)
修改node.name:node-1(说明如果是集群部署node.name必须不能相同)
node.rack(说明这个是设置机架,可以不用设置)
path.data:/data(设置数据保存路径)
path.logs:/bigdata/elasticsearch-5.5.1/logs(日志保存路径)
network.host:192.168.1.103(这台机器的ip地址,如果修改了hosts写主机名也可以,不建议写localhost)
http.port:9200(这个不需要修改,使用默认即可)
discovery.zen.ping.unicast.hosts:“192.168.1.103”
discovery.zen.minimum_master_nodes: 1 (使用1个master节点,几个master设置几个)
集群状态下设置完成后需要将/bigdata/elasticsearch-5.5.1文件发送到其他设备上,后修改node.name:node-1为其他
启动elasticsearch

/bigdata/elasticsearch-5.5.1/bin/elasticsearch -d

jps查看启动状况
用浏览器访问es所在机器9200端口
http://192.168.1.103:9200

{
  "name" : "node-1",
  "cluster_name" : "bigdata",
  "version" : {
    "number" : "5.5.1",
    "build_hash" : "bd980929010aef404e7cb0843e61d0665269fc39",
    "build_timestamp" : "2016-04-04T12:25:05Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.0"
  },
  "tagline" : "You Know, for Search"
}

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

#向store索引中添加一些书籍(添加用put,查找get,删除delete,修改update),下面的命令在centos上面输入就行
curl -XPUT ‘http://192.168.1.103:9200/store/books/1’ -d ‘{
“title”: “Elasticsearch: The Definitive Guide”,
“name” : {
“first” : “Zachary”,
“last” : “Tong”
},
“publish_date”:“2015-02-06”,
“price”:“49.99”
}’
#通过浏览器查询
http://192.168.1.103:9200/store/books/1
#在linux中通过curl的方式查询

curl -XGET 'http://192.168.1.103:9200/store/books/1'

#es安装插件下载es插件
/bigdata/elasticsearch-5.5.1/bin/plugin install mobz/elasticsearch-head

[hadoop@master es]$ bin/plugin install mobz/elasticsearch-head
-> Installing mobz/elasticsearch-head...
Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
Downloading ...........DONE
Verifying https://github.com/mobz/elasticsearch-head/archive/master.zip checksums if available ...
NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
Installed head into /export/servers/es/plugins/head

#本地方式安装head插件
./plugin install file:///home/bigdata/elasticsearch-head-master.zip

#访问head管理页面
http://192.168.1.103:9200/_plugin/head

或者通过 _update API的方式单独更新你想要更新的

curl -XPOST ‘http://172.16.0.14:9200/store/books/1/_update’ -d ‘{
“doc”: {
“price” : 88.88
}
}’

curl -XGET ‘http://172.16.0.14:9200/store/books/1

#删除一个文档
curl -XDELETE ‘http://172.16.0.14:9200/store/books/1

最简单filter查询

SELECT * FROM books WHERE price = 35.99

filtered 查询价格是35.99的

curl -XGET ‘http://172.16.0.14:9200/store/books/_search’ -d ‘{
“query” : {
“filtered” : {
“query” : {
“match_all” : {}
},
“filter” : {
“term” : {
“price” : 35.99
}
}
}
}
}’

#指定多个值
curl -XGET ‘http://172.16.0.14:9200/store/books/_search’ -d ‘{
“query” : {
“filtered” : {
“filter” : {
“terms” : {
“price” : [35.99, 99.99]
}
}
}
}
}’

SELECT * FROM books WHERE publish_date = “2015-02-06”

curl -XGET ‘http://172.16.0.14:9200/bookstore/books/_search’ -d ‘{
“query” : {
“filtered” : {
“filter” : {
“term” : {
“publish_date” : “2015-02-06”
}
}
}
}
}’

bool过滤查询,可以做组合过滤查询

SELECT * FROM books WHERE (price = 35.99 OR price = 99.99) AND (publish_date != “2016-02-06”)

类似的,Elasticsearch也有 and, or, not这样的组合条件的查询方式

格式如下:

{

“bool” : {

“must” : [],

“should” : [],

“must_not” : [],

}

}

must: 条件必须满足,相当于 and

should: 条件可以满足也可以不满足,相当于 or

must_not: 条件不需要满足,相当于 not

curl -XGET ‘http://172.16.0.14:9200/bookstore/books/_search’ -d ‘{
“query” : {
“filtered” : {
“filter” : {
“bool” : {
“should” : [
{ “term” : {“price” : 35.99}},
{ “term” : {“price” : 99.99}}
],
“must_not” : {
“term” : {“publish_date” : “2016-02-06”}
}
}
}
}
}
}’

嵌套查询

SELECT * FROM books WHERE price = 35.99 OR ( publish_date = “2016-02-06” AND price = 99.99 )

curl -XGET ‘http://172.16.0.14:9200/bookstore/books/_search’ -d ‘{
“query” : {
“filtered” : {
“filter” : {
“bool” : {
“should” : [
{ “term” : {“price” : 35.99}},
{ “bool” : {
“must” : [
{“term” : {“publish_date” : “2016-02-06”}},
{“term” : {“price” : 99.99}}
]
}}
]
}
}
}
}
}’

range范围过滤

SELECT * FROM books WHERE price >= 20 AND price < 100

gt : > 大于

lt : < 小于

gte : >= 大于等于

lte : <= 小于等于

curl -XGET ‘http://172.16.0.14:9200/store/books/_search’ -d ‘{
“query” : {
“filtered” : {
“filter” : {
“range” : {
“price” : {
“gt” : 20.0,
“lt” : 100
}
}
}
}
}
}’

另外一种 and, or, not查询

没有bool, 直接使用and , or , not

注意: 不带bool的这种查询不能利用缓存

查询价格既是35.99,publish_date又为"2015-02-06"的结果

curl -XGET ‘http://172.16.0.14:9200/bookstore/books/_search’ -d ‘{
“query”: {
“filtered”: {
“filter”: {
“and”: [
{
“term”: {
“price”:59.99
}
},
{
“term”: {
“publish_date”:“2015-02-06”
}
}
]
},
“query”: {
“match_all”: {}
}
}
}
}’

http://172.16.0.14:9200/bookstore/books/_search

#es安装插件下载es插件
/bigdata/elasticsearch-2.3.1/bin/plugin install elasticsearch/marvel/latest
#访问head管理页面
http://172.16.0.14:9200/_plugin/marvel

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值