Elastisearch: Can only use prefix queries on keyword and text fields - not on [isXyz] which is of type [boolean]

Viewed 3385

I am getting the following error on search. I just added a new field to index and it is of boolean type:

{
     "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "........",
        "node": ".......",
        "reason": {
          "type": "query_shard_exception",
          "reason": "Can only use prefix queries on keyword and text fields - not on [isXYZ] which is of type [boolean]",
          "index_uuid": "...........",
          "index": "......."
        }
      }
    ]
}

This is the query which causes the error:

{
    "query": {
      "bool": {
        "must": {
          "simple_query_string": {
            "query": "xyz*",
            "default_operator": "or",
            "fields": [
              "isXYZ",
              "anotherField"
            ]
          }
        }
      }
    }
  

But it works fine for following query:

{
    "query": {
      "bool": {
        "must": {
          "bool": {
            "must": [
              {
                "simple_query_string": {
                  "query": "true",
                  "default_operator": "or",
                  "fields": ["isXYZ"]
                }
              }
            ]
          }
        }
      }
    }
}

I am not sure what is wrong. I am new to Elastic Search

1 Answers

You can use lenient top-level parameter for simple query string here. Adding lenient just ignore exception that occurs due to format failures

For lenient parameter: If true, format-based errors, such as providing a text value for a numeric field, are ignored. Defaults to false.

To know more refer to this official ES documentation

Adding working example with index sample data, mapping, and search query.

Index Mapping

    {
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "pass": {
        "type": "boolean"
      }
    }
  }
}

Index data:

{
    "title": "hello world",
    "pass": true    
}

Search query:

{
  "query": {
    "bool": {
      "must": {
        "simple_query_string": {
          "query": "hello*",
          "default_operator": "or",
          "lenient": true,            <-- note this 
          "fields": [
            "title",
            "pass"
          ]
        }
      }
    }
  }
}

Search result:

"hits": [
  {
    "_index": "my-index",
    "_type": "_doc",
    "_id": "1",
    "_score": 1.0,
    "_source": {
      "title": "hello world",
      "pass": true
    }
  }
]
Related