栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 前沿技术 > 大数据 > 大数据系统

谷粒商城高级篇之es的基本语法

大数据系统 更新时间:发布时间: 百科书网 趣学号
基本语法 1.cat
GET  /_cat/nodes:查看所有节点
GET  /_cat/health:查看es健康状况
GET  /_cat/master:查看主节点
GET  /_cat/indices:查看所有索引
2.索引一个文档
PUT mytest/basic/1   //保存在mytest索引下的basic类型id为1号的数据
{
	“name":"test"
}

返回信息

{
    "_index": "mytest", //索引名称
    "_type": "basic",	//类型
    "_id": "1",		//id
    "_version": 1,	//版本
    "result": "created",	//操作
    "_shards": {		
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

POST不带id为新增操作,带了id则查看存在,则为修改,其他为新增。

put则必须带id,不然无法发送

3.查看文档
GET mytest/basic/1   //查看在mytest索引下的basic类型id为1号的数据

返回结果

{
    "_index": "mytest",  //索引
    "_type": "basic",	//类型
    "_id": "1",		//记录id
    "_version": 1,	//版本号
    "_seq_no": 0, 	//并发字段,更新+1。,可以做乐观锁
    "_primary_term": 1,	//主分片
    "found": true,	
    "_source": { //内容
        "name": "test"
    }
}
4.更新文档
POST mytest/basic/1/_update  //带来_update则会对比原来的数据,不相同版本才+,相同则不改变。且需要编写doc
{ 
	 "doc":{
        "name":"mytest"
    }
}
POST mytest/basic/1/  //不带update,则直接修改,版本+
{
	 "name":"mytest"
}

put和不带_update的post都会直接更新数据

5.删除文档
DELETE mytest/basic/1 //删除mytest索引下的basic类型的1号数据
DELETE mytest删除一个索引
6. bulk批量API
POST /mytest/basic/_bulk
{"index":{"_id":"1"}}
{"name":"test"}
{"index":{"_id":"2"}}
{ "name":"test2"}

返回结果

{
  "took" : 12, //花费时间
  "errors" : false, //没有错误
  "items" : [0
    {
      "index" : {  //第一个数据
        "_index" : "mytest",
        "_type" : "basic",
        "_id" : "1",
        "_version" : 5,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 4,
        "_primary_term" : 1,
        "status" : 200
      }
    },
    {		
      "index" : {		//第二个数据
        "_index" : "mytest",
        "_type" : "basic",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 5,
        "_primary_term" : 1,
        "status" : 201
      }
    }
  ]
}
POST /_bulk
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }} //删除website索引下blog类型下的id为123的记录,后面的以之前的类推即可
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title":	"My first blog post" }
{ "index":	{ "_index": "website", "_type": "blog" }}
{ "title":	"My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123"}}
{ "doc" : {"title" : "My updated blog post"} }

批量数据测试:https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json?raw=true

POST bank/account/_bulk //后面接受批量处理的json


转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/601752.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号