Sort on different fields depending on type

I would like to search in an index over multiple types and then sort based
on different (date) fields depending on type. Example:

index/type1/dateField1
index/type2/dateField2

Is this possible to get a single result list sorted on dateField1 or
dateField2? A workaround would be introducing a new field
"dateFieldCommon" for type1 and type2 but is there a better way?

--

I was able to get single sorted result list using script based sorting.
Don't know if it's a good solution though:

curl -XGET 'http://localhost:9200/index/_search' -d '{
"size" : 10,
"query" : {
"term" : { "content" : "foo" }
},
"sort" : {
"_script" : {
"script" : "if(doc["dateField1"].empty) {
doc. dateField2.value } else {doc. dateField1.value}",
"type" : "number",
"order" : "desc"
}
}
}
'

On Tuesday, October 9, 2012 10:43:52 AM UTC+2, Per wrote:

I would like to search in an index over multiple types and then sort based
on different (date) fields depending on type. Example:

index/type1/dateField1
index/type2/dateField2

Is this possible to get a single result list sorted on dateField1 or
dateField2? A workaround would be introducing a new field
"dateFieldCommon" for type1 and type2 but is there a better way?

--

Given the way you structured your script you could also try sorting on
dateField1, dateField2 if you specify that missing fields go last:

something like this (and I am not sure if you'll also need the
ignore_unmapped) :

"sort" : [
{ "dateField1" : {"missing" : "_last"} },
{ "dateField2" : {"missing" : "_last"} },
],

-- Raffaele

On Tue, Oct 9, 2012 at 6:27 AM, Per dev2null@gmail.com wrote:

I was able to get single sorted result list using script based sorting.
Don't know if it's a good solution though:

curl -XGET 'http://localhost:9200/index/_search' -d '{
"size" : 10,
"query" : {
"term" : { "content" : "foo" }
},
"sort" : {
"_script" : {
"script" : "if(doc["dateField1"].empty) { doc.
dateField2.value } else {doc. dateField1.value}",
"type" : "number",
"order" : "desc"
}
}
}
'

On Tuesday, October 9, 2012 10:43:52 AM UTC+2, Per wrote:

I would like to search in an index over multiple types and then sort based
on different (date) fields depending on type. Example:

index/type1/dateField1
index/type2/dateField2

Is this possible to get a single result list sorted on dateField1 or
dateField2? A workaround would be introducing a new field "dateFieldCommon"
for type1 and type2 but is there a better way?

--

--

Thanks for the suggestion but I don't think that it would work quite as I
wanted. With
"sort" : [
{ "dateField1" : {"missing" : "_last"} },
{ "dateField2" : {"missing" : "_last"} },
],

es would first sort based on dateField1 then on dateField2. _last seems to
map to 0x7FFFFFFFFFFFFFFF ..

Here are a bunch of curl commands to create test data then sort:

curl -XPUT 'http://localhost:9200/myindex/type1/1' -d '{
"dateField1" : "2009-11-15T14:12:12",
"name" : "type1.1"
}'

curl -XPUT 'http://localhost:9200/myindex/type1/2' -d '{
"dateField1" : "2012-01-11T14:12:12",
"name" : "type1.2"
}'

curl -XPUT 'http://localhost:9200/myindex/type2/1' -d '{
"dateField2" : "2011-02-12T14:12:12",
"name" : "type2.1"
}'

curl -XPUT 'http://localhost:9200/myindex/type2/2' -d '{
"dateField2" : "2010-03-22T14:12:12",
"name" : "type2.2"
}'

curl -XGET 'http://localhost:9200/myindex/_search' -d '{
"size" : 10,
"query" : {
"match_all" : {}
},
"sort" : {
"_script" : {
"script" : "if(!doc["dateField1"].empty) { doc.
dateField1.value } else {doc. dateField2.value}",
"type" : "number",
"order" : "desc"
}
}
}'

On Tuesday, October 9, 2012 6:10:22 PM UTC+2, Raffaele Sena wrote:

Given the way you structured your script you could also try sorting on
dateField1, dateField2 if you specify that missing fields go last:

Elasticsearch Platform — Find real-time answers at scale | Elastic

something like this (and I am not sure if you'll also need the
ignore_unmapped) :

"sort" : [
{ "dateField1" : {"missing" : "_last"} },
{ "dateField2" : {"missing" : "_last"} },
],

-- Raffaele

On Tue, Oct 9, 2012 at 6:27 AM, Per <dev2...@gmail.com <javascript:>>
wrote:

I was able to get single sorted result list using script based sorting.
Don't know if it's a good solution though:

curl -XGET 'http://localhost:9200/index/_search' -d '{
"size" : 10,
"query" : {
"term" : { "content" : "foo" }
},
"sort" : {
"_script" : {
"script" : "if(doc["dateField1"].empty) { doc.
dateField2.value } else {doc. dateField1.value}",
"type" : "number",
"order" : "desc"
}
}
}
'

On Tuesday, October 9, 2012 10:43:52 AM UTC+2, Per wrote:

I would like to search in an index over multiple types and then sort
based
on different (date) fields depending on type. Example:

index/type1/dateField1
index/type2/dateField2

Is this possible to get a single result list sorted on dateField1 or
dateField2? A workaround would be introducing a new field
"dateFieldCommon"
for type1 and type2 but is there a better way?

--

--

Quite an old question, but I wonder if there is a native support for such sorting (mentioned by @Per) in the newest versions of Elasticsearch? (I have a demand to cover an exact scenario). Otherwise, how critical is performance degradation in script-based solution? Will it be noticeable?