【发布时间】:2015-04-14 09:17:20
【问题描述】:
是否可以在 elasticsearch 中为字段名称设置别名? (就像索引名称的别名一样)
例如:我有一个文档 {'firstname': 'John', 'lastname': 'smith'}
我想将 'firstname' 别名为 'fn'...
【问题讨论】:
标签: elasticsearch lucene
是否可以在 elasticsearch 中为字段名称设置别名? (就像索引名称的别名一样)
例如:我有一个文档 {'firstname': 'John', 'lastname': 'smith'}
我想将 'firstname' 别名为 'fn'...
【问题讨论】:
标签: elasticsearch lucene
只是一个快速更新,Elasticsearch 6.4 提出了一个名为 Alias Datatype 的功能。检查以下映射和查询作为示例。
注意,在下面的字段名fn映射中,字段的类型是alias
PUT myindex
{
"mappings": {
"_doc": {
"properties": {
"firstname": {
"type": "text"
},
"fn": {
"type": "alias",
"path": "firstname"
}
}
}
}
}
GET myindex/_search
{
"query": {
"match" : {
"fn" : "Steve"
}
}
}
这个想法是将alias 用于创建倒排索引的实际字段。请注意,具有别名数据类型的字段不适用于write 操作,它仅用于查询目的。
虽然您可以参考我提到的链接了解更多详细信息,但以下只是其中的一些要点。
single mapping 时使用。索引必须在6.xx 版本之后创建,或者在旧版本中使用设置index.mapping.single_type: true 创建
querying、aggregations、sorting、highlighting 和suggestion 操作alias 字段的 alias
alias。单一别名,单一字段。_source 进行源过滤的一部分。 【讨论】:
没有直接的字段别名功能。但是,您可以使用映射中的 index_name 属性在索引时重命名字段。
index_name :将存储在索引中的字段的名称。 默认为属性/字段名称。
更多信息请看这里:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html
【讨论】:
为现有字段 firstname 添加别名 fn
PUT myindex/_mapping
{
"properties": {
"fn": {
"type": "alias",
"path": "firstname"
}
}
}
从 Elasticsearch 7 开始应该以这种方式工作。
【讨论】:
也许您可以尝试在索引上创建别名,并在所需字段上使用过滤器。您的过滤器必须以从您的字段中选择所有条目的方式编写。请参阅here 中的过滤别名部分。但我有兴趣了解您的用例。为什么要在特定字段上创建别名。
【讨论】: