simple_query_string 和 query_string 哪个更好?

Sur*_*lvi 8 elasticsearch

弹性搜索simple_query_string和之间有什么区别?query_string

哪个更适合搜索?

在elasticsearch的simple_query_string文档中,是这么写的

与常规query_string查询不同,该simple_query_string查询永远不会抛出异常并丢弃查询的无效部分。

但还不清楚。哪一个更好?

Pio*_*ski 7

没有简单的答案。这取决于 :)

一般来说,它query_string专用于更高级的用途。它有更多选项,但正如您引用的那样,当发送的查询无法作为一个整体进行解析时,它会抛出异常。相反,simple_query_string选项较少,但不会在无效部分引发异常。

作为示例,请看以下两个查询:

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

GET _search
{
  "query": {
    "simple_query_string": {
      "query": "hyperspace + crops",
      "fields": [
        "description"
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

两者是等效的,并且从索引中返回相同的结果。但是当你打破查询并发送时:

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

GET _search
{
  "query": {
    "simple_query_string": {
      "query": "hyperspace + crops +",
      "fields": [
        "description"
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后您将仅获得第二个结果 ( simple_query_string)。第一个 ( query_string) 会抛出如下内容:

{
  "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
}
Run Code Online (Sandbox Code Playgroud)

希望您现在了解抛出/不抛出异常的区别。

哪个更好?如果您想向一些普通的最终用户公开搜索,我宁愿建议使用simple_query_string. 感谢最终用户即使在查询中犯了错误,也会在每个查询案例中得到一些结果。query_string建议一些更高级的用户使用,他们将接受如何正确查询语法的培训,这样他们就会知道为什么在每种特定情况下都没有任何结果。