啥也不说,先附上文档链接
https://www.elastic.co/guide/en/elasticsearch/reference/7.17/field-alias.html#field-alias
本次就做一次 alias 的注意事项笔记,希望对大伙(不爱翻官方文档的)有点帮助(o゜▽゜)o☆[BINGO!]
首先简要描述一下 Alias 。它是别名,为索引中的字段定义替代名称,可以对别名搜索达到搜索映射来源的效果。
那么正文开始:
Alias targetsedit
There are a few restrictions on the target of an alias:
目标必须是一个明确的字段,不能是一个object对象或者其它别名。
错误尝试:指定的目标为 object
ps(name目标由于没有指定类型,且是一个json对象,所以是一个object类型)
PUT alias_test
{
"mappings": {
"properties": {
"name_content": {
"type": "alias",
"path": "name"
},
"name": {
"first": "John",
"last": "Smith"
}
}
}
}
*********************result*************************
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "No type specified for field [name]"
}
],
"type" : "mapper_parsing_exception",
"reason" : "Failed to parse mapping [_doc]: No type specified for field [name]",
"caused_by" : {
"type" : "mapper_parsing_exception",
"reason" : "No type specified for field [name]"
}
},
"status" : 400
}
错误尝试:指定的目标为 其它的别名
由于别名本身并不会作为真实字段进行存储与_source,所以当指定的目标为其它别名时,所报的提示错误会和指定了一个不存在字段的错误信息一致。
PUT alias_test
{
"mappings": {
"properties": {
"name_content": {
"type": "alias",
"path": "name"
},
"name": {
"type": "alias",
"path": "name_content"
}
}
}
}
*********************result*************************
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "Invalid [path] value [name] for field alias [name_content]: an alias must refer to an existing field in the mappings."
}
],
"type" : "mapper_parsing_exception",
"reason" : "Invalid [path] value [name] for field alias [name_content]: an alias must refer to an existing field in the mappings."
},
"status" : 400
}
在创建别名时,目标字段必须已经存在。
错误尝试:指定的目标字段不存在
PUT alias_test
{
"mappings": {
"properties": {
"name_content": {
"type": "alias",
"path": "name"
}
}
}
}
*********************result*************************
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "Invalid [path] value [name] for field alias [name_content]: an alias must refer to an existing field in the mappings."
}
],
"type" : "mapper_parsing_exception",
"reason" : "Invalid [path] value [name] for field alias [name_content]: an alias must refer to an existing field in the mappings."
},
"status" : 400
}
如果定义了嵌套对象,则字段别名必须与其目标具有相同的嵌套作用域。
en…这点博主没搞懂,望大伙解惑。
字段别名只允许有一个目标字段。
错误尝试:字段别名指定数组
它会将你的数组识别成一个目标字段,所以会报找寻不到此字段的错误。
如下,它会把数组识别成 “[name, address]” 字段,若是你有一个字段的名称 为 “[name, address]”,那倒是会添加成功。
PUT alias_test
{
"mappings": {
"properties": {
"name_content": {
"type": "alias",
"path": ["name","address"]
},
"name": {
"type": "keyword"
},
"address": {
"type": "keyword"
}
}
}
}
*********************result*************************
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "Invalid [path] value [[name, address]] for field alias [name_content]: an alias must refer to an existing field in the mappings."
}
],
"type" : "mapper_parsing_exception",
"reason" : "Invalid [path] value [[name, address]] for field alias [name_content]: an alias must refer to an existing field in the mappings."
},
"status" : 400
}
************************* success ***************************
PUT alias_test
{
"mappings": {
"properties": {
"name_content": {
"type": "alias",
"path": ["name","address"]
},
"[name, address]": {
"type": "keyword"
}
}
}
}
试图在索引或更新请求中使用别名将导致失败。
错误尝试:新增数据中,包含别名字段
PUT alias_test
{
"mappings": {
"properties": {
"name_content": {
"type": "alias",
"path": "name"
},
"name": {
"type": "keyword"
}
}
}
}
PUT /alias_test/_doc/1
{
"name": "Benedict Cumberbatch",
"name_content": "handsome"
}
************************* result ***************************
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "failed to parse"
}
],
"type" : "mapper_parsing_exception",
"reason" : "failed to parse",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Cannot write to a field alias [name_content]."
}
},
"status" : 400
}
aliases 不能当作 copy_to 的目标。
**错误尝试:aliases 被当作了 copy_to 的目标 **
因为 copy_to 的目标必须为存在的字段,而 alias 并不是。
创建索引的时候并不会失败,能创建成功,但是添加数据的时候会失败。
PUT alias_copy_to_test
{
"mappings": {
"properties": {
"name_content": {
"type": "alias",
"path": "name"
},
"name": {
"type": "keyword"
},
"last_name": {
"type": "keyword",
"copy_to": "name_content"
}
}
}
}
PUT alias_copy_to_test/_doc/1
{
"name": "Benedict Cumberbatch",
"name_content": "handsome",
"last_name": "Sherlock"
}
************************* result ***************************
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "failed to parse"
}
],
"type" : "mapper_parsing_exception",
"reason" : "failed to parse",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Cannot write to a field alias [name_content]."
}
},
"status" : 400
}
由于别名并不是真正存储于_source的字段,所以对_source进行查询是无效的。
PUT alias_test
{
"mappings": {
"properties": {
"name_content": {
"type": "alias",
"path": "name"
},
"name": {
"type": "keyword"
}
}
}
}
PUT /alias_test/_doc/1
{
"name": "Benedict Cumberbatch"
}
GET /alias_test/_search
************************* result ***************************
"hits" : [
{
"_index" : "alias_test",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "Benedict Cumberbatch"
}
}
]
结果并没有别名字段。
这次的笔记希望对大伙能有点收获,其中博主也有一个地方不太明白,
If nested objects are defined, a field alias must have the same nested scope as its target.
希望大伙能帮帮忙,解解惑┗|`0′|┛
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_29064815/article/details/122992466
内容来源于网络,如有侵权,请联系作者删除!