Home > Back-end >  Search string starting with OR keyword in elastic search
Search string starting with OR keyword in elastic search

Time:11-02

I want to make an elastic search query for invoice numbers which may start with OR keyword. Elastic search doesnt return any result for invoices starting with "OR" whereas it returns result for invoices starting with for instance "OP".

{
  "query": {
    "query_string": {
      "query": "OR AND 123123",
      "default_field": "invoice_number"
    }
  }
}

I tried these but they didnt work:

{
  "query": {
    "query_string": {
      "query": "(OR) AND (123123)",
      "default_field": "invoice_number"
    }
  }
}

{
  "query": {
    "query_string": {
      "query": "OR 123123",
      "operator" : "AND",
      "default_field": "invoice_number"
    }
  }
}

CodePudding user response:

Tldr;

Not sure it is documented anywhere, but I managed to make it work by wrapping the reserved token in single quote '

Example

POST /_bulk
{"index": {"_index": "74273735"}}
{"data": "This text contains an OR in caps"}
{"index": {"_index": "74273735"}}
{"data": "This text contains an AND in caps"}

GET 74273735/_search
{
  "query": {
    "query_string": {
      "default_field": "data",
      "query": "'OR'"
    }
  }
}
  • Related