elasticsearch - 如何更新 ElasticSearch 索引映射并向其中添加多字段?

标签 elasticsearch

我已经使用以下设置和映射创建了索引:

PUT test

    {
        "settings": {
            "number_of_shards": 2,
            "number_of_replicas": 1
        },
        "mappings": {
            "documents": {
                "properties": {
                    "title": {
                        "type": "text",
                        "fields": {
                            "raw": {
                                "type": "keyword"
                            }
                        }
                    },
                    "url": {
                        "type": "text",
                        "fields": {
                            "raw": {
                                "type": "keyword"
                            }
                        }
                    },
                    "tags": {
                        "type": "keyword"
                    }
                }
            }
        }
    }

在上面的映射中,我已经将标签声明为完全匹配的类型关键字。并在其中添加了 10 个文档,例如:

PUT test/documents/1
{
  "title":"John",
  "url":"/john",
  "tags":["name question","how"]
}

但我想为标签添加多字段支持以支持部分标签匹配,所以我更新了索引映射如下:

PUT test/documents/_mapping
{
    "properties": {
        "title": {
            "type": "text",
            "fields": {
                "raw": {
                    "type": "keyword"
                }
            }
        },
        "url": {
            "type": "text",
            "fields": {
                "raw": {
                    "type": "keyword"
                }
            }
        },
        "tags": {
            "type": "keyword",
            "fields": {
                "raw": {
                    "type": "text"
                }
            }
        }
    }
}

这是更新索引映射的正确方法吗?我的标签现在可以支持部分匹配吗? 请帮我解决这个问题

最佳答案

是的。这就是更新该字段的方式。尽管我不会使用 raw 作为子字段的名称。将 raw 作为 text 类型并不常见。 例如,我将其命名为 fulltext

无论如何,即使您更新了映射,您也必须重新索引所有文档。

Update by Query会这样做:

curl -X POST "localhost:9200/test/_update_by_query?conflicts=proceed"

顺便说一句(与您的问题无关)我将使用 _doc 作为类型名称,这样您就可以为将来删除类型做好更充分的准备。

关于elasticsearch - 如何更新 ElasticSearch 索引映射并向其中添加多字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51474413/

相关文章:

testing - 检查Elasticsearch是否已经完成索引

elasticsearch - Elasticsearch 'between'查询

elasticsearch - ElasticSearch使用数组进行查询

Elasticsearch:从/到/elastic 更改根 url

elasticsearch - 重新索引Elasticsearch,忽略不在映射中的字段

elasticsearch - 在ElasticSearch 6中使用过滤器按嵌套对象聚合

java - spring-data-elasticsearch 中的 XSD 验证错误

elasticsearch - 在一台机器上运行多个Elasticsearch生产节点

elasticsearch - Elasticsearch嵌套对象自动完成

reactjs - 如何从React前端获取 Elasticsearch 映射属性名称