Conditional Sort Using Two Fields

We have a document that has the following fields:


We want to include both fields in the sort. We do NOT want to sort field1, then field2. We want the sort to use both fields at the same sort level based on the value in another field. If I were to write this in SQL I would write it like this:

select *
-- create some dummy data
select 8 as value1, 1 as value2, 'val1' as valueType, 1 as sortValue union all
select 6 as value1, 3 as value2, 'val2' as valueType, 4 as sortValue union all
select 4 as value1, 5 as value2, 'val1' as valueType, 3 as sortValue union all
select 2 as value1, 7 as value2 , 'val2' as valueType, 2 as sortValue
) t
order by case valueType when 'val1' then value2 else value1 end -- use a case to handle the sort based on valueType field

Is there a way to do a similar case statement in the sort clause of an Elasticsearch query?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.