Is it possible to "normalize" a boolean field upon ingest when it may appear as a string or a boolean?

I tested this behavior on ES 6.7 and 7.6. The example below is using 6.7.

I would like to be able to convert the string literals "true" and "false" to the associated boolean values. My reasoning is that I have tools querying ES which I would rather not have to worry about unmarshalling the results individually and compensating for finding strings in the documents.

Create an index with the desired mapping:

PUT booltest1
{
  "mappings": {
    "_doc": {
      "dynamic": "true",
      "properties": {
        "foo": {
          "type": "boolean"
        }
      }
    }
  }
}

Verify the mapping:

GET booltest1/_mapping

Response:

{
  "booltest1" : {
    "mappings" : {
      "_doc" : {
        "dynamic" : "true",
        "properties" : {
          "foo" : {
            "type" : "boolean"
          }
        }
      }
    }
  }
}

Index a couple of documents, one using a string for the field value, one using a boolean:

PUT booltest1/_doc/1
{
  "foo" : true
}

POST booltest1/_doc/2
{
  "foo" : "true"
}

Query for the documents:

GET booltest1/_search
{
 "query": {
    "match_all": {}
  }
}

Response:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "booltest1",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "foo" : "true"
        }
      },
      {
        "_index" : "booltest1",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "foo" : true
        }
      }
    ]
  }
}

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