一、文档管理1. 准备环境• 清空历史记录[rootes01: ~]# curl -k -u elastic:123456 -X DELETE https://10.0.0.91:9200/index*{acknowledged:true}[rootes01: ~]# curl -k -u elastic:123456 -X DELETE https://10.0.0.91:9200/old*{acknowledged:true}• 准备索引vimproduct.json{aliases:{oldboy_alias_01:{},oldboy_alias_02:{},oldboy_alias_03:{}},mappings:{properties:{product_name:{type:keyword},inventory:{type:integer},price:{type:float},weight:{type:short,index:false},tags:{type:keyword,doc_values:true},merchant_ip:{type:ip},business_address:{type:geo_point},delivery_area:{type:geo_shape},product_upload_time:{type:date,format:yyyy-MM-dd HH:mm:ss||epoch_millis},app_version:{type:version},is_up:{type:boolean},specifications:{type:object,properties:{color:{type:keyword},size:{type:keyword},brand:{type:keyword},material:{type:text}}},comment:{type:text,fields:{keyword:{type:keyword,ignore_above:256}},analyzer:standard}}},settings:{number_of_shards:3,number_of_replicas:1}}# 创建 product.json 配置文件内容略vimproduct.json{aliases:{oldboy_alias_01:{},oldboy_alias_02:{},oldboy_alias_03:{}},mappings:{properties:{product_name:{type:keyword},inventory:{type:integer},price:{type:float},weight:{type:short,index:false},tags:{type:keyword,doc_values:true},merchant_ip:{type:ip},business_address:{type:geo_point},delivery_area:{type:geo_shape},product_upload_time:{type:date,format:yyyy-MM-dd HH:mm:ss||epoch_millis},app_version:{type:version},is_up:{type:boolean},specifications:{type:object,properties:{color:{type:keyword},size:{type:keyword},brand:{type:keyword},material:{type:text}}},comment:{type:text,fields:{keyword:{type:keyword,ignore_above:256}},analyzer:standard}}},settings:{number_of_shards:3,number_of_replicas:1}}# 创建索引[rootes01: ~]# curl -k -u elastic:123456 -X PUT https://10.0.0.91:9200/oldboy_index01?pretty -H Content-Type: application/json --data-binary ./product.json2. 创建文档单条文档创建指定 ID插入字段类型插入时写入geo_point坐标点[经度纬度]geo_shape坐标范围【“type”: “envelope”】表示形状为矩形形状写法“区域字段”: { “type”: “envelope”, “coordinates”: [ [坐标],[坐标] ] },geo_shape坐标范围【“type”: “circle”】表示形状为圆形形状写法“区域字段”: { “type”: “circle”, “coordinates”: [116.4, 39.9], ️注意这里的中心点就是商家坐标 “radius”: “1000m” ️表示一公里范围内画圆 }⭐️es7.11版本之后才支持geo_shape坐标范围【“type”: “polygon”】表示多边形要写很多个点(第1个和最后1个点相同经纬度闭合)# 创建文档ID 为 1[rootes01:~]# curl -k -u elastic:123456 -X PUT https://10.0.0.91:9200/oldboy_index01/_doc/1?pretty -H Content-Type: application/json -d {product_name:高端5G智能手机,inventory:204,price:5999.0,weight:3,tags:[2026年新款,光学摄像头,自动变焦,限时限量5折抢购,国补99%],merchant_ip:120.244.134.121,business_address:[116.4,39.9],delivery_area:{type:envelope,coordinates:[[116.387123,39.908992],[116.412877,39.891008]]},product_upload_time:2026-05-26 21:15:33,app_version:3.1.2,is_up:true,specifications:{color:尊贵金,size:430x932,brand:华为,material:28K纯金},comment:遥遥领先}单条创建-不指定ID指定id创建文档PUT、POST都行不指定ID创建文档必须使用POSTcurl-k-uelastic:123456-XPOSThttps://10.0.0.91:9200/oldboy_index01/_doc?pretty-HContent-Type: application/json-d { product_name:轻薄旗舰拍照手机, inventory:203, price:6899.0, weight:16, tags:[轻薄旗舰,拍照效果惊艳,便携设计,高颜值,拍照效果出众], merchant_ip:123.244.150.123, business_address:[116.410000,39.900000], delivery_area:{ type:polygon, coordinates:[[ [116.400,39.910], [116.420,39.910], [116.420,39.890], [116.400,39.890], [116.400,39.910] ]] }, product_upload_time:2025-02-27 17:15:00, app_version:3.2.5, is_up:true, specifications:{ color:晨曦金, size:6.4英寸, brand:OPPO, material:玻璃 }, comment:轻薄设计拍照效果非常惊艳 }• 查询所有文档[rootes01: ~]# curl -k -u elastic:123456 -X GET https://10.0.0.91:9200/oldboy_index01/_search?pretty -H Content-Type: application/json -d{query:{match_all:{}}} # 只查看文档 ID [rootes01: ~]# curl -k -u elastic:123456 -X GET https://10.0.0.91:9200/oldboy_index01/_search?prettyfilter_pathhits.hits._id -H Content-Type: application/json -d{query:{match_all:{}}}• 批量创建文档curl-k-uelastic:123456-XPOSThttps://10.0.0.91:9200/_bulk?pretty-HContent-Type: application/json-d { index : { _index : oldboy_index01, _id : 3 } } {product_name:高端旗舰智能手机,inventory:156,price:8999.0,weight:2,tags:[2025年新款,徕卡摄像头,5G网络,限时优惠,国补20%,旗舰机型],merchant_ip:123.244.134.107,business_address:[116.408000,39.904000],delivery_area:{type:polygon,coordinates:[[[116.398,39.914],[116.418,39.914],[116.418,39.894],[116.398,39.894],[116.398,39.914]]]},product_upload_time:2025-03-15 10:30:00,app_version:3.2.0,is_up:true,specifications:{color:星空黑,size:6.7英寸,brand:华为,material:陶瓷},comment:拍照效果非常出色夜景模式强大} { index : { _index : oldboy_index01, _id : 4 } } {product_name:性价比拍照手机,inventory:289,price:2999.0,weight:18,tags:[2024年新款,索尼摄像头,性价比高,学生优惠,拍照神器],merchant_ip:123.244.135.108,business_address:[116.416000,39.902000],delivery_area:{type:polygon,coordinates:[[[116.406,39.912],[116.426,39.912],[116.426,39.892],[116.406,39.892],[116.406,39.912]]]},product_upload_time:2025-03-14 14:20:00,app_version:2.9.1,is_up:true,specifications:{color:珍珠白,size:6.5英寸,brand:小米,material:玻璃},comment:拍照效果很好性价比超高} 3. 查询文档过滤# 查询单个文档[rootes01: ~]# curl -k -u elastic:123456 -X GET https://10.0.0.91:9200/oldboy_index01/_doc/1?pretty# 批量查询多个文档[rootes01: ~]# curl -k -u elastic:123456 -X GET https://10.0.0.91:9200/_mget?pretty -H Content-Type: application/json -d{docs:[{_index:oldboy_index01,_id:2},{_index:oldboy_index01,_id:3}]} # 所有文档 curl -k -u elastic:123456 -X GET https://10.0.0.91:9200/oldboy_index01/_search?pretty # 只显示 _source 数据不显示元数据 [rootes01: ~]# curl -k -u elastic:123456 -X GET https://10.0.0.91:9200/_mget?prettyfilter_pathdocs._source -H Content-Type: application/json -d{docs:[{_index:oldboy_index01,_id:2},{_index:oldboy_index01,_id:3}]}4. 修改文档• 准备环境[rootes01: ~]# curl -k -u elastic:123456 -X PUT https://10.0.0.91:9200/oldboy_index02?pretty -H Content-Type: application/json -d {settings:{number_of_shards:3,number_of_replicas:1}} [rootes01: ~]# curl -k -u elastic:123456 -X PUT https://10.0.0.91:9200/oldboy_index02/_doc/1?pretty -H Content-Type: application/json -d{name:裤子,price:1200,color:黑色}• 全量覆盖修改PUT[rootes01: ~]# curl -k -u elastic:123456 -X PUT https://10.0.0.91:9200/oldboy_index02/_doc/1?pretty -H Content-Type: application/json -d{name:外套}• 局部更新POST_update[rootes01: ~]# curl -k -u elastic:123456 -X PUT https://10.0.0.91:9200/oldboy_index02/_doc/2?pretty -H Content-Type: application/json -d{name:裤子,price:1200,color:黑色} [rootes01: ~]# curl -k -u elastic:123456 -X POST https://10.0.0.91:9200/oldboy_index02/_update/2?pretty -H Content-Type: application/json -d{doc:{price:859,库存:3}} [rootes01: ~]# curl -k -u elastic:123456 -X GET https://10.0.0.91:9200/oldboy_index02/_doc/2?pretty5. 删除文档# 单条删除[rootes01: ~]# curl -k -u elastic:123456 -X DELETE https://10.0.0.91:9200/oldboy_index02/_doc/1?pretty# 批量删除[rootes01: ~]# curl -k -u elastic:123456 -X POST https://10.0.0.91:9200/_bulk?pretty -H Content-Type: application/json -d{delete:{_index:oldboy_index02,_id:2}}