Through sorting by different fields


(M.ax) #1

I'd like get some sorted docs from elasticsearch. The problem is i'd like to get docs sorted by different fields, but I want to have results in one resultset.
For example:
First doc from elastic:

0:  {
     "_index": "documents"
     "_type": "order"
     "_id": "282"
     "_score": 1
     "_source": {
        "id": "282"
        ...
        "bookingsCost": 2047.21
        "annulmentsCost": 133.56
        "sendToPublisher": 578.05
     }
}

Another doc:

1:  {
     "_index": "documents"
     "_type": "collectedOrdersReport"
     "_id": "287"
     "_score": 1
     "_source": {
        "id": "287"
        ...
        "bookingsCost": 4611.22
        "annulmentsCost": 0
        "sendToPublisher": 4611.22
        "collectedTotalCostWithVat": 272.74
        "collectedTotalVat": 41.61
     }
}

Docs with type order (like first one) should be sorted by field sendToPublisher, but docs with type collectedOrdersReport (second) are sorted by field collectedTotalCostWithVat. Is there a way to get docs both types in one resultset with a through sorting by different fields?


(Nik Everett) #2

Try script based sorting. Try using doc['_type'] to get the type and then using the field. You might also want to compare that to just trying to get one of the fields and if it isn't found then trying the other field. In some cases that might be faster.


(system) #3