Elastic Search基本概念
文档 -> document -> 类似于Mysql中的一条数据
索引-> index ->类似于Mysql中的一张表 (ES6过后索引类似于Mysql的一张表,原先类比于一个database)
对Elastic Search的操作是基于Restful标准, 增删改查对应的Method为POST DELETE PUT GET
查询API:
/GET /{indexName}/{typeName}/id
如 /GET /testindex/doc/1 返回的结构体格式为:
{
"_index" : "testindex",
"_type" : "doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"id" : "1",
"name" : "shunhao"
}
}
各字段含义为:
_index: 索引名
_type: 类型名
_version: 该文档版本,每次更新都会使版本+1
_id: 唯一id
_source: 初始的JSON数据
_all: 整个所有字段内容到该字段,默认禁用,占用磁盘空间较大,不推荐使用
found: 是否找到对应文档
新增API:
/POST /{indexName}/{typeName}
jsonData
如
/POST /testindex/doc
{
“id”:“1”,
“name”:“shunhao”
}
该请求返回的结构体为
{
"_index" : "testindex",
"_type" : "doc",
"_id" : "l59DKnUBNnVcqZYm0Mua",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
es将会为你自动生成一个唯一id
此外也可以指定id来生成,语句与更新相同, 为
/PUT/{indexName}/{typeName}/{id}
jsonData
如
PUT /testindex/doc/1
{
“id”:“1”,
“name”:“shunhao”
}
该语句作用为更新 testindex这个索引下 id为1的数据,若该数据不存在,则新建
删除API:
DELETE /{indexName}/{typeName}/{id}
如
DELETE /testindex/doc/1
返回的结构体为
{
"_index" : "testindex",
"_type" : "doc",
"_id" : "1",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}