Elasticsearch 过滤器与许多 ID 的术语查询
Elasticsearch filter vs term query for many ids
我有一个与某些 product_id 相关的文档索引。我想找到特定 id 的所有文档(大约 100 000 product_id 被发现,索引中总共有 1 亿个)。
在那种情况下,过滤器查询会是最快最好的选择吗?
"query": {
"bool": {
"filter": {"terms": {"product_id": product_ids}
}
}
还是将 id 分块并仅使用术语查询或其他更好?
这个问题可能有点重复,但我将非常感谢您提供最佳实践建议(以及一些推理)。
对于非常大的结果集,您可以使用弹性搜索查询的“paging
”或“scrolling
”功能。
使用“from - to
”查询:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html
或“scroll
”查询:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
我认为 "From / To" 是一种更有效的方法,除非你想每次 return 数千个结果(这可能是很多很多 MB 的数据,所以你可能不想要那)
编辑:
您可以像这样批量查询:
获取my_index/_search
{
"query":{
"terms":{
"_id": [ "1", "2", "3", .... "10000" ] // 调整最佳数组长度
}
}
}
如果您的文档 ID 是连续的或您可以轻松排序的其他数字形式,并且有可用的字段,您可以执行“range query
”
获取_search
{
"query":{
"range":{
"document_id_that_is_a_number":{
"gte" : 0, // 在每个查询上按 "lte" 步长因子增加
"lte" : 10000 // 在这里找到一个合适的数字
}
}
}
}
经过一些测试和更多阅读后,我找到了答案:
过滤器查询的工作速度比仅包含术语查询的块要快得多。
但是制作非常大的过滤器会减慢获得结果的速度。
在我的例子中,使用包含 10,000 个 ID 的块的过滤器查询比一次使用包含所有 100,000 个 ID 的过滤器查询快 10 倍(顺便说一句,这个数字在 Elasticsearch 6 中已经受到限制)。
同样来自elasticsearch官方documentation:
Potentially the amount of ids specified in the terms filter can be a lot. In this scenario it makes sense to use the terms filter’s terms lookup mechanism.
唯一需要考虑的缺点是过滤器查询存储在缓存中。 (缓存实现 LRU 逐出策略:当缓存变满时,最近最少使用的数据将被逐出,为新数据让路。)
P.S。在所有情况下,我总是使用滚动。
我有一个与某些 product_id 相关的文档索引。我想找到特定 id 的所有文档(大约 100 000 product_id 被发现,索引中总共有 1 亿个)。
在那种情况下,过滤器查询会是最快最好的选择吗?
"query": {
"bool": {
"filter": {"terms": {"product_id": product_ids}
}
}
还是将 id 分块并仅使用术语查询或其他更好?
这个问题可能有点重复,但我将非常感谢您提供最佳实践建议(以及一些推理)。
对于非常大的结果集,您可以使用弹性搜索查询的“paging
”或“scrolling
”功能。
使用“from - to
”查询:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html
或“scroll
”查询:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
我认为 "From / To" 是一种更有效的方法,除非你想每次 return 数千个结果(这可能是很多很多 MB 的数据,所以你可能不想要那)
编辑:
您可以像这样批量查询:
获取my_index/_search { "query":{ "terms":{ "_id": [ "1", "2", "3", .... "10000" ] // 调整最佳数组长度 } } }
如果您的文档 ID 是连续的或您可以轻松排序的其他数字形式,并且有可用的字段,您可以执行“
range query
”获取_search { "query":{ "range":{ "document_id_that_is_a_number":{ "gte" : 0, // 在每个查询上按 "lte" 步长因子增加 "lte" : 10000 // 在这里找到一个合适的数字 } } } }
经过一些测试和更多阅读后,我找到了答案:
过滤器查询的工作速度比仅包含术语查询的块要快得多。 但是制作非常大的过滤器会减慢获得结果的速度。 在我的例子中,使用包含 10,000 个 ID 的块的过滤器查询比一次使用包含所有 100,000 个 ID 的过滤器查询快 10 倍(顺便说一句,这个数字在 Elasticsearch 6 中已经受到限制)。
同样来自elasticsearch官方documentation:
Potentially the amount of ids specified in the terms filter can be a lot. In this scenario it makes sense to use the terms filter’s terms lookup mechanism.
唯一需要考虑的缺点是过滤器查询存储在缓存中。 (缓存实现 LRU 逐出策略:当缓存变满时,最近最少使用的数据将被逐出,为新数据让路。)
P.S。在所有情况下,我总是使用滚动。