【ElasticSearch】简单crul操作crud Sample 01
【使用ES版本】
elasticsearch-7.11.0-linux-x86_64
【列出现有索引】
命令
curl -XGET 'localhost:9200/_cat/indices?v'
执行情况:
[ufo@localhost ~]$ curl -XGET 'localhost:9200/_cat/indices?v' health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open apple rDuNIVRrRYeeozSHltZs2Q 1 1 1 0 6kb 6kb yellow open ibm avStkF4IQbyxaVr3ePFeNQ 1 1 0 0 208b 208b [ufo@localhost ~]$
【插入数据】
准备插入的数据:
curl -H "Content-Type: application/json" -XPUT 'localhost:9200/moft/emp/1?pretty' -d' {"name":"道明寺","age":"21","salary":"10000","hdate":"2002-1-1T12:12:12","title":"教师"}' curl -H "Content-Type: application/json" -XPUT 'localhost:9200/moft/emp/2?pretty' -d' {"name":"花泽类","age":"22","salary":"20000","hdate":"2003-1-1T12:12:12","title":"教师"}' curl -H "Content-Type: application/json" -XPUT 'localhost:9200/moft/emp/3?pretty' -d' {"name":"西门","age":"23","salary":"30000","hdate":"2005-1-1T12:12:12","title":"医生"}' curl -H "Content-Type: application/json" -XPUT 'localhost:9200/moft/emp/4?pretty' -d' {"name":"小泽圆","age":"24","salary":"40000","hdate":"2006-1-1T12:12:12","title":"护士"}'
实际插入情况:
[ufo@localhost ~]$ curl -H "Content-Type: application/json" -XPUT 'localhost:9200/moft/emp/1?pretty' -d' {"name":"...","age":"21","salary":"10000","hdate":"2002-1-1T12:12:12","title":".."}' { "_index" : "moft", "_type" : "emp", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 } [ufo@localhost ~]$ curl -H "Content-Type: application/json" -XPUT 'localhost:9200/moft/emp/2?pretty' -d' {"name":"...","age":"22","salary":"20000","hdate":"2003-1-1T12:12:12","title":".."}' { "_index" : "moft", "_type" : "emp", "_id" : "2", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1 } [ufo@localhost ~]$ curl -H "Content-Type: application/json" -XPUT 'localhost:9200/moft/emp/3?pretty' -d' {"name":"..","age":"23","salary":"30000","hdate":"2005-1-1T12:12:12","title":".."}' { "_index" : "moft", "_type" : "emp", "_id" : "3", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 2, "_primary_term" : 1 } [ufo@localhost ~]$ curl -H "Content-Type: application/json" -XPUT 'localhost:9200/moft/emp/4?pretty' -d' {"name":"...","age":"24","salary":"40000","hdate":"2006-1-1T12:12:12","title":".."}' { "_index" : "moft", "_type" : "emp", "_id" : "4", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 3, "_primary_term" : 1 } [ufo@localhost ~]$
从结果看是从预期走的。
【按ID查找】
插入数据:
curl -H "Content-Type: application/json;charset=utf-8" -XPUT 'localhost:9200/moft/emp/5?pretty' -d' {"name":"宋江","age":"28","salary":"10000","hdate":"2002-1-1T12:12:12","title":"梁山扛把子"}'
执行结果:
[ufo@localhost ~]$ curl -H "Content-Type: application/json;charset=utf-8" -XPUT 'localhost:9200/moft/emp/5?pretty' -d' {"name":"..","age":"28","salary":"10000","hdate":"2002-1-1T12:12:12","title":"....."}' { "_index" : "moft", "_type" : "emp", "_id" : "5", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 5, "_primary_term" : 2 }
按ID查找命令:
url -XGET 'localhost:9200/moft/emp/5?pretty'
执行结果:
[ufo@localhost ~]$ curl -XGET 'localhost:9200/moft/emp/5?pretty' { "_index" : "moft", "_type" : "emp", "_id" : "5", "_version" : 1, "_seq_no" : 5, "_primary_term" : 2, "found" : true, "_source" : { "name" : "..", "age" : "28", "salary" : "10000", "hdate" : "2002-1-1T12:12:12", "title" : "....." } }
从上面的结果看,对中文的支持还有问题。
在浏览器里访问也不对头:
宋江的职称和名字都显示不出来。
【按字段查找】
查找命令:
curl -XGET 'localhost:9200/moft/emp/_search?q=name:道明寺&pretty'
curl -XGET 'localhost:9200/moft/emp/_search?q=age:21&pretty'
执行情况:
[ufo@localhost ~]$ curl -XGET 'localhost:9200/moft/emp/_search?q=name:...&prettry=true' {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"request [/moft/emp/_search] contains unrecognized parameter: [prettry] -> did you mean [pretty]?"}],"type":"illegal_argument_exception","reason":"request [/moft/emp/_search] contains unrecognized parameter: [prettry] -> did you mean [pretty]?"},"status":400}[ufo@localhost ~]$ curl -XGET 'localhost:9200/moft/emp/_search?q=name:...&pretty' { "took" : 815, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 0, "relation" : "eq" }, "max_score" : null, "hits" : [ ] } } [ufo@localhost ~]$ curl -XGET 'localhost:9200/moft/emp/_search?q=age:21&pretty' { "took" : 43, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.2039728, "hits" : [ { "_index" : "moft", "_type" : "emp", "_id" : "1", "_score" : 1.2039728, "_source" : { "name" : "...", "age" : "21", "salary" : "10000", "hdate" : "2002-1-1T12:12:12", "title" : ".." } } ] } } [ufo@localhost ~]$
由上可见,中文支持不好,估计是哪里有问题,而按年龄去找就正确了。
【按范围查找】
命令:
curl -H "Content-Type: application/json" -XGET 'localhost:9200/moft/emp/_search?pretty' -d' { "query":{ "range":{ "salary":{"from":"25000","to":"45000"} } } }'
执行结果:
[ufo@localhost ~]$ curl -H "Content-Type: application/json" -XGET 'localhost:9200/moft/emp/_search?pretty' -d' { > "query":{ > > "range":{ > > "salary":{"from":"25000","to":"45000"} > > } > > } > > }' { "took" : 118, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "moft", "_type" : "emp", "_id" : "3", "_score" : 1.0, "_source" : { "name" : "..", "age" : "23", "salary" : "30000", "hdate" : "2005-1-1T12:12:12", "title" : ".." } }, { "_index" : "moft", "_type" : "emp", "_id" : "4", "_score" : 1.0, "_source" : { "name" : "...", "age" : "24", "salary" : "40000", "hdate" : "2006-1-1T12:12:12", "title" : ".." } } ] } } [ufo@localhost ~]$
如期找出两条记录。
【删除记录】
命令:
curl -XDELETE 'localhost:9200/moft/emp/2?pretty'
执行情况:
[ufo@localhost ~]$ curl -XDELETE 'localhost:9200/moft/emp/2?pretty' { "_index" : "moft", "_type" : "emp", "_id" : "2", "_version" : 2, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 4, "_primary_term" : 1 } [ufo@localhost ~]$
END
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2020-02-21 使用java.io.RandomAccessFile更改文本文件的内容
2014-02-21 Win下执行Swing程序的BAT文件 和 Linux下执行Swing程序的SH文件
2014-02-21 【Canvas与钟表】儿童房表盘