了解如何创建索引,添加,删除,更新文档
参考文档 开始使用 Elasticsearch 1
本文用到Elasticsearch和Kibana 可以看之前的两篇先安装好
Elasticsearch 安装
Kibana安装
Elasticsearch 里的接口都是通过 REST 接口来实现的。
GET 读取数据
POST 插入数据
PUT 或 PATCH 更新数据,或如果是一个新的 id,则插入数据
DELETE 删除数据
http://localhost:5601/app/dev_tools
GET /
还可以复制为Curl
curl -XGET "https://1270.0.1:9200/" -H "kbn-xsrf: reporting"
同样可以 复制curl至页面就会变成 GET /
查看当前索引的 mapping:
GET dada/_mapping
{
"dada": {
"mappings": {
"properties": {
"cc": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"uid": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
Elasticsearch 的数据类型:
text: 全文搜索字符串
keyword: 用于精确字符串匹配和聚合
date 及 date_nanos: 格式化为日期或数字日期的字符串
byte, short, integer, long: 整数类型
boolean: 布尔类型
float,double,half_float: 浮点数类型
分级的类型:object 及 nested。
创建一个索引 test,并且含有 id 及 message 字段。id 字段为 keyword 类型,而 message 字段为 text 类型,那么我们可以使用如下的方法来创建:
PUT test
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"message": {
"type": "text"
}
}
}
}
#--------返回结果--------
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "test"
}
追加一个新的字段 age,并且它的类型为 long 类型:
PUT test/_mapping
{
"properties": {
"age": {
"type": "long"
}
}
}
#--------返回结果--------
{
"acknowledged": true
}
#查看结果
GET test/_mapping
#--------返回结果--------
{
"test": {
"mappings": {
"properties": {
"age": {
"type": "long"
},
"id": {
"type": "keyword"
},
"message": {
"type": "text"
}
}
}
}
}
_refresh用于使新文档在搜索时可见。
反过来,_flush用于在硬盘上持久化内存段。
_flush不会影响Elasticsearch中文档的可见性,因为搜索是在内存段中进行的,而_refresh会影响它们的可见性。
#存在就会修改
PUT dada/_doc/2
{
"user":"222",
"uid":2,
"cc":"222"
}
#存在就会返回报错
PUT dada/_create/2
{
"user":"222",
"uid":2,
"cc":"222"
}
#效果一样
PUT dada/_doc/2?op_type=create
{
"user":"22222",
"uid":2,
"cc":"222"
}
#--------返回报错--------
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[2]: version conflict, document already exists (current version [2])",
"index_uuid": "gFA5LQXRQIef2WBv3d_aWw",
"shard": "0",
"index": "dada"
}
],
"type": "version_conflict_engine_exception",
"reason": "[2]: version conflict, document already exists (current version [2])",
"index_uuid": "gFA5LQXRQIef2WBv3d_aWw",
"shard": "0",
"index": "dada"
},
"status": 409
}
查询只看_source部分
GET dada/_doc/2
#正常返回
{
"_index": "dada",
"_id": "2",
"_version": 2,
"_seq_no": 6,
"_primary_term": 1,
"found": true,
"_source": {
"user": "222",
"uid": 2,
"cc": "222"
}
}
#--------_source-------
GET dada/_source/2
#返回
{
"user": "222",
"uid": 2,
"cc": "222"
}
自动 ID 生成
使用 POST
POST dada/_doc/
{
"user":"55",
"uid":5,
"cc":"55"
}
#-----返回的结果:-----
{
"_index": "dada",
"_id": "u_g9C4kB2SZh9y2Iu2Gc",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 7,
"_primary_term": 1
}
只读部分数据
GET dada/_doc/2?_source=user,uid
#-----返回的结果------
{
"_index": "dada",
"_id": "2",
"_version": 2,
"_seq_no": 6,
"_primary_term": 1,
"found": true,
"_source": {
"user": "222",
"uid": 2
}
}
GET dada/_source/2?_source=user,uid
#-----返回的结果------
{
"user": "222",
"uid": 2
}
一次请求查找多个文档 _mget
GET _mget
{
"docs":[
{
"_index":"dada",
"_id":2
},
{
"_index":"test",
"_id":"vPhIC4kB2SZh9y2IU2G6"
}
]
}
#-----------返回的结果------------
{
"docs": [
{
"_index": "dada",
"_id": "2",
"_version": 2,
"_seq_no": 6,
"_primary_term": 1,
"found": true,
"_source": {
"user": "222",
"uid": 2,
"cc": "222"
}
},
{
"_index": "test",
"_id": "vPhIC4kB2SZh9y2IU2G6",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"age": 20,
"id": 11,
"message": "lalalalaaaa"
}
}
]
}
也可以只获得部分字段
GET _mget
{
"docs":[
{
"_index":"dada",
"_id":2,
"_source":["user","uid"]
},
{
"_index":"test",
"_id":"vPhIC4kB2SZh9y2IU2G6"
}
]
}
#---------返回的结果---------
{
"docs": [
{
"_index": "dada",
"_id": "2",
"_version": 2,
"_seq_no": 6,
"_primary_term": 1,
"found": true,
"_source": {
"user": "222",
"uid": 2
}
},
{
"_index": "test",
"_id": "vPhIC4kB2SZh9y2IU2G6",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"age": 20,
"id": 11,
"message": "lalalalaaaa"
}
}
]
}
GET _mget
{
"docs":[
{
"_index":"dada",
"_id":2,
"_source":["user","uid"]
},
{
"_index":"dada",
"_id":1
}
]
}
#--------可简写:---------
GET dada/_mget
{
"ids":["1","2"]
}
#------------返回结果------------
{
"docs": [
{
"_index": "dada",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"user": "GB",
"uid": "sss",
"cc": "aa"
}
},
{
"_index": "dada",
"_id": "2",
"_version": 2,
"_seq_no": 6,
"_primary_term": 1,
"found": true,
"_source": {
"user": "222",
"uid": 2,
"cc": "222"
}
}
]
}
修改一个文档
在上面我们看到了可以使用 POST 的命令来修改改一个文档。通常我们使用 POST 来创建一个新的文档。在使用 POST 的时候,我们甚至不用去指定特定的 id,系统会帮我们自动生成。但是我们修改一个文档时,我们通常会使用 PUT 来进行操作,并且,我们需要指定一个特定的 id 来进行修改:
PUT修改时,每一项都会改
PUT test/_doc/1
{
"age":110,
"id":1212
}
使用POST 只改需要改的字段,其他字段会保留下来
POST test/_update/1
{
"doc": {
"age":22222,
"id":20
}
}
先查询后修改
通过查询的方式来进行查询,让后进行修改。ES 也提供了相应的 REST 接口。
会把所有age为22222的都修改
POST test/_update_by_query
{
"query": {
"match": {
"age": "22222"
}
},
"script": {
"source": "ctx._source.id=params.id;ctx._source.message=params.message",
"lang": "painless",
"params":{
"id":888,
"message":"new哈哈哈",
"oth":"????"
}
}
}
可以通过 update 接口,使用 script 的方法来进行修改。这个方法也是需要知道文档的 id
POST test/_update/vPhIC4kB2SZh9y2IU2G6
{
"script": {
"source": "ctx._source.id=params.id;ctx._source.message=params.message",
"lang": "painless",
"params":{
"id":999,
"message":"new999哈哈哈",
"oth":"??999??"
}
}
}
#------返回结果--------
{
"_index": "test",
"_id": "vPhIC4kB2SZh9y2IU2G6",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 9,
"_primary_term": 1
}
在我们使用上面的方法更新文档时,如果当前的文档 id 不存在,那么我们甚至可以使用 upsert 属性来创建一个文档:
POST test/_update/3
{
"script": {
"source": "ctx._source.id=params.id;ctx._source.message=params.message",
"lang": "painless",
"params":{
"id":999,
"message":"new999哈哈哈",
"oth":"??999??"
}
},
"upsert":{
"id":3,
"message":"3333333"
}
}
我们甚至可以使用 _update 接口使用 ctx[‘_op’] 来达到删除一个文档的目的,比如:
当检测文档的 字段id 是否为 888,如果为 888 的话,那么该文档将被删除,否则将不做任何事情。
POST test/_update/1
{
"script": {
"source":"""
if(ctx._source.id == 888){
ctx.op = 'delete'
}else {
ctx.op = 'none'
}
"""
}
}
#------返回---------
{
"_index": "test",
"_id": "1",
"_version": 7,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 11,
"_primary_term": 1
}