简体   繁体   English

在弹性搜索中按COALESCE排序

[英]Sort with COALESCE in elastic search

I have issue on sorting null column with ElasticSearch. 我在使用ElasticSearch对null列进行排序时遇到问题。 Previously I'm using sql query COALESCE to get non-null value from column. 以前,我使用sql查询COALESCE从列获取非空值。 How can I sort non-null value from columns? 如何对列中的非空值排序?

SQL Query SQL查询

ORDER BY COALESCE(
  NULLIF(table1.colA,''),
  NULLIF(table1.colB,''),
  NULLIF(table1.colC,'')
) DESC

The above statement will check if colA has value, then pick colA. 上面的语句将检查colA是否有价值,然后选择colA。 If colA is null, it will check colB. 如果colA为null,它将检查colB。

How do I sort with COALESCE similar function in Elasticsearch? 如何在Elasticsearch中使用COALESCE类似功能进行排序?

You can use a script based scoring with this feature you have full control over how the _score field is calculated. 您可以将此功能用于基于脚本的评分 ,从而完全控制_score字段的计算方式。

In your case you could write a small painless script that returns the first non empty field in the document. 在您的情况下,您可以编写一个小的脚本来返回文档中的第一个非空字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM