首页 > 解决方案 > simple_query_string 或 query_string 哪个更好?

问题描述

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

哪个更适合搜索?

在弹性搜索simple_query_string文档中,它们是这样写的

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

但不清楚。哪一个更好?

标签: elasticsearch

解决方案


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

通常,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"
      ]
    }
  }
}

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

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

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

然后您将仅从第二个 ( 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
}

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

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


推荐阅读