Scoring by inner hits' values

I have an index that has nested objects. The objects have "id" and "rank" properties. I'd like to sort the main objects (the parents, I guess) by the sum of the "rank" of ONLY the matching nested objects. So if I have something tagged "angry", "rock" and "metal," but I search "angry rock," I only want to sum "angry" and "rock" ranks, I want to ignore the "metal" tag's rank.

Mapping:

{
   "mappings" : {
      "songs" : {
         "properties" : {
            "tags" : {
               "type" : "nested",
               "properties" : {
                  "id" : {
                     "type" : "integer"
                  },
                  "likeness_rank" : {
                     "type" : "integer"
                  }
               }
            }
         }
      }
   }
}

So if I search "angry rock", and a few songs were tagged with "angry" and "rock", but one song had a lower combined rank of those two tags (meaning it has other tags ranked above those), it would be lower on the list than a song whose highest ranked tags are "angry" and "rock".

How would I do this?

I've tried variations of something like this to no avail:

GET _search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "artists",
            "query": {
              "match": {
                "artists.name": "Artist 2"
              }
            }
          }
        },
        {
          "nested": {
            "path": "taggable_tags",
            "query": {
              "match": {
                "taggable_tags.tag_id": 111
              }
            }
          }
        },
        {
          "nested": {
            "path": "taggable_tags",
            "query": {
              "function_score": {
                "script_score": {
                  "script": "doc['taggable_tags.likeness_rank'].value"
                },
                "query": {
                  "constant_score": {
                    "filter": {
                      "term": {
                        "tag_id": 111
                      }
                    }
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
} 

But this doesn't return any results (when it definitely should).

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