Reputation: 1062
I am trying to search for the word "OR" in document from index.
Here is the query I type
GET index_name/_search
{
"query": {
"bool": {
"filter": [
{
"query_string": {
"query": "OR word1",
"fields": [
"field1"
]
}
}
]
}
}
}
I get the following Error
{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "Failed to parse query [OR word1]",
"index_uuid": "Brju2MNeQ5m7sI02Q2Y4gw",
"index": "index_name"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "index_name",
"node": "C5Suw-gaR7eehoH0WNGw1A",
"reason": {
"type": "query_shard_exception",
"reason": "Failed to parse query [OR word1]",
"index_uuid": "Brju2MNeQ5m7sI02Q2Y4gw",
"index": "index_name",
"caused_by": {
"type": "parse_exception",
"reason": "Cannot parse 'OR word1': Encountered \" <OR> \"OR \"\" at line 1, column 0.\nWas expecting one of:\n <NOT> ...\n \"+\" ...\n \"-\" ...\n <BAREOPER> ...\n \"(\" ...\n \"*\" ...\n <QUOTED> ...\n <TERM> ...\n <PREFIXTERM> ...\n <WILDTERM> ...\n <REGEXPTERM> ...\n \"[\" ...\n \"{\" ...\n <NUMBER> ...\n <TERM> ...\n ",
"caused_by": {
"type": "parse_exception",
"reason": "Encountered \" <OR> \"OR \"\" at line 1, column 0.\nWas expecting one of:\n <NOT> ...\n \"+\" ...\n \"-\" ...\n <BAREOPER> ...\n \"(\" ...\n \"*\" ...\n <QUOTED> ...\n <TERM> ...\n <PREFIXTERM> ...\n <WILDTERM> ...\n <REGEXPTERM> ...\n \"[\" ...\n \"{\" ...\n <NUMBER> ...\n <TERM> ...\n "
}
}
}
}
]
},
"status": 400
}
Why is this an error and whats the correct way to overcome this? If I remove the word "Or" from the query it works fine however when i keep it it give me this error
Upvotes: 1
Views: 2008
Reputation: 16172
It is not recommended to use query_string, as mentioned in the ES official documentation:
Because it returns an error for any invalid syntax, we don’t recommend using the query_string query for search boxes.
If you don’t need to support a query syntax, consider using the match query. If you need the features of a query syntax, use the simple_query_string query, which is less strict.
Search Query:
{
"query": {
"match": {
"title": {
"query": "OR"
}
}
}
}
Search Result:
"hits": [
{
"_index": "stof_63952836",
"_type": "_doc",
"_id": "2",
"_score": 0.43445712,
"_source": {
"title": "OR word1"
}
}
]
Upvotes: 1
Reputation: 290
If i understood your issue well, I created an index, and ingested two documents. I tried to devise a suggestion using the escape
characters = \\
Here are the data I ingested:
"field1": "word1"
"field1": "OR word1"
Then, I did a small modification to your query:
{
"query": {
"bool": {
"filter": [
{
"query_string": {
"query": "word1 \\OR",
"fields": ["field1"]
}
}
]
}
}
}
The response was:
"hits" : [
{
"_index" : "or-doc",
"_type" : "_doc",
"_id" : "9mCioHQBowpsxTkF3Jdx",
"_score" : 0.0,
"_source" : {
"field1" : "word1"
}
},
{
"_index" : "or-doc",
"_type" : "_doc",
"_id" : "82CjoHQBowpsxTkFCZgE",
"_score" : 0.0,
"_source" : {
"field1" : "OR word1"
}
}
]
Note: I escaped the OR
as it is considered as a reserved words
.
I will be glad to help if you have any issue.
Links: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
Upvotes: 1