elasticsearch - 使用 multi_match 查询时如何克服 maxClauseCount 错误
问题描述
我的Elasticsearch服务器上有 10 多个索引。
每个索引都有 1 个或多个具有不同类型分析器的字段:关键字、标准、ngram等...
对于我使用的全局搜索,multi_match
没有指定任何明确fields
的 .
对于查询我正在使用elasticsearch-dsl
库,代码如下:
def search_for_index(indice, term, num_of_result=10):
s = Search(index=indice).sort({"_score": "desc"})
s = s[:num_of_result]
s = s.query('multi_match', query=term, operator='and')
response = s.execute()
return response.to_dict()['hits']['hits']
我得到了很好的结果,搜索工作得很好,但有时有人输入了更长的文本,我得到了maxClauseCount
错误。
例如,当搜索词 term
等于时引发错误的搜索:
term=We are working on your request and will keep you posted at the earliest.
或者任何其他稍长的文本都会引发相同的错误。
你能帮我找出一些更好的方法来进行全局搜索,这样我就可以避免这种错误吗?
解决方案
首先,这种限制是有原因的。您拥有的布尔子句越多 - 搜索就越繁重。将其视为每个子句的文档 ID 的交叉 (AND) 或连接 (OR) 子集。这是一个非常繁重的操作,这就是为什么最初它有 1024 个子句的限制。
一般建议是尝试减少您正在搜索的字段数量。也许您的字段不包含文本数据或只有一些内部 ID。您可以在 multi_match 查询期间通过明确指定字段部分将它们删除。
如果您仍然决定采用当前方法并且您使用的是Elasticsearch 5.5+及更高版本,则可以通过在elasticsearch.yml中添加以下行并重新启动您的实例来更改这些方法。
indices.query.bool.max_clause_count: 250000
如果您使用的是Elasticsearch 5 之前的版本,则该设置称为index.query.bool.max_clause_count
推荐阅读
- android - DocumentContract 上的 ContentResolver 查询 列出所有文件而不考虑选择
- python - Newton-raphson 脚本冻结以尝试获取大量数字
- javascript - Next js 和 Apollo - Cookie 未通过
- python-3.x - 运行 Popen 后文件未读取文本文件
- android - 如何在 Android 中更改元素绑定的数据源
- sql - 如何将字符串与 listagg 连接导致红移?
- c# - 使用 C# 解析格式错误的 XML-SOAP
- reactjs - 使用 createStyles() 时如何创建确定性样式
- mysql - 在 Docker 中运行的 Laravel 应用程序无法从位于 VirtualBox 虚拟机上的 MySQL 数据库中获取数据(SQLSTATE[HY000] [2002] 连接被拒绝)
- node.js - npm 安装失败并出现错误:EPERM:不允许操作,取消链接“\node_modules”