前言:elasticsearch貌似版本迭代超快,现在网上大多的例子都是老版本的。
最新elasticsearch 7.x的版本里type的概念被废弃(https://www.elastic.co/guide/en/elasticsearch/reference/master/removal-of-types.html)。跑在新版elasticsearch下会警告或报错。以下命令符合7.x规范,Kibana里全部验证过。
// 创建一个索引
PUT school
{
"settings":{
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings":{
"properties":{
"name":{
"type":"text"
},
"address":{
"type":"keyword"
},
"age":{
"type":"integer"
},
"date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss|| yyy-MM-dd||epoch_millis"
}
}
}
}
// 插入一条数据,手动指定ID
PUT /school/_doc/1
{
"name":"杨旭",
"address":"山东烟台",
"age":22,
"date":"1995-04-12"
}
// 插入一条数据,自动生成ID
POST /school/_doc/
{
"name":"张小花",
"address":"山东烟台",
"age":24,
"date":"1996-07-24"
}
// 用ID查询某条数据
GET /school/_doc/1
// 用ID查询某条数据的指定字段
GET /school/_doc/1?_source=name,address
// 查询某库所有数据
GET /school/_search
// 复合查询
GET /school/_search
{
"query": {
"match": {
"name": "花"
}
},
"sort": [
{
"date": {
"order": "desc"
}
}
],
"from": 0,
"size": 10
}
// 查看索引的mapping
GET /school/_mapping
// 覆盖式修改数据(全覆盖,而非增量覆盖)
PUT /school/_doc/1
{
"name":"杨花旭",
"address":"山东济宁",
"date":"1995-04-12"
}
// 增量式修改数据
POST /school/_update/1/
{
"doc":{
"age":20
}
}
// 删除某条数据
DELETE /school/_doc/1