Data Types ignored when adding new document?


#1

Hi,

I'm using elasticsearch 5.1.1.

I created custom mapping and on one of my fields I have this (below). My goal being the "active" field should only allow a true/false value.

 "properties": {
      "active": {
      "type": "boolean"
     },
  }

If I then create a new document and populate the active field with a value say "r2452tgfw" (a non-Boolean value) or something, it allows the document to be created. Maybe that's just how it is or maybe I'm missing something, but is there a way to prevent a document from being indexed if one tries to put data in a field that is of a different data type? I'm expecting some sort of validation to happen I suppose.


(Nik Everett) #2

Have a look at coerce. I'm not 100% sure it effects booleans but it is a fairly important read anyway even if it turns out not to be what is up.


#3

Thanks for the reply @nik9000. After performing a test, it does not seem to affect booleans though, but it was a helpful read.


#4

I'm guessing this is why booleans are treated differently:

Boolean fields accept JSON true and false values, but can also accept strings and numbers which are interpreted as either true or false:

https://www.elastic.co/guide/en/elasticsearch/reference/2.1/boolean.html


(Nik Everett) #5

Yeah, I just dug through the code again. We have a lot of weird leniency around booleans. There is a proposal to make them properly strict:

I commented on it, even. But our "these six things mean false, everything else is true" way of doing it keeps slipping my mind.

So, at this point there is no way to be strict about booleans.


(system) #6

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.