练习数据获取:
查看节点信息
查看健康状况
查看主节点信息
查看所有的索引
PUT保存
PUT /konan/_doc/2
{
"name": "千岛的小岛",
"age": 26,
"desc": "敬佩的对象",
"tags": ["四季","温暖","技术宅"]
}
POST保存或修改
不带id是新增,带id之前有数据是修改,没有数据还是新增
{
"name": "konan",
"age": "18"
}
POST带update保存或修改
会对比前后数据,数据不变时不会检查元数据
POST /konan/_update/2
{
"doc": {
"name": "千岛上住着一只会飞的鱼",
"age": "18"
}
}
带乐观锁修改
http://127.0.0.1:9201/test1/_doc/1?if_seq_no=1&if_primary_term=1
{
"name": "qiandao"
}
http://127.0.0.1:9201/test1/_doc/1?if_seq_no=10&if_primary_term=1
{
"name": "konan"
}
delete带id删除文档
delete删除索引
没有请求体的查询
GET bank/_search?q=*&sort=account_number:asc
带请求体的查询
GET bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"balance": {
"order": "desc"
}
}
],
"from": 6,
"size": 3,
"_source": ["balance","age"]
}
查询指定字段符合条件的文档
GET bank/_search
{
"query": {
"match": {
"balance": 16418
}
}
}
进行分词的查询
GET bank/_search
{
"query": {
"match": {
"address": "mill road"
}
}
}
将查询的词作为一个整体不进行分词
相当于会模糊匹配字段
GET bank/_search
{
"query": {
"match_phrase": {
"address": "mill road"
}
}
}
仅当等于指定的字段时才能查出结果
GET bank/_search
{
"query": {
"match": {
"address.keyword": "789 Madison"
}
}
}
检索多个字段中符合条件的查询
会进行分词
GET bank/_search
{
"query": {
"multi_match": {
"query": "mill movico",
"fields": ["address","city"]
}
}
}
bool拼接多个条件查询
must必须要满足的条件
must_not必须不满足的条件
should满足的话会加分
filter用来过滤不满足条件的文档,不计算分值
GET bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "M"
}
},
{
"match": {
"address": "mill"
}
}
],
"must_not": [
{
"match": {
"age": 28
}
}
],
"should": [
{
"match": {
"lastname": "hines"
}
}
],
"filter": [
{
"range": {
"age": {
"gte": 10,
"lte": 40
}
}
}
]
}
}
}
用must进行区间查询
这里比较和filter的区别
用must查出的文档都是有分值的
GET bank/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"age": {
"gte": 30,
"lte": 40
}
}
}
]
}
}
}
用filter进行区间查询
filter和must实现的功能其实差不多
filter不计算得分,过滤不满足的条件的文档,类似于must_not
GET bank/_search
{
"query": {
"bool": {
"filter": [
{
"range": {
"age": {
"gte": 30,
"lte": 40
}
}
}
]
}
}
}
带排序的查询
GET konan/_search
{
"query": {
"match": {
"name": "千岛"
}
},
"_source": ["name","age"],
"sort": [
{
"age": {
"order": "asc"
}
}
],
"from": 0,
"size": 2
}
term查询指定字段里含有指定关键词的文档
和match类似,匹配某个属性的值
全文检索字段用match,其他非text字段匹配用term
GET bank/_search
{
"query": {
"term": {
"balance": {
"value": 19648
}
}
}
}
terms查询某个字段里含有多个关键词的文档
包含其一即匹配上
GET bank/_search
{
"query": {
"terms": {
"address": [
"789",
"street"
]
}
}
}
高亮查询
GET jd_goods/_search
{
"query": {
"match": {
"name": "mill"
}
},
"highlight": {
"pre_tags": "<p class=key style='color:red'>",
"post_tags": "</p>",
"fields": {
"name": {}
}
}
}
查询示例
GET zhiyi/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "王"
}
},
{
"match": {
"name": "李"
}
}
],
"filter": [
{
"range": {
"age": {
"gte": 18,
"lte": 24
}
}
}
]
},
"term": {
"name": {
"value": "千岛"
}
}
},
"sort": [
{
"age": {
"order": "asc"
}
}
],
"from": 0,
"size": 15,
"highlight": {
"pre_tags": "<span style='color:red'>",
"post_tags": "</span>",
"fields": {
"people": {}
}
}
}
聚合查询
搜索address中包含mill的所有年龄分布以及平均年龄、平均工资
GET bank/_search
{
"query": {
"match": {
"address": "mill"
}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 10
}
},
"ageAve": {
"avg": {
"field": "age"
}
},
"balanceAvg":{
"avg": {
"field": "balance"
}
}
},
"size": 0
}
聚合查询
按照年龄聚合,并且请求这些年龄段的这些人的平均薪资
GET bank/_search
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 100
},
"aggs": {
"balanceAvg": {
"avg": {
"field": "balance"
}
}
}
}
},
"size": 0
}
聚合查询
查出所有年龄分布,并且这些年龄段中M的平均薪资和F的平均薪资以及这个年龄段的总体平均薪资
GET bank/_search
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 100
},
"aggs": {
"genderAgg": {
"terms": {
"field": "gender.keyword",
"size": 10
},
"aggs": {
"balanceAvg": {
"avg": {
"field": "balance"
}
}
}
},
"balanceAvg": {
"avg": {
"field": "balance"
}
}
}
}
},
"size": 0
}
查询索引映射
GET my_index/_mapping
创建索引映射
PUT my_index
{
"mappings": {
"properties": {
"age":{"type": "integer"},
"email":{"type": "keyword"},
"name":{"type": "text","index": true}
}
}
}
给索引增加映射字段
index指定当前字段是否参与全文索引,默认为true
PUT my_index/_mapping
{
"properties":{
"employee-id":{
"type": "keyword",
"index": false
}
}
}
修改字段类型
es不支持修改字段类型,想要修改只能使用数据迁移的方法
新建一个映射,使用_reindex进行数据迁移,如下操作步骤
PUT /newbank
{
"mappings" : {
"properties" : {
"account_number" : {
"type" : "long"
},
"address" : {
"type" : "keyword"
},
"age" : {
"type" : "integer"
},
"balance" : {
"type" : "long"
},
"city" : {
"type" : "keyword"
},
"email" : {
"type" : "keyword"
},
"employer" : {
"type" : "keyword"
},
"firstname" : {
"type" : "keyword"
},
"gender" : {
"type" : "keyword"
},
"lastname" : {
"type" : "keyword"
},
"state" : {
"type" : "keyword"
}
}
}
}
GET bank/_mapping
GET newbank/_mapping
POST _reindex
{
"source": {
"index": "bank"
},
"dest": {
"index": "newbank"
}
}
GET bank/_search
GET newbank/_search
默认标准分词器
POST _analyze
{
"analyzer": "standard",
"text": "The 2 QUICK Brwon-Foxes jumped over the lazy dog's bone."
}
默认标准分词器
针对汉字只能逐个分词
POST _analyze
{
"analyzer": "standard",
"text": "世间美好与你环环相扣"
}
ik_max_word最细粒度分词
POST _analyze
{
"analyzer": "ik_max_word",
"text": "世间美好与你环环相扣"
}
ik_smart最粗粒度分词
POST _analyze
{
"analyzer": "ik_max_word",
"text": "世间美好与你环环相扣"
}
自定义ik分词远程词典
从nginx中读取
POST _analyze
{
"analyzer": "ik_max_word",
"text": "千岛你好,我在在这里!与真啊"
}
自定义ik分词本地词典
从项目文件读取
POST _analyze
{
"analyzer": "ik_max_word",
"text": "笔钢"
}