Dero
Dero

Reputation: 309

Elasticsearch: Searching on boolean field

I have a couple of documents in an Elasticsearch Cluster having a field isInFight holding a boolean value. Mapping is set correctly (already double-checked this) to boolean.

When I run a search with:

 curl -XGET localhost:9200/default/_search --data '{"query": {"term": {"isInFight": true}}}'

I get documents returned with the field being true and false. Changing the value in my search query to false no documents are returned at all.

To me this looks like the term query checked for the field being present or not rather then for it's value. Everything I found on the internet suggests that this query is the way to go though.

Am I missing something here? Is there a known bug regarding this?

The Elasticsearch in question is version 5.5.2 with lucene version 6.6.0 on an ubuntu 16.04 server.

Upvotes: 7

Views: 19954

Answers (2)

Shachaf.Gortler
Shachaf.Gortler

Reputation: 5745

This works for me:

{
  "query": {
    "bool": {
      "must": {
        "term": {
          "isInFight": true
        }
      }
    }
  }
}

Upvotes: 3

deerawan
deerawan

Reputation: 8443

Try this in my local elasticsearch and didn't find the issue

POST test_index/doc
{
  "id": 1,
  "isInFight": false
}

POST test_index/doc
{
  "id": 2,
  "isInFight": true
}

POST test_index/doc
{
  "id": 3,
  "isInFight": true
}

GET test_index/doc/_search
{
  "query": {
    "term": { "isInFight": false }
  }
}

and got this result

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "test_index",
        "_type": "doc",
        "_id": "AWU2Dxm7GR2l1TPuyKhN",
        "_score": 0.2876821,
        "_source": {
          "id": 1,
          "isInFight": false
        }
      }
    ]
  }
}

Upvotes: 7

Related Questions