1.elasticsearch-head界面说明
在浏览器访问http://IP:9100即可访问
可以看到我们的elasticsearch-head正是连接了我们的ES:9200地址,并且,ES集群名称为my-es,集群健康值为green,ES集群下有一个节点,名称为node-1
集群健康值的几种状态如下:
绿色:最健康的状态,代表所有的分片包括备份都可用
黄色:基本的分片可用,但是备份不可用(也可能是没有备份)
红色:部分的分片可用,表明分片有一部分损坏。此时执行查询部分数据仍然可以查到,遇到这种情况,还是赶快解决比较好
灰色:未连接到elasticsearch服务
通过上图我们可以看到有三个索引(.开头的名称),但是这三个索引不是我们自己创建的,接下来,我们将自己创建索引来对其进行更好地了解
2.索引的创建
创建一个test_index1的索引
创建完成后,在首页即可查看到我们的索引,0-4代表着我们的5个分片数
3.索引的删除
4.索引数据查看
在数据浏览中可以查看到我们各个索引的数据
5.索引文档的增删改查操作(restful风格)
Rest风格操作
5.1.添加索引(创建数据库)
在kibana中使用restful风格新建索引(库)
【工具可以自由选择,不限定一定要使用kibana】
PUT /索引名/类型名/文档id
{请求体}
PUT /数据库名/类型/一条数据的id
PUT /test_db1/type1/1
{
"name":"aibo",
"age":18
}
5.2.数据类型
那么name这个字段用不用指定类型呢。毕竟我们关系型数据库是需要指定类型的啊!
字符串类型text、 keyword
数值类型long, integer, short, byte, double, float, half float, scaled float
日期类型date
布尔值类型boolean
二进制类型binary
5.3.指定字段的类型(创建索引)
PUT /test_db2
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "long"
},
"birthday":{
"type": "date"
}
}
}
}
5.3.1默认字段类型(7以后的版本可以不指定类型可以用_doc表示默认类型)
PUT /test3/_doc/1
{
"name": "新加坡",
"age": 20,
"birthday": "2001-01-01"
}
5.4.修改操作
5.4.1直接put覆盖原数据(缺点:如果数据没写全就会丢失)
PUT /test_db3/_doc/1
{
"name": "洗脚盆",
"age": 20,
"birthday": "2001-01-01"
}
5.4.2使用update
POST /test3/_doc/1/_update
{
"doc":{
"name":"洗脚盆"
}
}
5.5.删除操作
DELETE test1
5.6.对文档的基本操作
PUT /test/user/1
{
"name":"张三",
"age": 23,
"desc":"个性张扬,敢闯敢拼",
"tags":["正直","乐观","易怒"]
}
5.6.1获取数据GET
GET /test/user/3
5.6.2简单的条件查询
GET /test/user/_search?q=name:张三
5.6.3复杂查询
5.6.3.1条件查询
GET test/user/_search
{
"query":{
"match":{
"name":"张"
}
}
}
5.6.3.2结果过滤
GET test/user/_search
{
"query":{
"match":{
"name":"张"
}
},
"_source": ["name","desc"]
}
5.6.3.3排序
GET test/user/_search
{
"query":{
"match":{
"name":"张"
}
},
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
5.6.3.4分页查询
GET test/user/_search
{
"query":{
"match":{
"name":"张"
}
},
"sort": [
{
"age": {
"order": "asc"
}
}
],
"from": 0,
"size": 1
}
from:从第几个数据开始
size:返回多少条数据(单页面的数据)
5.6.3.5与或非查询(布尔值查询)
must:所有的条件都要符合,对应于数据库中的and
should:多个条件,只要满足一个条件即可查出,相当于or操作
must_not:不符合条件的才能查出来,相当于逻辑非
GET kuangshen/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "狂神说"
},
"match": {
"age": 23
}
}
]
}
},
"_source": ["name", "desc"]
}
5.6.3.6过滤查询(大于小于查询)
GET test/user/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"name": "张三"
}
}
],
"filter": {
"range": {
"age": {
"gt": 10,
"lt": 25
}
}
}
}
}
}
5.6.3.7匹配条件查询(多个条件用空格分开)
GET test/user/_search
{
"query":{
"match": {
"tags": "正 乐"
}
}
}
5.6.3.8精确查询
关于分词:
term:直接精确查询(使用倒排索引,效率高)
match:会使用分词器解析(先分析文档,然后通过分析的文档进行查询)
GET test/user/_search
{
"query":{
"term": {
"name":"张"
}
}
}
5.6.3.9高亮查询
GET test/user/_search
{
"query":{
"match": {
"name":"张"
}
},
"highlight": {
"fields": {
"name":{}
}
}
}
5.6.3.10自定义高亮条件
GET test/user/_search
{
"query":{
"match": {
"name":"张"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"name":{}
}
}
}