Sorting when scoring documents with child function_score

Hi all,

Running into a tricky requirement when it comes to sorting in search so would appreciate getting some thoughts or advice on the matter

We have two collections of documents set with a join. The child documents have a date field and a value field. In search, the child collection has a restriction variable so that users are only able to access certain documents marked with the appropriate/related variable (think private companies or organizations). Since we don't know what restrictions will be relevant beforehand, this prevents us from enriching the parent document with max/min sorted values and directly sorting based on a value from the parent document. So for our normal parent sorting code, we are using function_score scoring using the value field on the child document, with score_mode = max for dsc sort and min for asc sort. Example:

{
  "function_score": {
    "query": { "match_all": { "boost": 1.0 } },
    "functions": [
      {
        "filter": { "match_all": { "boost": 1.0 } },
        "script_score": {
          "script": {
            "id": "script_id",
            "params": { "field": "_$value" }
          }
        }
      }
    ],
    "score_mode": "max", // min for asc
    "max_boost": 3.4028235e38,
    "boost": 10.0
  }
}
...
"sort": [
  { "_score": { "order": "desc" } }, // min for asc
  { "id": { "order": "desc" } }
],

Now we have a requirement that we need to score the parent using the value of the most recent dated child document (which can be different depending on the user performing the search), instead of the min or max score of all of the child documents. As far as I have been able to tell, there's not a way to sort the child documents by some field or variable prior to scoring the parent result, but perhaps I'm missing something. Is there a way to get the "max" where field is _$date but then score min or max based on _$value?

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