ES常用操作语句
注:本文中的操作语句基于ES5.5和7.7的版本,版本不同操作语句上可能有细微差别,如5.5版本有索引类型,7.7版本已废弃,查询不应该带索引类型
新增
# 添加字段,并设置字段类型
PUT /索引/_mapping/customer_info_type
{"properties": {"TELEPHONE_INTERVIEW_TYPE": { "type": "keyword" }}
}# 添加字段,并设置字段类型为date
PUT /customer_info/_mapping/customer_info_type
{"properties": {"DISEASE_STSC_DATE": { "type": "date","format": "yyyy-MM-dd" }}
}
#新增数据
PUT memb_label/_doc/61b9505da2863600083ae3ff
{"MEMBER_ID":"61b9505da2863600083ae3ff","BAS_MEMBER_NAME":"测试","LAST_RECE_TIME":"2023-07-15 10:00:00","LAST_APPOINT_TIME":"2023-07-19 10:00:00","GRADE_APPOINT_STATE":"UNCLAIMED","ONECOUP_LATEST_USE_TIME":null,"ONECOUP_LATEST_BUY_TIME":"2023-07-14"
}
#批量新增数据
PUT /memb_label/_bulk
{ "create" : { "_index" : "memb_label", "_id" : "758C2AEE-26CD-499C-A58F-B0BFEAF77834" } }
{ "MEMBER_ID" : "758C2AEE-26CD-499C-A58F-B0BFEAF77834","BAS_MEMBER_NAME" : "测试1" }
{ "create" : { "_index" : "memb_label", "_id" : "661C2D0B-C30A-4771-9A8F-B1C2D70AAF5E" } }
{ "MEMBER_ID" : "661C2D0B-C30A-4771-9A8F-B1C2D70AAF5E","BAS_MEMBER_NAME" : "测试2" }
删除
根据时间范围删除
POST /customer_info/customer_info_type/_delete_by_query
{"query": {"range": {"DATA_DATE": {"lt":"2021-01-20"}}}
}
修改
# 根据id更新指定字段数据
POST /memb_label/_update/61b96102a2863600083c1592
{"doc": {"LAST_RECE_TIME":"2023-07-13 10:00:00","LAST_APPOINT_TIME":"2023-06-10 10:00:00","GRADE_APPOINT_STATE":null,"ONECOUP_LATEST_USE_TIME":null,"ONECOUP_LATEST_BUY_TIME":null}
}# 根据id更新指定字段数据
POST /customer_info/customer_info_type/61b99dc3a2863600083ec3e5/_update
{"doc": {"TELEPHONE_INTERVIEW_TYPE": "TEL_HANG_UP"}
}# 批量修改数据
PUT /memb_label/_bulk
{ "update" : {"_id" : "758C2AEE-26CD-499C-A58F-B0BFEAF77834", "_index" : "memb_label"} }
{ "doc" : {"BAS_MEMBER_NAME" : "测试11"} }
{ "update" : {"_id" : "661C2D0B-C30A-4771-9A8F-B1C2D70AAF5E", "_index" : "memb_label"} }
{ "doc" : {"BAS_MEMBER_NAME" : "测试22"} }#in批量更新
POST /customer_info/customer_info_type/_update_by_query
{"query": {"bool": {"must": [{"terms": {"MEMBER_ID": ["3e1ceb112d3d4eb6a79ba67e2f930e5c", "000013F7-7B31-49C4-A904-489F19DBFAF1", "61b1c81c9c1682000692fba1"]}}]}},"script": {"inline": "ctx._source['HOME_MEDICAL_SERVICE_USER'] = 'Y'"}
}
查询
# 华为云根据sql查询数据(华为云如果使用到opendistro_sql插件加上这个,使用熟悉的SOL查询语法从Elasticsearch中提取见解)
POST _opendistro/_sql?format=csv
{"query": " select count(*) from memb_label where ( BAS_IS_TEL = 'N' or BAS_IS_TEL is null ) AND ( BAS_IS_MSG = 'N' or BAS_IS_MSG is null ) AND MAIN_CNSM_PHMC_CODE = '6573' "
}# 查询所有标签
GET customer_info/_mapping/customer_info_type# 查询所有会员
GET customer_info/_search
{"track_total_hits": true
}#查询OFFLINE_Y_GROSS_RATE为空的数据
GET /customer_info/_search
{"query": {"script" : {"script" : {"inline": "doc['BAS_MOBILE_PHONE'].length>0","lang": "painless"}}}
}#聚合分组查询
GET /hot_memb_label/_search
{"size": 0,"aggs": {"group_by_tags": {"terms": {"field": "MEMB_CARD_STATE"}}}
}#聚合查询出错解决方式
PUT /hot_memb_label/_mapping
{"properties":{"MEMB_CARD_STATE":{"type":"text","fielddata":true}}
}#模糊查询
GET /customer_info/customer_info_type/_search?pretty
{"query": {"match" : {"message" : {"query" : "131","operator" : "and"}}}}# 根据指定条件查询
GET /customer_info/_search
{"query": {"bool": {"must": [{"match": {"MEMBER_ID": "61ba04f8a286360008444b88"}}]}}
}# 根据多个条件查询数据
GET /customer_info/_search
{"query": {"bool": {"must": [{"term": {"BELONE_CITY_NAME": "常德"}},{"term": {"LAST_TIME_CUNSU_DATE":"2021-10-26"}}]}}
}# 根据多个条件查询指定字段
GET /customer_info/_search?_source=MEMBER_ID,GNDR
{"query": {"bool": {"must": [{"term": {"BELONE_CITY_NAME": "常德"}},{"term": {"LAST_TIME_CUNSU_DATE":"2021-10-26"}}]}}
}# 根据指定条件查询数据总数
GET /customer_info/_search
{"query": {"bool": {"must": [{"match": {"DATA_DATE": "2021-11-02"}}]}}, "size": 0
}#in批量查询
GET /customer_info/_search
{"query": {"bool": {"must": [{"terms": {"MEMBER_ID": ["3e1ceb112d3d4eb6a79ba67e2f930e5c", "000013F7-7B31-49C4-A904-489F19DBFAF1", "61b1c81c9c1682000692fba1"]}}]}}
}#根据时间范围查询
GET /customer_info/_search
{"query": {"range": {"DATA_DATE": {"lt":"2021-01-20"}}}
}#根据时间字段倒序并分页查询数据
GET /customer_info/_search
{"from": 1, "size": 20, "query": {"match_all": {}},"sort": [{"DATA_DATE": {"order": "desc"}}]
}#查询ES状态
GET _cat/indices?v&s=docs.count:desc,store.size:asc