Aggregating, picking top value using ordering then ordering buckets by score

Imagine a packages repository like NuGet or NPM. Multiple packages with the same ID and many versions.

These are entered individually as a document into the index.

Now in a query I want to aggregate them by ID, sort them by version and pick the highest version.
I can do that with something like this:

{
    "query": {
        "multi_match" : {
            "query" : "jquery",
            "fields": [
              "packageId^9",
              "title^8",
              "summary^7",
              "description^6",
              "releaseNotes^5"
            ]
        }
    },
    "size": 0,
    "aggs": {
      "group_by_package": {
        "terms": {
          "field": "packageId.keyword",
          "size": 100000
        },
        "aggs": {
          "top_package": {
            "top_hits": {
              "size": 1,
                "sort": [
                {
                  "versionMajor": {
                    "order": "desc"
                  }
                },
                {
                  "versionMinor": {
                    "order": "desc"
                  }
                },
                {
                  "versionPatch": {
                    "order": "desc"
                  }
                },
                {
                  "versionRevision": {
                    "order": "desc"
                  }
                },
                {
                  "versionReleaseLabels.keyword": {
                    "order": "desc"
                  }
                }
              ]
            }
          }
        }
      }
    }
}

What I noticed however is the score has disappeared, I think it's down to custom sorting, but I want scoring so I can order the buckets.

How can I achieve this?

This seems to work after modifying aggs:

"aggs": {
  "group_by_package": {
    "terms": {
      "field": "packageId.keyword",
      "size": 100000,
      "order": {
        "max_score": "desc"
      }
    },
    "aggs": {
      "top_package": {
        "top_hits": {
          "size": 1,
          "track_scores": true,
            "sort": [
            {
              "versionMajor": {
                "order": "desc"
              }
            },
            {
              "versionMinor": {
                "order": "desc"
              }
            },
            {
              "versionPatch": {
                "order": "desc"
              }
            },
            {
              "versionRevision": {
                "order": "desc"
              }
            },
            {
              "versionReleaseLabels.keyword": {
                "order": "desc"
              }
            }
          ]
        }
      },
      "max_score": {
          "max": {
            "script": "_score"
          }
        }
    }
  }
}

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