【问题标题】:Elasticsearch, how to check if my dynamic mapping works?Elasticsearch,如何检查我的动态映射是否有效?
【发布时间】:2016-08-26 21:03:55
【问题描述】:

我在 elasticsearch 中创建索引时提供了一个默认映射动态模板,并想检查它是否按预期工作。让我难过,我如何验证它是否有效? (使用 ES 2.2.2)

"mappings": {
    "_default_": {
        "dynamic_templates": [
            {
                "no_date_detection": {
                    "match_mapping_type": "string",
                    "mapping": {
                        "type": "string",
                        "date_detection": false
                    }
                }
            },
            {
                "language_de": {
                    "match_mapping_type": "*",
                    "match": "*_de",
                    "mapping": {
                        "type": "string",
                        "analyzer": "german"
                    }
                }
            },
            {
                "language_es": {
                    "match":              "*_es",
                    "match_mapping_type": "*",
                    "mapping": {
                        "type":           "string",
                        "analyzer":       "spanish"
                    }
                }
            },
            {
                "language_en": {
                    "match":              "*_en",
                    "match_mapping_type": "*",
                    "mapping": {
                        "type":           "string",
                        "analyzer":       "english"
                    }
                }
            }
        ]
    }
}

这非常简单,就像文档中提供的示例一样。

获取映射表明动态模板被传递给新类型

 "testobject": {
    "dynamic_templates": [
       {
          "no_date_detection": {
             "mapping": {
                "type": "string",
                "date_detection": false
             },
             "match_mapping_type": "string"
          }
       },
       {
          "language_de": {
       ...

但是当我创建一个带有新字段的对象时 "description_en": "一些英文文本" 并获取它刚刚显示的映射

       "description_en": {
          "type": "string"
       }

这不应该有 “分析器”:“英语” 在里面?

我做错了什么,如果我的动态映射是正确的,我如何验证它是否被应用?

提前致谢/Carsten


作为我的问题“我如何验证它是否被应用?”似乎不清楚,我尝试简化:

  1. 我使用默认动态映射创建索引。
  2. 我创建了一个类型“testobject”。
  3. “GET /myindex/testobject/_mappings”验证动态模板是否按预期传递给类型。
  4. 我在 testobject 类型的对象中创建了一个新字段。
  5. "GET /myindex/testobject/_mappings" 显示新字段,但没有说'"date_detection": false'。它将它显示为一个简单的字符串(见上文)。

如何验证动态模板是否/是否已应用于新创建的字段?


简化示例:

PUT /myindex
{
    "mappings": { 
        "_default_": {
            "dynamic_templates": [
                {
                    "no_date_detection": {
                        "match_mapping_type": "string",
                        "mapping": {
                            "type": "string",
                            "date_detection": false
                        }
                    }
                }
            ]
        }
    }
}


PUT /myindex/gardeners/1
{
    "name": "gary"
}

GET /myindex/_mapping

{
   "myindex": {
      "mappings": {
         "gardeners": {
            "dynamic_templates": [
               {
                  "no_date_detection": {
                     "mapping": {
                        "type": "string",
                        "date_detection": false
                     },
                     "match_mapping_type": "string"
                  }
               }
            ],
            "properties": {
               "name": {
                  "type": "string"
               }
            }
         },
         "_default_": {
            "dynamic_templates": [
               {
                  "no_date_detection": {
                     "mapping": {
                        "type": "string",
                        "date_detection": false
                     },
                     "match_mapping_type": "string"
                  }
               }
            ]
         }
      }
   }
}

我的新字段“名称”的映射

"properties": {
    "name": {
        "type": "string"
    }
}

不包含 "date_detection": false 为什么不流传下来?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    动态模板按照定义的顺序进行检查,第一个匹配的即被应用的模板。

    在您的情况下,no_date_detection 模板与您的字段 description_en 匹配,因为它是 string

    如果您希望该字段与english 分析器一起使用,那么您需要更改模板的顺序:

      "mappings": {
        "_default_": {
          "dynamic_templates": [
            {
              "language_de": {
                "match_mapping_type": "*",
                "match": "*_de",
                "mapping": {
                  "type": "string",
                  "analyzer": "german"
                }
              }
            },
            {
              "language_es": {
                "match": "*_es",
                "match_mapping_type": "*",
                "mapping": {
                  "type": "string",
                  "analyzer": "spanish"
                }
              }
            },
            {
              "language_en": {
                "match": "*_en",
                "match_mapping_type": "*",
                "mapping": {
                  "type": "string",
                  "analyzer": "english"
                }
              }
            },
            {
              "no_date_detection": {
                "match_mapping_type": "string",
                "mapping": {
                  "type": "string",
                  "date_detection": false
                }
              }
            }
          ]
        }
      }
    

    【讨论】:

    • 没错,是的,但它没有回答我的问题,为什么当我之后 GET.._mapping 时没有出现任何规则。如何检查/验证应用了哪些规则?
    • neither rule shows up 是什么意思?字段的映射是您的规则是否被应用的证明。此外,在索引/类型的映射中(如果您的类型不存在,则自动为您创建的映射)您将看到在dynamic_templates 部分中定义的动态模板。
    • 好的,我听到了,但我看不出我的问题怎么会模棱两可? (除非我完全脱离了轨道......)我试图加强我的问题(见上文)。
    • 查看映射是否应用的唯一方法是检查新字段的映射。如果他们确实具有您模板中的预期配置,则映射成功。
    • 好的,所以它应该显示为“GET /myindex/testobject/_mapping”,但它没有,正如我的 sn-ps 显示的那样。我的 dynamic_mapping 的语法有问题吗?为什么不应用?
    【解决方案2】:

    我在我的假设中发现了错误:"date_detection": false 不适用于 dynamic_templates。

    您必须直接在映射上指定 date_detection(而不是在特定类型的级别)。 https://www.elastic.co/guide/en/elasticsearch/guide/current/custom-dynamic-mapping.html

    如果您希望它自动添加到新索引中,您可以使用索引模板。

    感谢 Yannick 的提示 (https://discuss.elastic.co/t/mappings--default--dynamic-templates-doesnt-show-up-in-resulting-mapping/59030)

    【讨论】:

      猜你喜欢
      • 2015-04-29
      • 2014-04-12
      • 2021-03-10
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-16
      • 2016-05-19
      相关资源
      最近更新 更多