Home Articles

如果文档的字段映射不匹配,Elasticsearch将无提示失败

Asked
Viewed 198 times
10

我面临一个与Elasticsearch有关的奇怪问题 . 我的映射指定某个字段的类型为 long . 现在不小心我试图索引一些文件,这些文件的字段为 string 而不是 long . 我没有收到Elasticsearch的错误,但文档从未编入索引 . 当我修复问题时,文档被编入索引就好了 .

例:

我的映射:

{
    "field1": {
        "type": "long"
    }
}

当我发送此文档时,它会无声地失败:

{
     "field1": "this is a string"
 }

当我发送它时,它按预期工作:

{
     "field1": 12345
 }

有没有办法检测这种错误?

2 Answers

  • 5

    如果将ignore_malformed string设置为false . 如果文档格式错误但是抛出异常,它不会索引文档 . 至少在elasticsearch 1.6.0中 .

    例:

    put test
    
    put test/test/_mapping 
    {
        "properties" : {
            "title" : {"type" : "string"},
            "data" : {"type": "long" ,"ignore_malformed":false}
    
        }
    }
    
    put test/test/1
    {
        "data" : "1",
        "title" : "valid coerce string to number"
    }
    
    put test/test/2
    {
        "data" : "hello",
        "title" : "invalid number"
    }
    
    #Failed Response
    {
       "error": "MapperParsingException[failed to parse [data]]; nested: NumberFormatException[For input string: \"hello\"]; ",
       "status": 400
    }
    
    Query with Get fails
    
    get test/test/2
    
    
    {
       "_index": "test",
       "_type": "test",
       "_id": "2",
       "found": false
    }
    
  • 4

    我怀疑你的映射看起来像这样:

    {
        "long_field": {
            "type": "long"
        }
    }
    

    如果是这种情况,您可以将 coerce 标志设置为 false ,因为它是true by default并始终尝试将字符串转换为数字并截断整数的分数 .

    {
        "long_field": {
            "type": "long",
            "coerce": false
        }
    }
    

    如果你这样做,下次你尝试将长字段索引为字符串时,ES会告诉你:

    MapperParsingException[failed to parse [long_field]]; nested: IllegalArgumentException[Long value passed as String];
    

Related