【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

posted @   逆火狂飙  阅读(67)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需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与钟表】儿童房表盘
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示