进阶检索
1. Search API
GET bank/_search?q=*&sort=account_number:asc
GET /bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"account_number": "asc"
}
]
}
GET /bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"account_number": "asc"
},{
"balance": "desc"
]
}
2. Query DSL
GET /bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"balance": "desc"
}
],
"from" : 0,
"size":5 ,
"_source": ["balance","firstname"]
}
3. match匹配查询
GET /bank/_search
{
"query": {
"match": {
"account_number": "20"
}
}
}
GET /bank/_search
{
"query": {
"match": {
"address": "Kings"
}
}
}
4. match_phrase 短语匹配
GET /bank/_search
{
"query": {
"match_phrase": {
"address": "mill road"
}
}
}
GET /bank/_search
{
"query": {
"match": {
"address.keyword": "132 Gunnison"
}
}
}
5. multi_match 多字段匹配
GET /bank/_search
{
"query": {
"multi_match": {
"query": "mill",
"fields": ["state","address"]
}
}
}
6. bool 复杂查询
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "F"
}
},
{
"match": {
"age": "40"
}
}
],
"must_not": [
{
"match": {
"age": "38"
}
}
],
"should": [
{
"match": {
"employer": "Suremax"
}
}
]
}
}
}
7. filter结果过滤
GET /bank/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}
}
}
8. term
GET /bank/_search
{
"query": {
"term": {
"age": "30"
}
}
}
9. aggregations 执行聚合
GET /bank/_search
{
"query": {
"match": {
"address": "mill"
}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 10
}
},
"ageAvg":{
"avg": {
"field": "age"
}
}
},
"size": 0
}
GET /bank/_search
{
"query": {
"match_all": {}
},"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 10
},
"aggs": {
"ageAvg": {
"avg": {
"field": "balance"
}
}
}
}
}
}
GET /bank/_search
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 100
},
"aggs": {
"genderAvg": {
"terms": {
"field": "gender.keyword",
"size": 10
},
"aggs": {
"balanceAvg": {
"avg": {
"field": "balance"
}
}
}
},
"ageBalance":{
"avg": {
"field": "balance"
}
}
}
}
}
}
映射
映射
GET /bank/_mapping
PUT /my-index
{
"mappings": {
"properties": {
"age": { "type": "integer" },
"email": { "type": "keyword" },
"name": { "type": "text" }
}
}
}
PUT /my-index/_mapping
{
"properties": {
"employee-id": {
"type": "keyword",
"index": false
}
}
}
数据迁移
PUT /newbank
{
"mappings": {
"properties": {
"account_number": {
"type": "long"
},
"address": {
"type": "text"
},
"age": {
"type": "integer"
},
"balance": {
"type": "long"
},
"city": {
"type": "keyword"
},
"email": {
"type": "keyword"
},
"employer": {
"type": "keyword"
},
"firstname": {
"type": "text"
},
"gender": {
"type": "keyword"
},
"lastname": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"state": {
"type": "keyword"
}
}
}
}
POST _reindex
{
"source": {
"index": "bank",
"type": "account"
},
"dest": {
"index": "newbank"
}
}