Elasticsearch Mapping Index 索引,文字内容来自 B 站中华石杉 Elasticsearch 高手进阶课程,英文内容来自官方文档。
如何建立索引
在建立索引 mapping 的时候,因为 string 类型,所以在早期版本的 Elasticsearch 上,index 字段有三个状态:
PUT website_mapping{ "mappings": { "properties": { "author_id": { "type": "long" }, "title": { "type": "text", "analyzer": "english" }, "content": { "type": "text" }, "post_date": { "type": "date" }, "publisher_id": { "type": "text", "index": "not_analyzed" } } }}
复制代码
得到一个报错信息:
... "caused_by" : { "type" : "illegal_argument_exception", "reason" : "Could not convert [publisher_id.index] to boolean", "caused_by" : { "type" : "illegal_argument_exception", "reason" : "Failed to parse value [not_analyzed] as only [true] or [false] are allowed." }...
复制代码
从官方文档来看,在 mapping 的时候,当前版本(7.1.10) index 的选项只有 true 和 false 两种。
index
The index option controls wether field values are indexed. It accepts true or false and defaults to true. Fields that are not indexed are not queryable.
从 Elastic Blog 上的文章来看,似乎在 5.0 版本之后,就取消了 string 类型。
Strings are dead, long live strings!
the string field has split into two new types: text, which should be used for full-text search, and keyword, which should be used for keyword search.
Fields that used to be mapped as an analyzed string
{ "foo": { "type": "string", "index": "analyzed" }}
复制代码
Now need to be mapped as a text field:
{ "foo": { "type": "text", "index": "true" }}
复制代码
And Fields that used to be mapped as not_analyzed string
{ "foo": { "type": "string", "index": "not_analyzed" }}
复制代码
Now need to be mapped as a keyword field:
{ "foo": { "type": "keyword", "index": true }}
复制代码
所以,之前的脚本改成了
PUT website_mapping{ "mappings": { "properties": { "author_id": { "type": "long" }, "title": { "type": "text", "analyzer": "english" }, "content": { "type": "text" }, "post_date": { "type": "date" }, "publisher_id": { "type": "keyword" } } }}
复制代码
修改 mapping
只能创建 index 时手动建立 mapping,或者新增 field mapping,但是不能 update field mapping
如果试图修改或者更新 field mapping,那么就会收到报错信息
PUT website_mapping{ "mappings": { "properties": { "author_id": { "type": "text" } } }}
{ "error" : { "root_cause" : [ { "type" : "resource_already_exists_exception", "reason" : "index [website_mapping/VbUIuI63STK3E3Wg9i1jRA] already exists", "index_uuid" : "VbUIuI63STK3E3Wg9i1jRA", "index" : "website_mapping" } ], "type" : "resource_already_exists_exception", "reason" : "index [website_mapping/VbUIuI63STK3E3Wg9i1jRA] already exists", "index_uuid" : "VbUIuI63STK3E3Wg9i1jRA", "index" : "website_mapping" }, "status" : 400}
复制代码
测试 mapping
GET website_mapping/_analyze{ "field": "content", "text": "my-dogs"}
{ "tokens" : [ { "token" : "my", "start_offset" : 0, "end_offset" : 2, "type" : "<ALPHANUM>", "position" : 0 }, { "token" : "dogs", "start_offset" : 3, "end_offset" : 7, "type" : "<ALPHANUM>", "position" : 1 } ]}
复制代码
如果是一个不存在的 field,与 5.2 版本不同,7.10.1 版本不会报错,而是会按照默认的分析器得到结果
GET website_mapping/_analyze{ "field": "new_field", "text": "my dogs"}
# 7.10{ "tokens" : [ { "token" : "my", "start_offset" : 0, "end_offset" : 2, "type" : "<ALPHANUM>", "position" : 0 }, { "token" : "dogs", "start_offset" : 3, "end_offset" : 7, "type" : "<ALPHANUM>", "position" : 1 } ]}
# 5.2{ "error": { "root_cause": [ { "type": "remote_transport_exception", "reason": "[4onsTYV][127.0.0.1:9300][indices:admin/analyze[s]]" } ], "type": "illegal_argument_exception", "reason": "Can't process field [new_field], Analysis requests are only supported on tokenized fields" }, "status": 400}
复制代码
评论