当前文档所用ES版本 6.4.3
ElasticSearch 提供了一系列的Restful风格的API,我们可以使用curl命令进行使用,也可以在kibana中使用。
Restful风格
它是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。 RESTFUL适用于移动互联网厂商作为业务使能接口的场景,实现第三方OTT调用移动网络资源的功能,动作类型为新增、变更、删除所调用资源。通过不同的请求方法来实现不同的功能。
GET
获取数据 POST
添加数据 PUT
添加数据 DELETE
删除数据
ElasticSearch的核心概念与关系数据库对比
curl -XGET 'http://hadoop137:9200'
curl -XGET 'http://hadoop137:9200/_cluster/state?pretty'
#这里在url后面添加了pretty是为了让其在控制台上输出的结果是一个优美的json格式
curl -XGET 'http://hadoop137:9200/_cat/indices?pretty&v'
curl -XPUT 'http://hadoop137:9200/upuptop?pretty'
curl -XDELETE 'http://hadoop137:9200/upuptop?pretty'
# 9200/索引库名/文档类型/id/ -d 文档内容
# id可以忽略,ES会自动生成id,如果id存在,那么就是更新数据,字段可以增加
curl -XPOST 'http://hadoop137:9200/upuptop/stu/1?pretty' -H 'Content-Type: application/json' -d '
{
"name":"shaofei",
"age":22,
"sex":1
}'
# 给id为1的学生修改姓名为upuptop,年龄修改为25,添加学号字段
curl -XPOST 'http://hadoop137:9200/upuptop/stu/1?pretty' -H 'Content-Type: application/json' -d '
{
"name":"upuptop",
"age":25,
"sex":0,
"number":"1501260222"
}'
curl -XGET 'http://hadoop137:9200/upuptop/stu/_search?pretty'
curl -XGET 'http://hadoop137:9200/upuptop/stu/1?pretty'
curl -XDELETE 'http://hadoop137:9200/upuptop/stu/1?pretty'
ES最主要的功能,搜索,也是就是查询文档。下面我们来看看主要的查询命令吧。
首先搞点数据到ElasticSearch中
这里使用logstash工具将mysql数据库中的数据导入到ES中
对于LogStash的介绍请查看这篇文章:《LogStash的安装部署与应用》。
使用查询命令对数据进行查询。
查询name含有‘upuptop'字符串的数据、模糊查询
curl -XGET http://hadoop137:9200/upuptop/stu/_search?q=name="upuptop"
查询name含有‘upuptop'字符串的数据、模糊查询
curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty -H 'Content-Type: application/json' -d '
{
"query":
{"match":
{"name":"upuptop"}
}
}'
查询lastname、firstname含有‘upuptop'字符串的数据
curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty -d '
{
"query":
{"multi_match":
{
"query":"upuptop",
"fields":["last_name","first_name"],
"operator":"and"
}
}
}'
# 组合查询,must,must_not,should
# must + must : 交集
# must +must_not :差集
# should+should : 并集
# 查询 first_name为upuptop并且年龄是33的数据
curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty -d '
{
"query":
{"bool" :
{
"must" :
{"match":
{"first_name":"upuptop"}
},
"must" :
{"match":
{"age":33}
}
}
}
}'
# 查询 first_name为upuptop并且年龄不是33的数据
curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty -d '
{
"query":
{"bool" :
{
"must" :
{"match":
{"first_name":"upuptop"}
},
"must_not" :
{"match":
{"age":33}
}
}
}
}'
# 查询 first_name不为upuptop并且年龄不是33的数据
curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty -d '
{
"query":
{"bool" :
{
"must_not" :
{"match":
{"first_name":"upuptop"}
},
"must_not" :
{"match":
{"age":33}
}
}
}
}'
curl -XGET http://hadoop137:9200/upuptop/stu/_search -d '
{
"query":
{"bool" :
{
"must" :
{"term" :
{ "first_name" : "upuptop" }
}
,
"must_not" :
{"range":
{"age" : { "from" : 20, "to" : 33 }
}
}
}
}
}'
## 创建索引库并设置副本数为2个,默认5个主分片,每个分片有两个副本,一共15个片
curl -XPUT 'http://hadoop137:9200/test2/' -d'{"settings":{"number_of_replicas":2}}'
## 设置3个主分片、3个从分片
curl -XPUT 'http://hadoop137:9200/test3/' -d'{"settings":{"number_of_shards":3,"number_of_replicas":3}}'
curl -XPUT 'http://hadoop137:9200/test4/' -d'{"settings":{"number_of_shards":6,"number_of_replicas":4}}'
## 设置索引的mapping,设置数据类型,分词规则等
curl -XPOST http://192.168.9.11:9200/upuptop/person/_mapping -d'
{
"person": {
"properties": {
"content": {
"type": "string",
"store": "no",
"term_vector": "with_positions_offsets",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"include_in_all": "true",
"boost": 8
}
}
}
}'
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有