0

问题

我是弹性搜索的新手,我想了解动态映射和重新索引文档之间的关系。

根据我在启用动态映射时的实验,文档的重新索引是自动完成的。这意味着如果您向文档添加一个新字段,它会自动编入索引。

另一方面,如果索引没有开启动态映射,新的映射将不会被索引并且必须执行手动重新索引。其他文件将不会在搜索中找到。

这是一个正确的假设吗。我试图找出有关此的明确信息,但找不到。

你可以在下面找到我的实验

TEST1 - 带映射的索引

# 1.1 create index with mapping dynamic

    PUT /product_dyn_on
        {
            "mappings" : {
                "dynamic": true,
                "properties": {
                    "price": { "type": "integer" }
                }
            }
        }


# 1.2 create a document

        POST /product_dyn_on/_doc/1
        { "price": 100 } 


# 1.3 create a document with a field that was not mapped at the beginning

        POST /product_dyn_on/_doc/2
        { "price": 100, "discount" : 20}


# 1.4 browse document to see if it has was mapped
      GET /product_dyn_on/_search

        GET /product_dyn_on/_search
        { 
          "query": {"term" : {"discount" : 20}}

        }


        GET /product_dyn_on/_search
        { 
          "query": {"match" : {"price" : 100}}

        }


# 1.5 - update first object


    PUT /product_dyn_on/_doc/1
    {
      "price" : 200,
  "discount" : 20
    }

# 1.6 - check results (both objects are returned)


        GET /product_dyn_on/_search
        { 
          "query": {"match" : {"discount" : 20}}

        }

TEST2 - 没有映射的索引

# 2.1 create index with mapping dynamic

        PUT /product_dyn_off
            {
                "mappings" : {
                    "dynamic": false,
                    "properties": {
                        "price": { "type": "integer" },
                        "is_active": {"type": "boolean" },
                        "price": { "type": "integer"},
                        "sold": { "type": "long"}
                    }
                }
            }


    # 2.2 create a document

            POST /product_dyn_off/_doc/1
            { "price": 100 } 


    # 2.3 create a document with a field that was not mapped at the beginning

            POST /product_dyn_off/_doc/2
            { "price": 100, "discount" : 20}


    # 2.4 browse document to see if it has was mapped
          GET /product_dyn_off/_search

            GET /product_dyn_off/_search
            { 
              "query": {"term" : {"discount" : 20}}

            }


            GET /product_dyn_off/_search
            { 
              "query": {"match" : {"price" : 100}}

            }


    # 2.5 - Update first object


        PUT /product_dyn_off/_doc/1
        {
          "price" : 200,
      "discount" : 20
        }

    # 2.6 - Check results

            GET /product_dyn_off/_search

            GET /product_dyn_off/_search
            { 
              "query": {"match" : {"discount" : 20}}

            }

            GET /product_dyn_off/_search
            { 
              "query": {"match" : {"price" : 200}}

            }


    # Clean Everything

        DELETE /product_dyn_on
        DELETE /product_dyn_off
4

1 回答 1

0

动态索引

默认情况下,如果找到任何新字段(null,boolean(true/false),float,integer,object,array,string(date,long,double,text field,with a keyword sub-field))并且相同,elasticsearch 将索引文档我们可以使用动态映射和模板进行自定义 https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-mapping.html

动态关键字

我们可以使用这个关键字来限制索引动态字段。true(默认),false(未编入索引但已存储)和 strict(抛出并拒绝异常) https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic.html

于 2019-09-15T06:50:30.743 回答