PHP

解析ES的document核心元数据:_index,_type,

2018-04-05  本文已影响0人  WinnXUDONG

进入Kibana的DevTools执行下面操作:

#添加一条document
PUT /test_index/test_type/1
{
  "test_content":"test test"
}

#查询
GET /test_index/test_type/1
#返回
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "test_content": "test test"
  }
}

1、 _index元数据解析

2、 _type元数据解析

3、 _id元数据解析

4、document id的手动指定与自动生成两种方式解析

1. 手动指定document id
(1)根据应用情况来说,是否满足手动指定document id的前提:

举个例子,比如说,我们现在在开发一个电商网站,做搜索功能,或者是OA系统,做员工检索功能。这个时候,数据首先会在网站系统或者IT系统内部的数据库中,会先有一份,此时就肯定会有一个数据库的primary key(自增长,UUID,或者是业务编号)。如果将数据导入到es中,此时就比较适合采用数据在数据库中已有的primary key。

#语法:
put /index/type/id
#手动生成id
PUT /test_index/test_type/2
{
  "test_content": "my test"
}

2. 自动生成document id

#语法:
post /index/type
#自动生成id
POST /test_index/test_type
{
  "test_content": "my test"
}
#返回
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "AWKVr3MWWhuqAs-7Mpj5",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

自动生成的id,长度为20个字符,URL安全,base64编码,GUID,分布式系统并行生成时不可能会发生冲突

GUID:GUID算法,可保证在分布式的环境下,不同节点同一时间创建的 _id 一定是不冲突的。


GUID不冲突解释

4、_source元数据以及定制返回结果解析

  1. _source元数据
#添加数据
put /test_index/test_type/1
{
  "test_field1": "test field1",
  "test_field2": "test field2"
}

#获取
get /test_index/test_type/1
#返回
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "1",
  "_version": 2,
  "found": true,
  "_source": {
    "test_field1": "test field1",
    "test_field2": "test field2"
  }
}

_source元数据:就是说,我们在创建一个document的时候,使用的那个放在request body中的json串(所有的field),默认情况下,在get的时候,会原封不动的给我们返回回来。

  1. 定制返回结果

定制返回的结果,指定_source中,返回哪些field

#语法:
GET /test_index/test_type/1?_source=test_field2
#返回
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "1",
  "_version": 2,
  "found": true,
  "_source": {
    "test_field2": "test field2"
  }
}

#也可返回多个field使用都好分割
GET /test_index/test_type/1?_source=test_field2,test_field1
上一篇 下一篇

猜你喜欢

热点阅读