【问题标题】:Dynamic Mapping Templates seem to prevent mappings from being created动态映射模板似乎阻止创建映射
【发布时间】:2015-11-24 23:40:13
【问题描述】:

我正在尝试使用动态用户创建的字段来索引一些数据。输入数据如下所示:

{ "kind": "feature",
  "creationDate": 1424561126000,
  "key" : "sdfgsdfg",
  "user": {
    "email" : "me@example.com",
    "custom": {
      "hello": "world",
      "answer": 42,
      "enable_the_thing": true
    }
  }
}

user.custom 对象中的键和值是动态的,甚至有可能某些实例对于同一个键具有不同类型的值。所以,我将它们翻译成这样的形状:

    "custom": {
      "hello": {
        "stringVal": "world"
      },
      "answer": {
        "numVal": 42
      },
      "enable_the_thing": {
        "boolVal": true
      }
    }

(这样,我可以确定user.custom.hello.stringVal 将始终是一个字符串,即使另一个文档有一个数字user.custom.hello.numVal。)

到目前为止,这似乎有效。我将user.custom 的映射声明为:

{
  "type": "object",
  "dynamic": true
}

当我得到映射时,我看到这些动态字段是按预期创建的。

但是,我还需要保留所有这些值的未分析字符串表示形式。所以,我添加了一个像这样的动态映射模板(还有一个用于numVal 和boolVal 字段):

"custom_attribute_string": {
    "mapping": {
        "fields": {
            "stringVal": {
                "index": "analyzed", 
                "type": "string"
            }, 
            "untouched": {
                "index": "not_analyzed", 
                "type": "string"
            }
        }, 
        "type": "multi_field"
    }, 
    "path_match": "user.custom.*.stringVal"
}

但是,现在动态自定义字段没有在映射中创建,文档也没有被索引。

我怀疑我的映射模板有问题,但我不确定在哪里查看。我根据这些docs 创建了这个模板,但也许我应该寻找其他地方?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您的动态映射完美运行,untouched 字段已创建,但在查看文档源时它不会显示在您的结果中。 _source 只会包含您发送用于索引的任何数据。 Elasticsearch 会根据你指定的映射索引每个字段,但不会修改_source

    您可以通过对其运行聚合来看到untouched 子字段确实存在:

    {
      "size": 0,
      "aggs": {
        "untouched_values": {
          "terms": {
            "field": "user.custom.hello.stringVal.untouched"
          }
        }
      }
    }
    

    附带说明,multi-field 类型已在不久前被弃用,因此即使它仍然有效,您也应该改用指定多字段的新方法,如下所示:

    "custom_attribute_string": {
        "mapping": {
            "type": "string",
            "fields": {
                "untouched": {
                    "index": "not_analyzed", 
                    "type": "string"
                }
            }
        }, 
        "path_match": "user.custom.*.stringVal"
    }
    

    请注意,为了重现上述问题,我创建了以下索引:

    curl -XPUT localhost:9200/testindex -d '{
      "mappings": {
        "test": {
          "dynamic_templates": [
            {
              "custom_attribute_string": {
                "mapping": {
                  "fields": {
                    "stringVal": {
                      "index": "analyzed",
                      "type": "string"
                    },
                    "untouched": {
                      "index": "not_analyzed",
                      "type": "string"
                    }
                  },
                  "type": "multi_field"
                },
                "path_match": "user.custom.*.stringVal"
              }
            }
          ],
          "properties": {
            "user": {
              "properties": {
                "email": {
                  "type": "string"
                },
                "custom": {
                  "type": "object",
                  "dynamic": true
                }
              }
            }
          }
        }
      }
    }'
    

    然后我索引了以下测试文档:

    curl -XPUT localhost:9200/testindex/test/1 -d '{
      "user": {
        "email": "val@me.com",
        "custom": {
          "hello": {
            "stringVal": "world"
          },
          "answer": {
            "numVal": 42
          },
          "enable_the_thing": {
            "boolVal": true
          }
        }
      }
    }'
    

    运行此程序后,我检索了映射,您可以看到 hello.stringVal.untouched 子字段存在:

    {
      "customs" : {
        "mappings" : {
          "custom" : {
            "dynamic_templates" : [ {
              "custom_attribute_string" : {
                "mapping" : {
                  "type" : "multi_field",
                  "fields" : {
                    "untouched" : {
                      "index" : "not_analyzed",
                      "type" : "string"
                    },
                    "stringVal" : {
                      "index" : "analyzed",
                      "type" : "string"
                    }
                  }
                },
                "path_match" : "user.custom.*.stringVal"
              }
            } ],
            "properties" : {
              "user" : {
                "properties" : {
                  "custom" : {
                    "dynamic" : "true",
                    "properties" : {
                      "answer" : {
                        "dynamic" : "true",
                        "properties" : {
                          "numVal" : {
                            "type" : "long"
                          }
                        }
                      },
                      "enable_the_thing" : {
                        "dynamic" : "true",
                        "properties" : {
                          "boolVal" : {
                            "type" : "boolean"
                          }
                        }
                      },
                      "hello" : {
                        "dynamic" : "true",
                        "properties" : {
                          "stringVal" : {
                            "type" : "string",
                            "fields" : {
                              "untouched" : {
                                "type" : "string",
                                "index" : "not_analyzed"
                              }
                            }
                          }
                        }
                      }
                    }
                  },
                  "email" : {
                    "type" : "string"
                  }
                }
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 有趣。当我在插入一些文档后尝试获取映射时,我没有看到为动态字段创建的映射。更重要的是,我也根本没有看到索引中的文档,所以我猜测映射和文档中存在不兼容的内容。我最终将我的三个动态模板换成一个具有三个字段(stringVal、boolVal 和numVal)的对象的单个模板,并在user.custom.* 上使用path_match。
    • 通常,即使映射不匹配,ES 也会始终索引某些内容。您如何索引您的文档以及如何检索它们?
    • 我使用 elastigo 库对它们进行了索引,并且我正在(尝试)在浏览器选项卡上使用 head 插件检索它们(除了我试图插入的测试文档之外,此索引为空) .
    • 另外,我在插入时没有收到任何错误,但我怀疑 elastigo 有时会吞下不应该出现的错误。但是,我在插入文档时遇到了错误,该文档的字段类型与映射(即布尔字段中的数值)不兼容。这就是导致我做整个stringVal/boolVal/etc 的事情的原因。
    • 我的建议是,在制作映射时,请始终先使用 curl 或 Sense 徒手操作,当一切正常时,您可以通过库为您的数据建立索引。最小意外原则!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    相关资源
    最近更新 更多