在练习语法前,先导入官网中的数据,来进行各种的语法测试
官方文档上有详细的操作过程,就根据官方文档来进行操作
GET /bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"account_number": "asc",
"age": "desc"
}
],
"from": 0,
"size": 10
}
GET /bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"account_number": "asc",
"age": "desc"
}
],
"from": 0,
"size": 10,
"_source": ["balance","firstname"]
}
GET /bank/_search
{
"query": {
"match": {
"address": "Mill"
}
},
"sort": [
{
"account_number": "asc",
"age": "desc"
}
],
"from": 0,
"size": 10,
"_source": ["balance","firstname"]
}
GET /bank/_search
{
"query": {
"match_phrase": {
"address": "Mill Lane"
}
},
"sort": [
{
"account_number": "asc",
"age": "desc"
}
],
"from": 0,
"size": 10
}
GET /bank/_search
{
"query": {
"multi_match": {
"query": "Mill",
"fields": ["address","city"]
}
}
}
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "M"
}
},
{
"match": {
"address": "mill"
}
}
]
}
}
}
GET /bank/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 30
}
}
}
}
}
}
GET /bank/_search
{
"query": {
"term": {
"age":"28"
}
}
}
# aggreation address 包含mill的人的年龄分布和平均年龄
GET /bank/_search
{
"query": {
"match": {
"address": "mill"
}
},
"aggs": {
"allAge": {
"terms": {
"field": "age",
"size": 10
}
},
"aveAge":{
"avg": {
"field": "age"
}
}
},
"size": 0
}
在aggreation中有很多字段,后边的size等于0是为了不出现检索数据,只出现聚合数据。聚合不仅仅和水平,也可以嵌套。
# 创建索引并制定映射关系
PUT /my-index
{
"mappings": {
"properties": {
"age":{"type": "integer"},
"email":{"type": "keyword"},
"name":{"type": "text"}
}
}
}
添加新的字段,可以通过index为true来设置不被索引(相当于是冗余字段)
# 给已经创建的所有添加映射
PUT /my-index/_mapping
{
"properties": {
"employee-id": {
"type": "keyword",
"index": false
}
}
}
可以通过GET /my-index/_mapping来查看所有的映射
PUT /new-index
{
"mappings": {
"properties" : {
"account_number" : {
"type" : "long"
},
"address" : {
"type" : "keyword"
},
"age" : {
"type" : "integer"
},
"balance" : {
"type" : "long"
},
"city" : {
"type" : "keyword"
},
"email" : {
"type" : "keyword"
},
"employer" : {
"type" : "text"
},
"firstname" : {
"type" : "text"
},
"gender" : {
"type" : "keyword"
},
"lastname" : {
"type" : "text"
},
"state" : {
"type" : "keyword"
}
}
}
}
数据迁移,将老index,type为account的移动到new-index下
POST _reindex
{
"source": {
"index": "bank",
"type": "account"
},
"dest": {
"index": "new-index"
}
}
配置自定义字典的地址
重启ES服务(docker restart elasticsearch)
在Kibana中进行测试,可以看到分词成功
ES就说到这,官网上有更全面的案例和语法规则!