我一直使用Elasticsearch 7.6和PHP客户端API进行所有操作。我已经创建了elasticsearch索引设置和映射,如下所示
$params = [
'index' => $index,
'body' => [
'settings' => [
"number_of_shards" => 1,
"number_of_replicas" => 0,
"index.queries.cache.enabled" => false,
"index.soft_deletes.enabled" => false,
"index.refresh_interval" => -1,
"index.requests.cache.enable" => false,
"index.max_result_window"=> 2000000
],
'mappings' => [
'_source' => [
"enabled" => false
],
'properties' => [
"text" => [
"type" => "text",
"index_options" => "docs"
]
]
]
]
];
我的布尔运算OR查询如下
$json = '{
"from" : 0, "size" : 2000000,
"query": {
"bool": {
"filter": {
"match" : {
"text" : {
"query" : "apple orange grape banana",
"operator" : "or"
}
}
}
}
}
}';
我对200万个文档进行了索引,所有文档都与查询相匹配,而且我也得到了预期的所有文档。因为我匹配了所有的文档,所以我在bool查询中使用了一个过滤器来避免打分。
但在我的日志文件中,我重复得到以下消息,直到查询执行完毕。有时,当我对文档进行批量索引时,也会得到同样的消息。
[2020-05-15T19:15:45,720][INFO ][o.e.m.j.JvmGcMonitorService] [node1] [gc][14] overhead, spent [393ms] collecting in the last [1.1s]
[2020-05-15T19:15:47,822][INFO ][o.e.m.j.JvmGcMonitorService] [node1] [gc][16] overhead, spent [399ms] collecting in the last [1s]
[2020-05-15T19:15:49,827][INFO ][o.e.m.j.JvmGcMonitorService] [node1] [gc][18] overhead, spent [308ms] collecting in the last [1s]
我的堆内存给了16GB。elasticsearch日志中没有其他警告。这可能是什么原因呢,还是在检索大量文档的时候会出现这种情况?我了解滚动API,但我很好奇为什么当我使用index.max_result_window的大值时,会出现这种情况。 非常感谢您的帮助?先谢谢你
你所看到的是Elasticsearch的正常行为,特别是上述配置,以及任何Java应用的一般行为。
index.max_result_window
?是的。正如文件上所说的 index.max_result_window
状态,产生的垃圾量与查询返回的文档数量成正比。
搜索请求占用的堆内存和时间与from +大小成正比,这就限制了内存。
是的,如果你的批量请求规模很大,可能会触发垃圾收集。
当然,ES会在堆上分配需要发回给用户的文档,紧接着这些文档就会变成垃圾,从而成为垃圾收集的对象。
你可以找到一些相关的信息,例如 此处.
有的,比如说。匹配所有 查询。
与让所有文档都匹配某个查询相比,如何更好?Elasticsearch不需要查询索引,可以马上去获取文档(性能和资源使用更好)。
Scroll API是值得推荐的方式,因为它的扩展能力远远超过了一个Elasticsearch节点的内存容量(一个人可以从几个机器组成的集群中下载1TB的数据,内存大约为16GB)。
然而,如果你想仍然使用普通的搜索查询,你可以考虑使用 from
和 size
参数,并做分页处理(这样可以限制每次查询获取的文档量,使GC更好的分布在时间上)。
希望对大家有所帮助!