10

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?

2 Answers 2

7

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.

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

Comments

2

Adding to what @Piotr has mentioned,

What I understand is when you want the external users or consumers want to make use of the search solution, simple query string offers better solution in terms of error handling and limiting what kind of queries users can probably construct.

In other words, if the search solution is available publicly for any consumers to consume the solution, then I guess simple_query_string would make sense, however if I do know who my end-users are in a way I can drive them as what they are looking for, no reason why I cannot expose them via query_string

Also QueryStringQueryBuilder.java makes use of QueryStringQueryParser.java while SimpleQueryStringBuilder.java makes use of SimpleQueryStringQueryParser.java which makes me think that there would be certain limitations in parsing and definitely the creators wouldn't want many features to be managed by end-users. for .e.g dis-max and which is available in query_string.

Perhaps the main purpose of simple query string is to limit end-users to make use of simple querying for their purpose and devoid them of all forms of complex querying and advance features so that we have more control on our search engine (which I'm not really sure about but just a thought).

Plus the possibilities to misuse query_string might be more as only advanced users are capable of constructing certain complex queries in correct way which may be a bit too much for simple users who are looking for basic search solution.

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.