目錄:
一、需求
二、數據演示
三、關於es painless 說明
四、參考文章鏈接
一、需求
對nested(數組)進行操作:
- 增加元素
- 刪除元素
- 更新某個元素的值
- 查詢包含有指定特性(如id)的緩存
二、數據演示
PUT /group
{
"mappings": {
"properties": {
"groupId": {"type": "text"},
"groupName": {"type": "keyword"},
"user": {
"type": "nested",
"properties": {
"userId": {
"type": "text"
},
"userName": {
"type": "text"
},
"content": {
"type": "keyword"
}
}
}
}
}
}
#查詢定義的結構
GET group/_mapping
#插入數據
POST group/_doc
{
"groupId": "1001",
"groupName": "聊天1群",
"user":[
{
"userId":"2001",
"userName":"小李",
"content":"作為一級篩選條件單獨使用表示,表示只返回聚合結果,不返回具體數據。"
},
{
"userId":"2002",
"userName":"小王",
"content":"作為一級篩選條件單獨使用表示,表示只返回聚合結果,不返回具體數據。"
},
{
"userId":"2003",
"userName":"小張",
"content":"作為一級篩選條件單獨使用表示,表示只返回聚合結果,不返回具體數據。"
}
]
}
POST group/_doc
{
"groupId": "1002",
"groupName": "聊天2群",
"user":[
{
"userId":"3001",
"userName":"小李2",
"content":"2作為一級篩選條件單獨使用表示,表示只返回聚合結果,不返回具體數據。"
},
{
"userId":"3002",
"userName":"小王2",
"content":"2作為一級篩選條件單獨使用表示,表示只返回聚合結果,不返回具體數據。"
},
{
"userId":"3003",
"userName":"小張2",
"content":"2作為一級篩選條件單獨使用表示,表示只返回聚合結果,不返回具體數據。"
}
]
}
#查詢數據
GET group/_search
#根據嵌套對象外部條件查詢
GET group/_search
{
"query": {
"bool": {
"must": [
{ "match": { "groupId": "1002" }}
]
}
}
}
#根據嵌套對象內部數組條件查詢
GET group/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{ "match": { "user.userId": "3001" }}
]
}
}
}
}
}
#根據嵌套對象內部數組條件查詢且有外部條件
GET group/_search
{
"query":{
"bool":{
"must":[
{"match":{"groupName":"聊天"}},
{
"nested":{
"path":"user",
"query":{
"bool":{
"must":[
{ "match": { "user.userId": "3001" }}
]
}
}
}
}
]
}
}
}
#新增 第一種寫法
POST group/_update/50Bh5H8BmwYplCYFGcvg
{
"script": {
"source": """
if (ctx._source.user == null) {
List ls = new ArrayList();
ls.add(params.user);
ctx._source.user = ls;
} else {
ctx._source.user.add(params.user);
}
""",
"lang": "painless",
"params": {
"user": {
"userId":"3004",
"userName":"小閆",
"content":",不返回具體數據。"
}
}
}
}
#新增 第二種寫法
POST group/_update/50Bh5H8BmwYplCYFGcvg
{
"script" : {
"source": "ctx._source.user.add(params.user)",
"lang": "painless",
"params": {
"user": {
"userId":"3005",
"userName":"小卡",
"content":"不返回具體數據。"
}
}
}
}
#刪除文檔
POST group/_update/50Bh5H8BmwYplCYFGcvg
{
"script": {
"source": "ctx._source.user.removeIf(item -> item.userId == params.userId)",
"lang": "painless",
"params": {
"userId": "3005"
}
}
}
#遍歷修改文檔
POST group/_update/50Bh5H8BmwYplCYFGcvg
{
"script": {
"source": "for (item in ctx._source.user) { if (item['userId'] == params.userId) { item['userName'] = params.userName;item['content'] = params.content}}",
"lang": "painless",
"params": {
"userId":"3004",
"userName": "小閆2",
"content":"update"
}
}
}
#根據下標修改內部文檔
POST group/_update/qpYS5H8BavSRcXzlpytr
{
"script": {
"lang": "painless",
"source": "ctx._source.user[1].content = params.text",
"params": {
"text": "blue"
}
}
}
#批量修改文檔
POST group/_update/5kG_5H8BmwYplCYFsIYV
{
"script": {
"source": "for(def i=0;i<params.update.length;i++) {for (item in ctx._source.user) { if (item['userId'] == params.update[i].userId) { item['userName'] = params.update[i].userName;item['content'] = params.update[i].content}}}",
"lang": "painless",
"params": {
"update":[
{
"userId":"4005",
"userName": "小閆2",
"content":"update"
},
{
"userId":"4006",
"userName": "小閆3",
"content":"update"
}
]
}
}
}
#動態修改字段值
POST group/_update/mEqhIIABmwYplCYFctest
{
"script": {
"source": "for (item in ctx._source.user) { if (item['id'] == params.update.id) { item[params.update.field] = params.update.value;}}",
"lang": "painless",
"params": {
"update":{
"id":"1001",
"field":"userName",
"value": "test"
}
}
}
}
三 painless 說明
上面的演示數據中 script 是 painless的語法,painless是es中對腳本支持較好的。
四、參考文章
#查看插件
GET /_cat/plugins
#查看索引
GET _cat/indices
#刪除索引
DELETE listen_speech_result_srt_index
#查詢索引結構
GET listen_speech_result_doc_index/_mapping
#查詢索引數據
GET listen_speech_result_doc_index_dev/_search
#根據id刪除數據
DELETE listen_speech_result_index_dev/_doc/1001
#分詞效果
GET _analyze
{
"analyzer": "standard",
"text": "奧迪a4l"
}
#修改索引結構
POST _reindex
{
"source": {
"index": "my_index"
},
"dest": {
"index": "my_index2"
}
}
#修改內嵌數組長度限制 默認10000
PUT listen_speech_result_index/_settings
{
"index.mapping.nested_objects.limit":50000
}