Elasticsearch[es]的命令总结

http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html
https://github.com/elastic/elasticsearch
什么是Elasticsearch??
Elasticsearch是一个实时的分布式搜索分析引擎,以一个之前从未有过的速度和规模,去探索数据
Elasticsearch是一个开源的搜索引擎,建立在一个全文搜索引擎库Apache Lucene基础之上;Lucene可以说是当下最先进,高性能,全功能的搜索引擎库[Lucene仅仅只是一个库,为了充分发挥其功能,需要使用java并将Lucene直接集成到应用程序中]

Elasticsearch不仅仅是Lucene,不仅仅只是一个全文搜索引擎:
1.一个分布式的实时文档存储,每个字段可以被索引与搜索
2.一个分布式实时分析搜索引擎
3.能胜任上百个服务节点的扩展,并支持PB级别的结构化或者非结构化数据

Elasticsearch安装:
[在多台机器上执行下面的命令]
环境准备:
1.es启动时需要使用非root用户,所以创建一个bigdata用户;[博主举例创建dance用户]
useradd dance
2.为dance用户添加密码:
echo 123456 | password –stdin bigdata
3.将dance添加到home
echo “dance ALL = (root) NOPASSWD:ALL” | tee /home/dance
chmod 0440 /home/dance
**.解决sudo:sorry,you must have a tty to run sudo问题,在/home注释掉Default requiretty 一行
sudo sed -i ‘s/Defaults requiretty/Defaults:dance !requiretty/’ /home
4.切换用户
su - dance

5.创建一个dance目录
mkdir /{bigdata,mydata}
给相应的目录添加权限
chown -R dance:dance / {bigdata,mydata}
这里写图片描述
出现权限问题,需要exit到root创建
这里写图片描述
第一个与最后一个在root下创建成功,并已经使用了命令:
chown dance:dance bigdata
chown dance:dance mydata

切换到dance用户安装
1.安装jdk[jdk要求1.8.20或1.7.55以上]
2.上传es安装包
3.解压es
tar -zxvf elasticsearch-2.3.1.tar.gz -C /home/dance
4.elasticsearch-2.3.1目录下创建plugins文件夹并添加权限
mkdir plugins
chown -R dance:dance plugins
5.安装plugins插件[后面也有详细说明]
bin/plugin install mobz/elasticsearch-head
6.修改配置
vi /elasticsearch-2.3.1/config/elasticsearch.yml
#集群名称,通过组播的方式通信,通过名称判断属于哪个集群
cluster.name: dance
#节点名称,要唯一
node.name: es-1
#数据存放位置
path.data: /mydata/es/data
#日志存放位置
path.logs: /mydata/es/logs
#es绑定的IP地址
network.host: 192.168.222.156
#初始化时可进行选举的节点
discovery.zen.ping.unicast.hosts: [“minimaster”,”miniSlave1”,”miniSlave2”]
7.使用scp拷贝到其他节点[每个节点都需要通过环境准备的环节]
scp -r elasticsearch-2.3.1 miniSlave1: PWDscprelasticsearch2.3.1miniSlave2: PWD
8.在其他节点上修改es配置,需要修改的有node.name和network.host
9.启动es
/elasticsearch-2.3.1/bin/elasticsearch -d
10.在浏览器访问es所在机器的9200端口:
http://192.168.222.156:9200/

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

#本地方式安装head插件[上传压缩包]
./plugin install file:///home/dance/elasticsearch-head-master.zip

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

向store索引中添加一些书籍
curl -XPUT ‘http://192.168.222.156/store/books/1’ -d ‘{
“title”:”Elasticsearch: The Definitive Guide”,
“name”: {
“first”: “Zachary”,
“last”: “Tong”
},
“publish_date”:”2015-02-06”,
“price”: “49.99”
}’
这里写图片描述

#在linux中通过curl的方式查询
curl -XGET ‘http://192.168.222.156:9200/store/books/1
执行结果:
{“_index”:”store”,”_type”:”books”,”_id”:”1”,”_version”:3,”found”:true,”_source”:{“title”:”Elasticsearch: The Definitive Guide”,”name”:{“first”:”Zachary”,”last”:”Tong”},”publish_date”:”2016-02-06”,”price”:88.88}}

#通过_source获取指定的字段

 curl -XGET 'http://192.168.222.156:9200/store/books/1?_source=title,price'

执行结果:
{“_index”:”store”,”_type”:”books”,”_id”:”1”,”_version”:3,”found”:true,”_source”:{“title”:”Elasticsearch: The Definitive Guide”,”price”:88.88}

curl -XGET 'http://192.168.222.156:9200/store/books/1?_source'

执行结果:
“_index”:”store”,”_type”:”books”,”_id”:”1”,”_version”:3,”found”:true,”_source”:{“title”:”Elasticsearch: The Definitive Guide”,”name”:{“first”:”Zachary”,”last”:”Tong”},”publish_date”:”2016-02-06”,”price”:88.88}

#可以通过覆盖的方式更新(执行所有的语句,并在其中进行修改属性值)
#或者通过_update API的方式单独更新你想要更新的

 curl -XPOST 'http://192.168.222.156:9200/store/books/1/_update' -d '{
 "doc" :{
    "price":77.77
    }
 }'

#最简单filter查询
#select * from books where price = 77.77
#filtered查询价格是77.77的

curl -XGET 'http://192.168.222.156:9200/store/books/_search' -d '{
   "query": {
     "filtered" : {
         "query":{
            "match_all" :{}
            },
         "filter" : {
            "term" :{
            "price" : 77.77
            }         
         }        
     }
    }
 }'

bool过滤查询,可以做组合过滤查询
select * from books where (price = 35.99 or price = 99.99) and (public_data != “2016-02-16”)
Elasticsearch也有and,or,not 这样的组合条件的查询方式
格式如下:
{
“bool” : {
“must”: [], //条件必须满足,相当于and
“should”: [], //条件可以满足也可以不满足,相当于or
“must_not”:[], //条件不需要满足,相当于not
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值