elasticsearch - simple_query_string 或 query_string 哪个更好?
问题描述
simple_query_string
弹性搜索和弹性搜索有什么区别query_string
?
哪个更适合搜索?
在弹性搜索simple_query_string
文档中,它们是这样写的
与常规
query_string
查询不同,simple_query_string
查询永远不会抛出异常并丢弃查询的无效部分。
但不清楚。哪一个更好?
解决方案
没有简单的答案。这取决于 :)
通常,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
推荐给一些更高级的用户,他们将接受如何正确查询语法的培训,这样他们就会知道为什么在每种特定情况下都没有任何结果。
推荐阅读
- jquery - JQuery循环使用for循环通过多维json数组
- python - 检查当前单词是否接近字符串中的单词的有效方法是什么?
- javascript - 任务:react-native-webview:compileDebugJavaWithJavac FAILED
- android - Android 模拟器无法在 Android Studio 中运行
- javascript - 如何从 Trefle API 查询信息
- sql - SQL查询不同流服务中用户的重叠?
- html - 插入 FAVICON 的替代方法?
- windows - WEVTUtil 从源过滤
- reactjs - 反应路由器并将道具传递给其他组件
- python - 更改布尔值 tkinter 的按钮