# Scoring tagged documents with custom scores

**URL:** https://discuss.elastic.co/t/scoring-tagged-documents-with-custom-scores/231224
**Category:** Elasticsearch
**Created:** [May 5, 2020, 9:21pm UTC](https://discuss.elastic.co/t/scoring-tagged-documents-with-custom-scores/231224 "2020-05-05T21:21:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Charles\_Lariviere](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/charles_lariviere/32/67753_2.png) [@Charles\_Lariviere](https://discuss.elastic.co/u/Charles_Lariviere)
#### Post date: [May 5, 2020, 9:21pm UTC](https://discuss.elastic.co/t/scoring-tagged-documents-with-custom-scores/231224/1 "2020-05-05T21:21:46Z")

</div>

Hey folks,

We're interested in customizing ElasticSearch relevance scoring by using our own relevance values for each tag that was added to a document. We're still early in our exploration process but would appreciate some guidance on how to best achieve this with ElasticSearch (and whether it _is_ possible).

Given this document:

```auto
{
  "id": "10252",
  "popularity_score": "1.28",
  "tags": [
    {
      "tag": "beach",
      "relevance": "0.7"
    },
    {
      "tag": "illustration",
      "relevance": "0.3"
    },
    {
      "tag": "california",
      "relevance": "0.9"
    },
  ]
}

```

and given the following query:

```auto
"query" : "beach illustration"

```

We would like the score for this document to be:

```auto
"score" = mean(relevance) * popularity_score
        = mean([0.7, 0.3]) * 1.28
        = 0.64

```

From our research, this sounds possible through nested queries and script score. However, the following case complicates things slightly:

For the same document, and the following query:

```auto
"query": "house illustration"

```

We would like the score to be:

```auto
"score" = mean([0, 0.3]) * 1.28

```

(since the document does _not_ have the tag "house")

We wouldn't want to store all possible tags (\>100k) on each document, and it sounds like the sparse vector data type would have been useful here -- however it appears to have been deprecated in 7.6.

Any guidance here (or even just if this is indeed possible to do) would be greatly appreciated. Thanks!

---

<div class="post-metadata">

### Author: ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)
#### Post date: [May 7, 2020, 9:03pm UTC](https://discuss.elastic.co/t/scoring-tagged-documents-with-custom-scores/231224/2 "2020-05-07T21:03:14Z")

</div>

There is a new datatype called [rank\_features](https://www.elastic.co/guide/en/elasticsearch/reference/current/rank-features.html) that may help is your use-case. Queries on it are very efficient. However there is no possibility of custom scoring on features, there are only 3 available [predefined functions](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-rank-feature-query.html) on rank\_features.

If you model your tags as rank\_features, you can run the following query:

```auto
{
  "query": {
    "script_score": {
      "query": {
        "bool": {
          "should": [
            {
              "rank_feature": {
                "field": "tags.beach"
              }
            },
            {
              "rank_feature": {
                "field": "tags.illustration"
              }
            }
          ]
        }
      },
      "script": {
        "source": "_score * doc['popularity_score'].value"
      }
    }
  }
}

```

Note that, `_score` here will be calculated by default using saturation function.

We also currently [discussing a possibility of using linear function](https://github.com/elastic/elasticsearch/issues/49859) for rank\_features.

---

<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: [June 4, 2020, 9:16pm UTC](https://discuss.elastic.co/t/scoring-tagged-documents-with-custom-scores/231224/3 "2020-06-04T21:16:41Z")

</div>

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