# Scoring by inner hits' values

**URL:** <https://discuss.elastic.co/t/scoring-by-inner-hits-values/89501>\
**Category:** Elasticsearch\
**Created:** [June 15, 2017, 6:01am UTC](https://discuss.elastic.co/t/scoring-by-inner-hits-values/89501 "2017-06-15T06:01:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![robokid](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robokid/32/19083_2.png) [@robokid](https://discuss.elastic.co/u/robokid)\
**Post date:** [June 15, 2017, 6:01am UTC](https://discuss.elastic.co/t/scoring-by-inner-hits-values/89501/1 "2017-06-15T06:01:03Z")

</div>

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:

```auto
{
   "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?

---

<div class="post-metadata">

**Author:** ![robokid](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robokid/32/19083_2.png) [@robokid](https://discuss.elastic.co/u/robokid)\
**Post date:** [June 15, 2017, 3:34pm UTC](https://discuss.elastic.co/t/scoring-by-inner-hits-values/89501/2 "2017-06-15T15:34:55Z")

</div>

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

```auto
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).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 13, 2017, 3:35pm UTC](https://discuss.elastic.co/t/scoring-by-inner-hits-values/89501/3 "2017-07-13T15:35:35Z")

</div>

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