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

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-normalize-a-boolean-field-upon-ingest-when-it-may-appear-as-a-string-or-a-boolean/227178>\
**Category:** Elasticsearch\
**Created:** [April 8, 2020, 4:36pm UTC](https://discuss.elastic.co/t/is-it-possible-to-normalize-a-boolean-field-upon-ingest-when-it-may-appear-as-a-string-or-a-boolean/227178 "2020-04-08T16:36:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomj](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@tomj](https://discuss.elastic.co/u/tomj)\
**Post date:** [April 8, 2020, 4:36pm UTC](https://discuss.elastic.co/t/is-it-possible-to-normalize-a-boolean-field-upon-ingest-when-it-may-appear-as-a-string-or-a-boolean/227178/1 "2020-04-08T16:36:59Z")

</div>

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
        }
      }
    ]
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 6, 2020, 4:37pm UTC](https://discuss.elastic.co/t/is-it-possible-to-normalize-a-boolean-field-upon-ingest-when-it-may-appear-as-a-string-or-a-boolean/227178/2 "2020-05-06T16:37:03Z")

</div>

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