ES增删改查
一:结构DDL操作
1.查看实例信息
GET /
1.查询所有索引名
GET _cat/indices?v&s=index:asc
2.查询对应索引的表结构
GET biz_commission_book
3.创建表
PUT cfg_send_sku { "settings": { "number_of_shards": 5, "number_of_replicas": 1 }, "mappings": { "cfg_send_sku" : { "properties" : { "obdCode" : { "type" : "keyword" }, "skuNo" : { "type" : "keyword" }, "skuName" : { "type" : "keyword" }, "weight" : { "type" : "double" }, "volume" : { "type" : "double" }, "cartonNumbers" : { "type" : "integer" }, "factory" : { "type" : "keyword" }, "batchNo" : { "type" : "keyword" }, "commissionBookNumber" : { "type" : "keyword" }, "tenantCode" : { "type" : "keyword" }, "createUser" : { "type" : "keyword" }, "createTime" : { "type" : "date", "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "updateUser" : { "type" : "keyword" }, "updateTime" : { "type" : "date", "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "isDelete" : { "type" : "integer" } } } } }
4.增加字段
PUT biz_commission_book/_mapping/biz_commission_book
{
"properties":{
"carrierCode" : {
"type" : "keyword"
},
"carrierName" : {
"type" : "keyword"
},
"transCapType" : {
"type" : "integer"
}
}
}
5.删除索引
DELETE biz_commission_book
二:数据DML操作
1.增加数据
#iot_vehicle_biz_case是index,biz_case是type,14是id值
PUT iot_vehicle_biz_case/biz_case/14 { "vehicleNo" : "皖AA1227", "caseProperties" : 1, "orgNameV" : "总机构", "orgCodeV" : "001", "caseStatus" : 2, "caseId" : "88888", "caseSubType" : 1, "caseTime" : "2020-11-25 18:37:00", "caseLevel" : 1, "tenantCode" : "lestore_web", "vehicleAlias" : "666666", "caseType" : 1 }
2.删除
2.1删除所有
_delete是删除命令
GET cfg_send_sku/cfg_send_sku/_delete_by_query { "query": { "match_all": {} } }
2.2删除指定条件
GET cfg_send_sku/cfg_send_sku/_delete_by_query { "query": { "match": { "skuName":"大白兔奶糖" } } }
3.更新
3.1更新某个字段的值
#JyrzKnUBgCgVdP2UH0aa指的是id POST biz_commission_book/biz_commission_book/JyrzKnUBgCgVdP2UH0aa/_update { "doc": { "transCapType":1 } }
3.2按条件更新
POST biz_commission_book/_doc/_update_by_query
{
"script": {
"source": "ctx._source.isResign=0",
"lang": "painless"
},
"query": {
"bool": {
"must_not": [{
"exists": {
"field": "isResign"
}
}]
}
}
}
4.查询
4.1查询所有
cfg_send_sku是索引名,cfg_send_sku是索引类型,_search是固定后缀,是查询命令。本例子是查询所有
GET cfg_send_sku/cfg_send_sku/_search
{ "query":
{
"match_all": {}
}
}
4.2查询指定
GET cfg_send_sku/cfg_send_sku/_search
{
"query": {
"match": {
"skuName":"大白兔奶糖"
}
}
}
4.3查询多个字段
GET biz_commission_book/biz_commission_book/_search { "query": { "bool": { "must": [ { "match": { "receiverNumber":"4695619" } }, { "match": { "truckingOrderNumber":"AS1598403676403696" } } ] } } }
4.3.1查询时间
GET biz_commission_book/_doc/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"msgTime":{
"gte":"2023-02-09 00:00:00"
}
}
},
{
"term": {
"toId":"wm3v6zEQAADgwLjOjAiX29yadQaDPpcw"
}
}
]
}
}
}
4.4 短语匹配查询
GET biz_commission_book/_doc/_search
{
"query": {
"match_phrase": {
"textContent":"可乐测试"
}
}
}
4.5模糊匹配查询
GET biz_commission_book/_doc/_search
{
"query": {
"wildcard": {
"name": "测试*"
}
}
}
4.6精确查询
GET biz_commission_book/_doc/_search
{
"query": {
"term": {
"bookId":"15618705332"
}
}
}
三:写入优化
PUT test_index/_settings
{ "refresh_interval": "1000ms" }
GET test_index/_settings