Elasticsearch fails silently if document has mapping mismatch for a field

后端 未结 2 1835
一生所求
一生所求 2021-02-06 01:59

I am facing a weird issue with Elasticsearch. My mapping specifies that a certain field is of type long. Now accidentally I was trying to index some documents which

相关标签:
2条回答
  • 2021-02-06 02:21

    If you set ignore_malformed string to false. It would not index the document if it is malformed but throws an exception. At least in elasticsearch 1.6.0.

    Example:

    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
    }
    
    0 讨论(0)
  • 2021-02-06 02:38

    I suspect your mapping looks similar to this:

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

    If that's the case, you can set the coerce flag to false as it is true by default and will always try to convert strings to numbers and truncate fractions for integers.

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

    If you do this, the next time you try to index a long field as a string, ES will tell you this:

    MapperParsingException[failed to parse [long_field]]; nested: IllegalArgumentException[Long value passed as String]; 
    
    0 讨论(0)
提交回复
热议问题