0

this is part of my elasticsearch query:

 {
    "match": {
        "value": {
            "query": "1",
        }
    }
}

value is a boolean field in my index, does elasticsearch accept 1 as true and 0 as false in search on the boolean field?

saba safavi
  • 160
  • 7
  • 20

2 Answers2

1

This is easy to test...

First create the index:

PUT test 
{
  "mappings": {
    "properties": {
      "bool_field": {
        "type": "boolean"
      }
    }
  }
}

Then index a document:

PUT test/_doc/1
{
  "bool_field": true
}

Try to query using 0/1 instead of a boolean

POST test/_search
{
  "query": {
    "term": {
      "bool_field": "1"
    }
  }
}

Response: Can't parse boolean value [1], expected [true] or [false]

{
  "error" : {
    "root_cause" : [
      {
        "type" : "query_shard_exception",
        "reason" : "failed to create query: Can't parse boolean value [1], expected [true] or [false]",
        "index_uuid" : "bZpN3j1kT9KtMBnGkpOmKQ",
        "index" : "test"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "test",
        "node" : "CyVrqrOtR0CP3RfZtdBTag",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "failed to create query: Can't parse boolean value [1], expected [true] or [false]",
          "index_uuid" : "bZpN3j1kT9KtMBnGkpOmKQ",
          "index" : "test",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "Can't parse boolean value [1], expected [true] or [false]"
          }
        }
      }
    ]
  },
  "status" : 400
}

PS: It used to be possible up to ES 5. As of ES 6, only true/false are accepted values for boolean fields.

Val
  • 207,596
  • 13
  • 358
  • 360
1

as specified in the doc ES accept true/"true"/false/"false" as boolean values.

Other values like 0/1 will throw error in recent versions

error: failed to create query: Can't parse boolean value 1, expected [true] or [false]

NB: you should use a term query to filter in boolean field

Pierre Mallet
  • 7,053
  • 2
  • 19
  • 30