查询
查询全部
GET /bank/_search
{
"query": {
"match_all": {}
}
, "sort": [
{
"balance": {
"order": "desc"
}
}
],
"from": 10,
"size": 5,
"_source": ["account_number", "balance", "firstname"]
}
match
全文检索,维护了倒排索引,分词,按评分由高到低排序
GET /bank/_search
{
"query": {
"match": {
"address": "Mill Lane"
}
}
}
match_phrase
把词组当成整体查询,包含这个字段,不分词
GET /bank/_search
{
"query": {
"match_phrase": {
"address": "Mill Lane"
}
}
}
.keyword
只能查询精确匹配,全等于
GET /bank/_search
{
"query": {
"match": {
"address.keyword": "Mill Lane"
}
}
}
fields
多个字段包含某个词,多字段匹配 分词
GET /bank/_search
{
"query": {
"multi_match": {
"query": "Mill Road",
"fields": ["address","firstname"]
}
}
}
bool
must必须满足, must_not必须不满足,should可以匹配可以不匹配,会贡献相关性得分
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "M"
}
},
{
"match": {
"address": "mill"
}
}
],
"must_not": [
{
"match": {
"age": "28"
}
}
],
"should": [
{
"match": {
"lastname": "Holland"
}
}
]
}
}
}
must range
会计算每个结果的相关性得分
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"age": {
"gte": 40,
"lte": 50
}
}
}
]
}
}
}
filter
不会计算每个结果的相关性得分
GET /bank/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gte": 40,
"lte": 50
}
}
}
}
}
}
term
找精确字段(如年龄)使用term, text文本全文检索的使用match
GET /bank/_search
{
"query": {
"term": {
"age": "28"
}
}
}
聚合
可以执行多个聚合
GET /bank/_search
{
"size": 0,
"query": {
"bool": {
"filter": {
"range": {
"balance": {
"gte": 30000,
"lte": 40000
}
}
}
}
},
"aggs": {
"aggAge": {
"terms": {
"field": "age",
"size": 10
}
},
"ageAvg": {
"avg": {
"field": "age"
}
},
"balanceAvg": {
"avg": {
"field": "balance"
}
}
}
}
查看年龄段的平均薪资
GET /bank/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"ageRange": {
"range": {
"field": "age",
"ranges": [
{
"from": 10,
"to": 20
},
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"bananceAvg": {
"avg": {
"field": "balance"
}
}
}
}
}
}
查出所有年龄分布 并且这些年龄中M的和F的平均薪资,最大最小薪资以及这个年龄段的平均薪资最大最小薪资
GET /bank/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"ageRange": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"genderAgg": {
"terms": {
"field": "gender.keyword"
},
"aggs": {
"balanceAvg": {
"avg": {
"field": "balance"
}
},
"balanceMax": {
"max": {
"field": "balance"
}
},
"balanceMin": {
"min": {
"field": "balance"
}
}
}
},
"balanceAvg": {
"avg": {
"field": "balance"
}
},
"balanceMax": {
"max": {
"field": "balance"
}
},
"balanceMin": {
"min": {
"field": "balance"
}
}
}
}
}
}
映射
获取字段映射
GET /myindex/_mapping
新增映射
6.0版本后建议索引后面不加类型
PUT /myindex
{
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"email": {
"type": "keyword"
},
"name": {
"type": "text"
}
}
}
}
添加字段映射
index:false是否被检索
PUT /myindex/_mapping
{
"properties": {
"remark": {
"type": "text",
"index": false
}
}
}
修改字段映射
1.创建一个新索引2.将数据迁移过去
创建一个新索引
PUT /newindex
{
"mappings": {
"properties": {
"age": {
"type": "long"
},
"email": {
"type": "text"
},
"name": {
"type": "text"
}
}
}
}
数据迁移没有类型的
# 数据迁移没有类型的
POST _reindex
{
"source": {
"index": "myindex"
},
"dest": {
"index": "newindex"
}
}
# 6.0之前有类型的,需要加类型
POST _reindex
{
"source": {
"index": "myindex",
"type": "type"
},
"dest": {
"index": "newindex"
}
}