Home > Software engineering >  How to order by two different attributes on same document in elasticsearch?
How to order by two different attributes on same document in elasticsearch?

Time:11-30

I have documents as

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "journeys-development-latest",
        "_type" : "_doc",
        "_id" : "1399",
        "_score" : 1.0,
        "_source" : {
          "draft_recent_edit_at" : "2023-01-14T04:16:41.318Z",
          "recent_edit_at" : "2022-09-23T14:13:41.246Z"
        }
      },
      {
        "_index" : "journeys-development-latest",
        "_type" : "_doc",
        "_id" : "1394",
        "_score" : 1.0,
        "_source" : {
          "draft_recent_edit_at" : "2022-07-02T16:19:41.347Z",
          "recent_edit_at" : "2022-12-26T10:12:41.333Z"
        }
      },
      {
        "_index" : "journeys-development-latest",
        "_type" : "_doc",
        "_id" : "1392",
        "_score" : 1.0,
        "_source" : {
          "draft_recent_edit_at" : "2022-05-20T11:33:41.372Z",
          "recent_edit_at" : "2021-12-21T03:36:41.359Z"
        }
      }
    ]
  }
}

What I know is if I do

{
  "size": 12,
  "from": 0,
  "query": {
    ......,
    ......
  },
  "sort": [
    {
      "recent_edit_at": {
        "order": "desc"
      }
    }
  ]
}

This will order by recent_edit_at in desc order.

Similarly replacing recent_edit_at with draft_recent_edit_at will order by draft_recent_edit_at in desc order.

What I am struggling is to find a way where I can say I want to order by max in draft_recent_edit_at, recent_edit_at and then order the documents according to those.

===========================Update===========================

After adding sort proposed by HPringles the output is

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "Math.max(doc['draft_recent_edit_at'].value.toInstant().toEpochMilli(),\n                   doc['recent_edit_at'].value.toInstance().toEpochMilli())\n          ",
          "                                                                                                                     ^---- HERE"
        ],
        "script": "\n          Math.max(doc['draft_recent_edit_at'].value.toInstant().toEpochMilli(),\n                   doc['recent_edit_at'].value.toInstance().toEpochMilli())\n          ",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "journeys-development-latest",
        "node": "GGAHq1ufQQmSqeLRyzka5A",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "Math.max(doc['draft_recent_edit_at'].value.toInstant().toEpochMilli(),\n                   doc['recent_edit_at'].value.toInstance().toEpochMilli())\n          ",
            "                                                                                                                     ^---- HERE"
          ],
          "script": "\n          Math.max(doc['draft_recent_edit_at'].value.toInstant().toEpochMilli(),\n                   doc['recent_edit_at'].value.toInstance().toEpochMilli())\n          ",
          "lang": "painless",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "dynamic method [org.elasticsearch.script.JodaCompatibleZonedDateTime, toInstance/0] not found"
          }
        }
      }
    ]
  },
  "status": 400
}

CodePudding user response:

If I'm understanding correctly, you can do this with a painless script at runtime.

See below:

"sort": {
      "_script": {
        "type": "number",
        "script": {
          "lang": "painless",
          "source": """
          Math.max(doc['draft_recent_edit_at'].value.toInstant().toEpochMilli(),
                   doc['recent_edit_at'].value.toInstance().toEpochMilli())
          """,
          "params": {
            "factor": 1.1
          }
        },
        "order": "asc"
      }
    }

This will work out the maximum of the two, and then sort based on that value.

CodePudding user response:

As far as I know you might also want to convert the Epoch values to long.

Something like -

"sort": {
      "_script": {
        "type": "number",
        "script": {
          "lang": "painless",
          "source": """
             long draft_recent_edit_at = doc['draft_recent_edit_at'].value.toInstant().toEpochMilli();
             long recent_edit_at = doc['recent_edit_at'].value.toInstant().toEpochMilli();
             Math.max(draft_recent_edit_at, recent_edit_at);
          """
        },
        "order": "asc"
      }
    }
  • Related