1. 查询某个字段(del)为null的数据
GET /my_index1/_search
{
"query": {
"bool": {
"must_not": {
"exists":{"field":"del"}
}
}
}
}
2.elasticsearch批量修改,批量更新某个字段值
POST /mix_data_2/_update_by_query
{
"query": {
"bool": {
"must_not": {
"exists":{"field":"del"}
}
}
},
"script": {
"inline": "ctx._source['del'] = 1"
}
}
POST /ekr_*_org_publish_stat/_update_by_query
{
"query": {
"bool": {
"must": {
"match": {
"code.keyword": "YQKT"
}
}
}
},
"script": {
"inline": "ctx._source['code'] = 'LHKT'"
}
}
设置del为null的数据,del=1
POST请求/索引/文档名/_update_by_query
主要看一下下面的script
ctx._source[字段名] = “值”;ctx._source[字段名] = “值”;
多个的话就用分号隔开。
java api操作:
//集群模式,获取链接
Client client = elasticsearchTemplate.getClient();
UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
String name = "修改数值";
updateByQuery.source("索引")
//查询要修改的结果集
.filter(QueryBuilders.termQuery("field", 412))
//修改操作
.script(new Script( "ctx._source['field']='"+ name+"';ctx._source['field']='"+name+"'"));
//响应结果集
BulkByScrollResponse response = updateByQuery.get();
long updated = response.getUpdated();
查询:
POST _plugins/_sql?format=jdbc
{
"query":"select * from ekr_test_1"
}
添加:
POST /ekr_test_1/_doc/1
{
"year": "2010",
"yearcount": 12,
"userCode": "小红"
}
批量添加:
POST /ekr_test_1/_bulk?refresh=true
{ "index" : {"_id" : "2" } }
{ "userCode": "小红", "year": 2011, "yearcount": 5}
{ "index" : {"_id" : "3" } }
{ "userCode": "小红", "year": 2012, "yearcount": 15}
POST /_bulk?refresh=true
{ "index" : { "_index" : "ekr_test_1", "_id" : 3 } }
{ "year": 2012, "yearcount": 3, "userCode": "小红"}
{ "index" : { "_index" : "ekr_test_1", "_id" : 4 } }
{ "year": 2010, "yearcount": 13, "userCode": "小明"}
{ "index" : { "_index" : "ekr_test_1", "_id" : 5 } }
{ "year": 2011, "yearcount": 9, "userCode": "小明"}
{ "index" : { "_index" : "ekr_test_1", "_id" : 6 } }
{ "year": 2012, "yearcount": 3, "userCode": "小明"}
参考:Bulk - OpenSearch documentation
es 查询删除的几种方式
1.根据id删除
#根据id删除
POST / indexname / _delete_by_query {
"query": {
"match": {
"id": "100000"
}
}
}
2.根据多个id删除
#根据多个id删除
POST /indexname/_delete_by_query
{
"query": {
"bool": {
"filter": [{
"terms": {
"id": [
"100000",
"200000"
]
}
}]
}
}
}
3.根据多个id范围删除
#根据多个id范围删除
POST / indexname / _delete_by_query {
"query": {
"range": {
"id": {
"gte": 100000,
"lte": 200000
}
}
}
}
4.删除字段为空
POST /ekr_personal_publish_visit_history/_delete_by_query
{
"query": {
"bool": {
"must": {
"match": {
"resCode.keyword": ""
}
}
}
}
}
5.删除为null
POST /ekr_personal_publish_visit_history/_delete_by_query
{
"query": {
"bool": {
"must_not": {
"exists":{"field":"code"}
}
}
}
}
注意:删除完成后,执行以下脚本回收索引空间
curl -XPOST http://127.0.0.1:9200/indexname/_forcemerge?max_num_segments=1
修改字段类型,加上keyword
PUT ekr_cmfd_author_cd_stat/_mapping
{
"properties": {
"agencycode": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"author": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"authoragency": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"authorcode": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"kjcode": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"year": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
#更新索引
POST ekr_cdfd_author_cd_stat/_update_by_query
可以用*号匹配多索引同时更新同结构
建索引
PUT /ekr_spc_test
{
"mappings": {
"properties": {
"userCode": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"year": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"yearcount": {
"type": "long"
}
}
},
"settings": {
"analysis": {
"analyzer": {
"my_pinyin_analyzer": {
},
"my_lowercase_analyzer": {
"filter": [
"lowercase"
],
"type": "custom",
"tokenizer": "keyword"
}
}
}
}
}
重命名:新建索引,复制数据,删除旧索引
POST _reindex
{
"source": {
"index": "ekr_test_1"
},
"dest": {
"index": "ekr_dpc_test"
}
}
向索引文档中添加数据
PUT /flight/_doc/1
{
"Icao":"A0835D",
"Alt":2400,
"Lat":39.984322,
"Long":-82.925616
}
或curl命令
curl -X PUT "localhost:9200/flight/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"Icao":"A0835D",
"Alt":2400,
"Lat":39.984322,
"Long":-82.925616
}'
注意:本文归作者所有,未经作者允许,不得转载