1

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.