ClickHouse - 在嵌套字段中搜索

Posted

技术标签:

【中文标题】ClickHouse - 在嵌套字段中搜索【英文标题】:ClickHouse- Search within nested fields 【发布时间】:2019-05-22 10:28:05 【问题描述】:

我有一个名为 items.productName 的嵌套字段,我想在其中检查产品名称是否包含特定字符串。

SELECT * FROM test WHERE hasAny(items.productName,['Samsung'])

这仅适用于产品名称为三星的情况。

我试过数组加入

SELECT 
    *
FROM test
ARRAY JOIN items
WHERE items.productName LIKE '%Samsung%' 

这可行,但速度很慢(500 万条记录约 1 秒)

有没有办法像在 hasAny 中一样执行?

【问题讨论】:

【参考方案1】:

您可以使用 arrayFilter 函数来实现这一点。 ClickHouse docs

查询

Select * from test where arrayFilter(x -> x LIKE '%Samsung%', items.productName) != []

如果你不使用 != [] 那么你会得到一个错误“DB::Exception: Illegal type Array(String) of column for filter. Must be UInt8 or Nullable(UInt8) or Const variables of them.”

【讨论】:

更好的方法是使用arrayExists函数:WHERE arrayExists(x -> x LIKE '%Samsung%', items.productName)。这消除了与空数组进行比较的需要,并允许 ClickHouse 在找到第一个匹配的数组值时通过短路来优化子句,而不需要迭代整个数组。

以上是关于ClickHouse - 在嵌套字段中搜索的主要内容,如果未能解决你的问题,请参考以下文章