elasticsearch Alias field type 使用注意事项

x33g5p2x  于2022-02-18 转载在 ElasticSearch  
字(7.1k)|赞(0)|评价(0)|浏览(648)

Alias field type

啥也不说,先附上文档链接
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:

The target must be a concrete field, and not an object or another field 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
}

The target field must exist at the time the alias is created.

在创建别名时,目标字段必须已经存在。
错误尝试:指定的目标字段不存在

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
}

If nested objects are defined, a field alias must have the same nested scope as its target.

如果定义了嵌套对象,则字段别名必须与其目标具有相同的嵌套作用域。
en…这点博主没搞懂,望大伙解惑。

A field alias can only have one target

字段别名只允许有一个目标字段。
错误尝试:字段别名指定数组
它会将你的数组识别成一个目标字段,所以会报找寻不到此字段的错误。
如下,它会把数组识别成 “[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"
      }
    }
  }
}

Attempting to use an alias in an index or update request will result in a failure.

试图在索引或更新请求中使用别名将导致失败。

错误尝试:新增数据中,包含别名字段

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 cannot be used as the target of copy_to

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
}

Because alias names are not present in the document source, aliases cannot be used when performing source filtering

由于别名并不是真正存储于_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"
        }
      }
    ]

结果并没有别名字段。

end

这次的笔记希望对大伙能有点收获,其中博主也有一个地方不太明白,
If nested objects are defined, a field alias must have the same nested scope as its target.
希望大伙能帮帮忙,解解惑┗|`0′|┛

相关文章