当前位置: 首页 > news >正文

Elasticsearch 核心入门(四)文档操作

一、文档管理

1. 准备环境

• 清空历史记录

[root@es01: ~]# curl -k -u elastic:123456 -X DELETE 'https://10.0.0.91:9200/index*'{"acknowledged":true}[root@es01: ~]# 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}}# 创建索引[root@es01: ~]# curl -k -u elastic:123456 -X PUT "https://10.0.0.91:9200/oldboy_index01?pretty" -H 'Content-Type: application/json' --data-binary @./product.json

2. 创建文档

单条文档创建(指定 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[root@es01:~]# 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创建文档,必须使用POST

curl-k-uelastic:123456-XPOST"https://10.0.0.91:9200/oldboy_index01/_doc?pretty"-H'Content-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":"轻薄设计,拍照效果非常惊艳" }'

• 查询所有文档

[root@es01: ~]# 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 [root@es01: ~]# curl -k -u elastic:123456 -X GET 'https://10.0.0.91:9200/oldboy_index01/_search?pretty&filter_path=hits.hits._id' -H 'Content-Type: application/json' -d'{"query":{"match_all":{}}}'

• 批量创建文档

curl-k-uelastic:123456-XPOST'https://10.0.0.91:9200/_bulk?pretty'-H'Content-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. 查询文档过滤

# 查询单个文档[root@es01: ~]# curl -k -u elastic:123456 -X GET 'https://10.0.0.91:9200/oldboy_index01/_doc/1?pretty'# 批量查询多个文档[root@es01: ~]# 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 数据,不显示元数据 [root@es01: ~]# curl -k -u elastic:123456 -X GET "https://10.0.0.91:9200/_mget?pretty&filter_path=docs._source" -H 'Content-Type: application/json' -d'{"docs":[{"_index":"oldboy_index01","_id":"2"},{"_index":"oldboy_index01","_id":"3"}]}'

4. 修改文档

• 准备环境

[root@es01: ~]# 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}}' [root@es01: ~]# 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)

[root@es01: ~]# 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

[root@es01: ~]# 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":"黑色"}' [root@es01: ~]# 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"}}' [root@es01: ~]# curl -k -u elastic:123456 -X GET 'https://10.0.0.91:9200/oldboy_index02/_doc/2?pretty'

5. 删除文档

# 单条删除[root@es01: ~]# curl -k -u elastic:123456 -X DELETE "https://10.0.0.91:9200/oldboy_index02/_doc/1?pretty"# 批量删除[root@es01: ~]# 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"}}'
http://www.jsqmd.com/news/902700/

相关文章:

  • 告别手动拼接!用Tiled Map Editor + Cocos2d-x 4.0快速制作游戏地图(附完整素材包)
  • Python新手如何快速接入Taotoken调用多款大模型API
  • 绍兴装修公司推荐|2026年6月 避坑必看!本土靠谱装修怎么选,这 8 大雷区千万别踩 - 博客万
  • Windows热键冲突终极解决方案:Hotkey Detective完整实战手册
  • 乌鲁木齐黄金回收乱象曝光:福昌夏教你识破陷阱,安全变现 - 黄金上门回收
  • 2026芜湖市本地人必选的水质检测专业机构TOP7推荐!生活饮用水检测、直饮水检测、污水废水检测、矿泉水检测,正规CMA资质检测公司排名推荐 (2026年5月水质检测最新深度调研方案) - 一修哥咨询
  • Mac菜单栏终极管理指南:用Ice让你的Mac桌面焕然一新
  • 云端IDE故障深度复盘:WebSocket、文件同步与性能优化实战
  • 设计模式分类介绍
  • Clayton vs Gumbel vs Frank:三大参数Copula函数怎么选?看完这篇对比指南就懂了
  • Selenium IDE导出的Python脚本跑不起来?手把手教你解决Edge驱动路径和常见报错
  • 深度解析caj2pdf:逆向工程如何破解知网CAJ格式壁垒
  • 唐山黄金回收测评报告,福昌夏荣膺S级评级标杆 - 黄金上门回收
  • 南京卡地亚 / 梵克雅宝怎么卖?高端首饰回收测评 - 合扬奢侈品交易中心
  • 广安黄金上门回收怎么选?福运来透明靠谱 - 上门黄金回收
  • 冠融集团介绍:2009年成立,旗下有恒发北京冠融等分子公司 - 冠融盈科
  • PatternFly 3性能优化技巧:减少加载时间和提升渲染效率
  • 北京回收黄金2026平台对比+资深避坑经验分享 - 奢侈品回收测评
  • caj2pdf:三步解决知网CAJ文献的跨平台阅读难题
  • Unlock-Music:终极音乐解锁指南 - 如何轻松解密20+种加密音乐格式
  • Windows安卓连接终极解决方案:最新ADB Fastboot驱动一键安装工具
  • 保姆级教程:手把手教你用Autosar MCAL的ICU模块采集PWM信号(基于GTM-CCU6)
  • DDrawCompat:3大核心功能让Windows老游戏在现代系统上完美运行
  • 技术深度解析:Sequential-Hidden-Decoding-8B-n8-Instruct的多流嵌入架构设计
  • 2026徐州市本地人必选的水质检测专业机构TOP7推荐!生活饮用水检测、直饮水检测、污水废水检测、矿泉水检测,正规CMA资质检测公司排名推荐 (2026年5月水质检测最新深度调研方案) - 一修哥咨询
  • 安卓手机本地AI部署实战:从硬件选型到模型量化全指南
  • 苹方字体终极指南:如何在Windows系统上免费获得苹果级排版体验
  • 如何用Joy-Con Toolkit彻底解决Switch手柄漂移问题并实现个性化定制
  • 基于句子级AI检测与人性化改写工具的技术实现与应用
  • 朱砂难辨真假?高纯度朱砂手串怎么选?一物一码保真才安心 - 博客万