首页 > 解决方案 > 使用 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.

或者任何其他稍长的文本都会引发相同的错误。

你能帮我找出一些更好的方法来进行全局搜索,这样我就可以避免这种错误吗?

标签: elasticsearchelasticsearch-dsl

解决方案


首先,这种限制是有原因的。您拥有的布尔子句越多 - 搜索就越繁重。将其视为每个子句的文档 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


推荐阅读