elasticsearch(三):7.x 版本 基础增删改查 index doc _update 入门的第二步

参考官网

 1、简单的增删改查,默认分片,默认类型

 

[root@vcontroller ~]# curl -X PUT "localhost:9200/customer?pretty"
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "customer"
}
[root@vcontroller ~]# curl -X GET 'localhost:9200/_cat/indices?v'
health status index     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   commodity 0RlMh0czQPyNfIDTn5fkEg   1   1          0            0       208b           208b
yellow open   customer  Mb-4XQCzRDyf2Ins9oAJIg   1   1          0            0       208b           208b
[root@vcontroller ~]# curl -X PUT "localhost:9200/customer/doc/1?pretty" -d '{"name": "shunzi2016"}'
{
  "error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",
  "status" : 406
}
[root@vcontroller ~]# curl -X PUT "localhost:9200/customer/doc/1?pretty" -H "Content-Type: application/json" -d '{"name": "shunzi2016"}'
{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
[root@vcontroller ~]# curl -X GET "localhost:9200/customer/doc/1?pretty"
{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "shunzi2016"
  }
}
[root@vcontroller ~]# curl -X DELETE "localhost:9200/customer/doc/1?pretty"
{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}
[root@vcontroller ~]# curl -X GET "localhost:9200/customer/doc/1?pretty"
{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "found" : false
}
[root@vcontroller ~]# curl -X PUT "localhost:9200/customer/doc/1?pretty" -H "Content-Type:application/json" -d '{"name": "shunzi2016"}'
{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}
[root@vcontroller ~]# curl -X POST "localhost:9200/customer/doc/1/_update?pretty" -H "Content-Type:application/json" -d '{"doc": {"name":"shunzi2016", "age": 30}}'
{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}
[root@vcontroller ~]# curl -X GET "localhost:9200/customer/doc?pretty"
{
  "error" : "Incorrect HTTP method for uri [/customer/doc?pretty] and method [GET], allowed: [POST]",
  "status" : 405
}
[root@vcontroller ~]# curl -X GET "localhost:9200/customer/doc/1?pretty"
{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 3,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "shunzi2016",
    "age" : 30
  }
}
[root@vcontroller ~]# curl -X GET "localhost:9200/customer"
{"customer":{"aliases":{},"mappings":{"properties":{"age":{"type":"long"},"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"settings":{"index":{"routing":{"allocation":{"include":{"_tier_preference":"data_content"}}},"number_of_shards":"1","provided_name":"customer","creation_date":"1611825831984","number_of_replicas":"1","uuid":"Mb-4XQCzRDyf2Ins9oAJIg","version":{"created":"7100299"}}}}}[root@vcontroller ~]# curl -X GET "localhost:9200/customer/doc"
{"error":"Incorrect HTTP method for uri [/customer/doc] and method [GET], allowed: [POST]","status":405}[root@vcontroller ~]# 
[root@vcontroller ~]# 

# 部分更新
[root@vcontroller ~]# curl -X POST "localhost:9200/customer/doc/1/_update?pretty" -H "Content-Type:application/json" -d '{"doc": {"name":"shunzi20162222", "age": 30}}'
{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}
[root@vcontroller ~]# curl -X GET "localhost:9200/customer/doc/1?pretty"
{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "shunzi20162222",
    "age" : 30
  }
}

1.1 可以越过单独创建索引的过程,直接创建索引以及插入数据

[root@vcontroller ~]# curl -X PUT -H "Content-Type:application/json" "localhost:9200/person/info/01" -d '{"id":"23456","name":"张三"}'
{"_index":"person","_type":"info","_id":"01","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

 

 1.2 在DSL语句结尾加上?pretty  可以更清晰直观的看到执行结果,否则当数据量大时,会眼花缭乱

[root@vcontroller ~]# curl -X PUT -H "Content-Type:application/json" "localhost:9200/person/info/03?pretty" -d '{"id":"23ssssssw","name":"王八"}' 
{
  "_index" : "person",
  "_type" : "info",
  "_id" : "03",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

1.3  针对同 id,不同内容  的数据,put 两次会直接进行覆盖

_update  是部分更新,

[root@vcontroller ~]# curl -X PUT -H "Content-Type:application/json" "localhost:9200/person/info/04?pretty" -d '{"id":"5","name":"QQ"}' 
{
  "_index" : "person",
  "_type" : "info",
  "_id" : "04",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

[root@vcontroller ~]# curl -X GET localhost:9200/person/info/04?pretty
{
  "_index" : "person",
  "_type" : "info",
  "_id" : "04",
  "_version" : 1,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "id" : "5",
    "name" : "QQ"
  }
}

[root@vcontroller ~]# curl -X PUT -H "Content-Type:application/json" "localhost:9200/person/info/04?pretty" -d '{"id":"5","name":"微信"}' 
{
  "_index" : "person",
  "_type" : "info",
  "_id" : "04",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 5,
  "_primary_term" : 1
}
[root@vcontroller ~]# curl -X GET localhost:9200/person/info/04?pretty
{
  "_index" : "person",
  "_type" : "info",
  "_id" : "04",
  "_version" : 2,
  "_seq_no" : 5,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "id" : "5",
    "name" : "微信"
  }
}

1.4 查看

2、条件查询

2.1 按照 索引,类型, id 查询

[root@vcontroller ~]# curl -X GET localhost:9200/ykc/active/01?pretty
{
  "_index" : "ykc",
  "_type" : "active",
  "_id" : "01",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "id" : "121213",
    "name" : "李四"
  }
}

2.2 无条件查询全部数据

[root@vcontroller ~]# curl -X GET localhost:9200/_search?pretty
{
  "took" : 111,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "customer",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "韩小顺2222",
          "age" : 30
        }
      },
      {
        "_index" : "person",
        "_type" : "info",
        "_id" : "01",
        "_score" : 1.0,
        "_source" : {
          "id" : "121213",
          "name" : "李四"
        }
      }
    ]
  }
}

