一、索引操作

1、查看当前节点的所有的index

查看当前节点的所有的index

[root@es1 ~]# curl -X GET 'http://10.87.6.2:9200/_cat/indices?v'
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   students Btx9nIaLQ1GnVqy5ncbipg   5   1          6            0       48kb           24kb

 

2、查看每个index下的type和filed的对应的数据类型

查看每个index下的type和filed的类型
[root@es1 ~]# curl 'http://10.87.6.2:9200/_mapping?pretty=true'
{
  "students" : {
    "mappings" : {
      "class1" : {
        "properties" : {
          "passwd" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "username" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

 

 

3、创建索引的操作

创建索引,返回true,意味着创建成功了
[root@es1 ~]# curl -X PUT 'http://10.87.6.2:9200/weather'
{"acknowledged":true,"shards_acknowledged":true,"index":"weather"}[root@es1 ~]# 



创建完成后,查看索引
[root@es1 ~]# curl -X GET 'http://10.87.6.2:9200/_cat/indices?v'
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   students Btx9nIaLQ1GnVqy5ncbipg   5   1          6            0       48kb           24kb
green  open   weather  VWnR2eM9RIG8A0MJgNWORw   5   1          0            0      1.5kb           690b

 

4、删除索引操作

删除索引,返回true,意味着删除成功
[root@es1 ~]# curl -X DELETE 'http://10.87.6.2:9200/weather'
{"acknowledged":true}[root@es1 ~]#


删除完成后,查看索引
[root@es1 ~]# curl -X GET 'http://10.87.6.2:9200/_cat/indices?v'
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   students Btx9nIaLQ1GnVqy5ncbipg   5   1          6            0       48kb           24kb
[root@es1 ~]# 

 

二、文档操作

1、新增一个文档记录,指定索引为数字

[root@es1 ~]# curl -XPUT 'http://10.87.6.2:9200/students/class1/7?pretty' -d '{"usernmae":"baoliang","passwd":"44444444"}' -H "Content-Type: application/json"
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "7",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}


查看新增的记录
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/7?pretty'
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "7",
  "_version" : 1,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "usernmae" : "baoliang",
    "passwd" : "44444444"
  }
}

 

2、创建一个文档,指定索引为字符串

这里我们注意,这条文档的id为7,但是id不一定为7,为abc也是可以的

[root@es1 ~]# curl -XPUT 'http://10.87.6.2:9200/students/class1/abc?pretty' -d '{"usernmae":"wxz","passwd":"5555555555"}' -H "Content-Type: application/json"
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "abc",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/abc?pretty'
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "abc",
  "_version" : 1,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "usernmae" : "wxz",
    "passwd" : "5555555555"
  }
}

 

3、创建文档,不指定索引,由elasticsearch为我们指定索引,但是方法要采用XPOST方法,不能使用XPUT方法

这里还需要注意,上面都指定id了,我们有可以不指定id进行新增文档,但是不能XPUT的方法,要用XPOST方法
[root@es1 ~]# curl -XPOST 'http://10.87.6.2:9200/students/class1/' -d '{"usernmae":"wxz","passwd":"5555555555"}' -H "Content-Type: application/json"
{"_index":"students","_type":"class1","_id":"2oUVBmkBGaSC379Rl48e","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":3,"_primary_term":1}[root@es1 ~]# 


我们看到这次的id是随机生成的2oUVBmkBGaSC379Rl48e

通过这个随机的id我们查看我们的文档
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48e?pretty'
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2oUVBmkBGaSC379Rl48e",
  "_version" : 1,
  "_seq_no" : 3,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "usernmae" : "wxz",
    "passwd" : "5555555555"
  }
}

 

4、这里还需要注意一点

这里还需要注意,如果我们输入的index不存在,elasticsearch也不会报错,他会为我们新建一个索引,所以这里要非常的注意,index一定不能写错

 

5、查看文档的操作

查看某个文档的
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48e?pretty=true'
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2oUVBmkBGaSC379Rl48e",
  "_version" : 1,
  "_seq_no" : 3,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "usernmae" : "wxz",
    "passwd" : "5555555555"
  }
}



如果id输错了,就会查不到数据
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48?pretty=true'
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2oUVBmkBGaSC379Rl48",
  "found" : false
}

 

6、删除文档的操作

删除记录的方法,输入index,type,id就可以删除指定的文档
[root@es1 ~]# curl -XDELETE 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48e'
{"_index":"students","_type":"class1","_id":"2oUVBmkBGaSC379Rl48e","_version":2,"result":"deleted","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":4,"_primary_term":1}[root@es1 ~]#


删除成功后,我们在查看这个文档,就已经查不到了
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/2oUVBmkBGaSC379Rl48?pretty=true'
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2oUVBmkBGaSC379Rl48",
  "found" : false
}

 

 

7、更新操作,采用_update方法

更新操作
[root@es1 ~]# curl -XPOST 'http://10.87.6.2:9200/students/class1/7/_update?pretty=true' -d '{"doc":{"passwd":"55555555"}}' -H "Content-Type: application/json"
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "7",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 5,
  "_primary_term" : 1
}


查看更新后的结果
[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/7?pretty=true'
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "7",
  "_version" : 2,
  "_seq_no" : 5,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "usernmae" : "baoliang",
    "passwd" : "55555555"
  }
}

 

 

8、覆盖操作,采用XPUT方法,id输入已有的id,也就是我们要覆盖的文档的id

xput方法是覆盖,update方法更新,上面我们介绍了一下update方法,下面我们看下xput方法,我们看到新的数据只有一个字段了,username字段被覆盖掉了


[root@es1 ~]# curl -XPUT 'http://10.87.6.2:9200/students/class1/7?pretty=true' -d '{"passwd":"55555555"}' -H "Content-Type: application/json"
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "7",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 6,
  "_primary_term" : 1
}



[root@es1 ~]# curl -XGET 'http://10.87.6.2:9200/students/class1/7?pretty=true'
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "7",
  "_version" : 3,
  "_seq_no" : 6,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "passwd" : "55555555"
  }
}

 

至此,我们的elasticsearch的索引操作和文档操作的总结暂时就完成了!

 

版权声明:本文为bainianminguo原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/bainianminguo/p/10402535.html