弹性搜索中在Java中进行排序的条件

huangapple go评论67阅读模式
英文:

Elasticsearch condition on sorting in java

问题

以下是翻译好的部分:

对于单字段排序,我正在使用以下代码:

searchSourceBuilder.sort(SortBuilders.fieldSort("count").order(SortOrder.DESC));

但是现在我需要添加类似这样的条件:

searchSourceBuilder.sort(SortBuilders.fieldSort(count <= 0 ? "visitor" : "visitor" * "count").order(SortOrder.DESC));

我无法找到任何线索来完成这个操作。在Elasticsearch中是否有可能实现,还是我需要使用另一种方法?

英文:

For single field sorting I am using this code

searchSourceBuilder.sort(SortBuilders.fieldSort(&quot;count&quot;).order(SortOrder.DESC));

but now I need to add condition like this

searchSourceBuilder.sort(SortBuilders.fieldSort(count &lt;= 0 ? &quot;visitor&quot;:&quot;visitor&quot; * &quot;count&quot;).order(SortOrder.DESC));

I am not getting any clue to do this. Is it possible at elasticsearch or I need to do it another way?

答案1

得分: 2

你需要使用的是 SortBuilders.scriptSort()基于脚本的排序),像这样:

Script script = new Script("doc.count.value <= 0 ? doc.visitor.value : doc.visitor.value * doc.count.value");
SortBuilders.scriptSort(script, ScriptSortType.NUMBER).order(SortOrder.DESC);
英文:

What you need to use is SortBuilders.scriptSort() (script-based sorting), like this:

Script script = new Script( &quot;doc.count.value &lt;= 0 ? doc.visitor.value : doc.visitor.value * doc.count.value&quot;);
SortBuilders.scriptSort(script, ScriptSortType.NUMBER).order(SortOrder.DESC);

huangapple
  • 本文由 发表于 2020年10月13日 13:51:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64329312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定