2.3 模糊查询

[root@vcontroller ~]# curl -H 'Content-Type: application/json' localhost:9200/person/info/_search?pretty -d '{"query":{"match":{"name":"李"}}}'
{
  "took" : 44,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "person",
        "_type" : "info",
        "_id" : "01",
        "_score" : 0.2876821,
        "_source" : {
          "id" : "121213",
          "name" : "李四"
        }
      }
    ]
  }
}

2.4 模糊查询,返回指定的字段列数据

[root@vcontroller ~]# curl -H 'Content-Type: application/json' 'localhost:9200/person/info/_search?_source=name&pretty=true' -d '{"query":{"match":{"name":"李"}}}'
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "person",
        "_type" : "info",
        "_id" : "01",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "李四"
        }
      }
    ]
  }
}

2.5 引号的使用错误: 双重 双引号 

[root@vcontroller ~]# curl -X PUT -H "Content-Type:application/json" "localhost:9200/person/info/01" -d "{"id":"23456","name":"张三"}"
{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse"}],"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"json_parse_exception","reason":"Unexpected character ('i' (code 105)): was expecting double-quote to start field name\n at [Source: (byte[])\"{id:23456,name:张三}\"; line: 1, column: 3]"}},"status":400}

2.6 查询当前类型所有数据:

curl -X GET localhost:9200/person/info/_search?pretty -d '{"query":{"match_all":{}}}' -H "Content-Type:application/json"

curl -X GET localhost:9200/person/info/_search?pretty

curl -X GET localhost:9200/person/_search?pretty

]# curl -X GET localhost:9200/person/info/_search?pretty
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "person",
        "_type" : "info",
        "_id" : "01",
        "_score" : 1.0,
        "_source" : {
          "id" : "23456",
          "name" : "张三"
        }
      },
      {
        "_index" : "person",
        "_type" : "info",
        "_id" : "02",
        "_score" : 1.0,
        "_source" : {
          "id" : "23ssssssw",
          "name" : "赵六"
        }
      },
      {
        "_index" : "person",
        "_type" : "info",
        "_id" : "03",
        "_score" : 1.0,
        "_source" : {
          "id" : "23ssssssw",
          "name" : "王八"
        }
      },
      {
        "_index" : "person",
        "_type" : "info",
        "_id" : "04",
        "_score" : 1.0,
        "_source" : {
          "id" : "5",
          "name" : "微信"
        }
      }
    ]
  }
}

2.7 查询 当前类型的所有数据 2

 

你可能感兴趣的:(Java,数据库,elasticsearch,_update,indices,curl)