如何在使用 group by 子句时跳过 Orientdb 中的空记录

How to skip null records in Orientdb while using group by clause

当我执行以下查询时:

SELECT count(*) as count, $new_source as name 
from news 
LET $new_source = if(eval("source.indexof('Other') === 0"), "Other", source)
where country_id = "111111"
group by $new_source

它抛出一个错误说:

com.orientechnologies.orient.core.exception.OCommandExecutionException: expression item 'source' cannot be resolved because current record is NULL

如果给定 country_id 在 "news" class 中至少有一条记录,则它工作正常,但如果给定 country_id 没有记录,则它抛出这个错误。

因为我对所有新闻记录都使用通用查询,而不考虑 country_id,如果没有针对特定国家/地区的记录,我希望空记录集应该是 return。

我也尝试过使用 orientdb 的 ifnull 函数来跳过空值,像这样:

SELECT count(*) as count, $new_source as name from news LET $new_source = ifnull(source, 0, if(eval("source.indexof('Other') === 0"), "Other", source)) where country_id = "111111" group by $new_source

但它不起作用,并抛出相同的错误。

我正在使用 OrientDb 2.1.8。我不想使用 javascript 函数并从控制台调用它(按照建议 here

有什么办法可以在使用 if 和 group by 时跳过空值吗?

这应该可以解决您的问题:

删除您的索引并使用引擎 Lucene 重新创建另一个全文类型的索引。

查询你得到这个结果:

我尝试修改您的查询,也许我找到了解决方案:

select count(*) as count,name
from (
  select if(eval("source.indexof('Other') === 0"), "Other", source) as name 
  from news where country_id = "111111")  
group by name

我没有错误,也没有正确的结果。

select count(*) as count,name
from (
  select if(eval("source.indexof('Other') === 0"), "Other", source) as name 
  from news where country_id = "1110")  
group by name

这里正确我有结果

希望对您有所帮助。