前言
9200和9300的区别
9200端口:ES节点和外部通讯使用
9300端口:ES节点之间通讯使用
1 安装jdk
在安装ElasticSearch之前需要先安装idk,安装过程省略。
2 安装ElasticSearch
2.1 下载ElasticSearch并解压到指定目录
我的解压目录是在:/opt/application/es6.7.0
2.2 修改ElasticSearch的elasticsearch.yml
在/opt/application/es6.7.0下执行cd config命令后,再执行vi elasticsearch.yml命令,出现如下:
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# network.host: 192.168.86.128
#
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, consult the network module documentation.
接着,我们把“network.host: 192.168.86.128”和“ http.port: 9200”前面的“#”去掉,同时把IP地址改成自己服务器的IP地址。
2.3 ElasticSearch的启动不能在root用户下,创建用户
[root@mazhen001 es6.7.0]# groupadd esmazhen
[root@mazhen001 es6.7.0]# useradd esmazhen001 -g esmazhen -p mameng1992
[root@mazhen001 es6.7.0]# chown -R esmazhen001:esmazhen /opt/application/es6.7.0/
2.4 修改sysctl.conf和limits.conf
进入到/etc目录下
[root@mazhen001 es6.7.0]# vi /etc/sysctl.conf
在这个文件中输入: vm.max_map_count = 655360,保存后:
[root@mazhen001 etc]# sysctl -p
vm.max_map_count = 655360
[root@mazhen001 etc]# vi /etc/security/limits.conf
在limits.conf文件中输入:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
然后重启服务器。
切换到esmazhen001用户,然后执行:
[esmazhen001@mazhen001 root]$ cd /opt/application/es6.7.0/bin/
[esmazhen001@mazhen001 bin]$ ./elasticsearch
看到如下界面,则启动成功:
最后一点,不要忘记关闭防火墙:
[esmazhen001@mazhen001 bin]$ systemctl stop firewalld.service
浏览器中输入服务器IP和端口:http://192.168.86.128:9200/
{
"name" : "oOrQ-wF",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "0_ca5YlCQ-ax6-J6QmoDaQ",
"version" : {
"number" : "6.7.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "8453f77",
"build_date" : "2019-03-21T15:32:29.844721Z",
"build_snapshot" : false,
"lucene_version" : "7.7.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
出现上述代码,则一切OK!
3 DSL查询
DSL查询是POST过去一个JSON,由于POST请求是JSON格式的,所以存在很多灵活性、也有很多形式,例如:
###DSL查询
###term查询是精确匹配,不会对字段分词查询
GET /mytest/user/_search
{
"query": {
"term": {
"name": "小明"
}
}
}
###match支持分词查询
GET /mytest/user/_search
{
"from": 0,
"size": 3,
"query": {
"match": {
"name": "小"
}
}
}
###过滤age在10到40之间的
GET /mytest/user/_search
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 40
}
}
}
}
},
"from": 0,
"size": 20,
"_source": ["name","age"]
}
4 kibana语句
###创建索引
PUT /mytest
GET /mytest
###创建文档(/索引/类型/id)
PUT /mytest/user/1
{
"name":"mazhen",
"age":27,
"sex":0
}
###查询文档(/索引/类型/id)
GET /mytest/user/2
###修改文档(/索引/类型/id)
PUT /mytest/user/1
{
"name":"wangmengmeng",
"age":30,
"sex":1
}
###删除索引
DELETE /mytest
###不指定id的方式生成文档
POST /mytest/user
{
"name":"蒙梦",
"sex":1,
"age":30
}
###查询当前类型下的所有文档
GET /mytest/user/_search
###根据多个id进行查询
GET /mytest/user/_mget
{
"ids":[1,2,"7ru87WkBvs3SBRfGdj_Q"]
}
###查询age为27的
GET /mytest/user/_search?q=age:27
###查询age在20到40之间的
GET /mytest/user/_search?q=age[20 TO 40]
###查询age在20到40之间,并且age降序、从0条数据到第1条数据
GET /mytest/user/_search?q=age[20 TO 40]&sort=age:desc&from=0&size=1
###查询age在20到40之间,并且age降序、从0条数据到第1条数据,展示name和age字段
GET /mytest/user/_search?q=age[20 TO 40]&sort=age:desc&from=0&size=1&_source=name,age
参考文章:https://blog.csdn.net/weixin_41615494/article/details/79591335