Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better simple_query_string or query_string?

What is the difference between simple_query_string and query_string in elastic search?

Which is better for searching?

In the elastic search simple_query_string documentation, they are written

Unlike the regular query_string query, the simple_query_string query will never throw an exception and discards invalid parts of the query.

but it not clear. Which one is better?

like image 212
Suraj Dalvi Avatar asked Apr 28 '26 01:04

Suraj Dalvi


1 Answers

There is no simple answer. It depends :)

In general the query_string is dedicated for more advanced uses. It has more options but as you quoted it throws exception when sent query cannot be parsed as a whole. In contrary simple_query_string has less options but does not throw exception on invalid parts.

As an example take a look at two below queries:

GET _search
{
  "query": {
    "query_string": {
      "query": "hyperspace AND crops",
      "fields": [
        "description"
      ]
    }
  }
}

GET _search
{
  "query": {
    "simple_query_string": {
      "query": "hyperspace + crops",
      "fields": [
        "description"
      ]
    }
  }
}

Both are equivalent and return the same results from your index. But when you will break the query and sent:

GET _search
{
  "query": {
    "query_string": {
      "query": "hyperspace AND crops AND",
      "fields": [
        "description"
      ]
    }
  }
}

GET _search
{
  "query": {
    "simple_query_string": {
      "query": "hyperspace + crops +",
      "fields": [
        "description"
      ]
    }
  }
}

Then you will get results only from the second one (simple_query_string). The first one (query_string) will throw something like this:

{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "Failed to parse query [hyperspace AND crops AND]",
        "index_uuid": "FWz0DXnmQhyW5SPU3yj2Tg",
        "index": "your_index_name"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      ...
    ]
  },
  "status": 400
}

Hope you are now understand the difference with throwing/not throwing exception.

Which is better? If you want expose the search to some plain end users I would rather recommend to use simple_query_string. Thanks to that end user will get some result in each query case even if he made a mistake in a query. query_string is recommended for some more advanced users who will be trained in how is the correct query syntax so they will know why they do not have any results in every particular situation.

like image 170
Piotr Pradzynski Avatar answered Apr 30 '26 18:04

Piotr Pradzynski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!