# Elasticsearch - decay documents using property value

**URL:** <https://discuss.elastic.co/t/elasticsearch-decay-documents-using-property-value/190205>\
**Category:** Elasticsearch\
**Created:** [July 12, 2019, 10:02am UTC](https://discuss.elastic.co/t/elasticsearch-decay-documents-using-property-value/190205 "2019-07-12T10:02:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![WildVictor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wildvictor/32/49985_2.png) [@WildVictor](https://discuss.elastic.co/u/WildVictor)\
**Post date:** [July 12, 2019, 10:02am UTC](https://discuss.elastic.co/t/elasticsearch-decay-documents-using-property-value/190205/1 "2019-07-12T10:02:12Z")

</div>

My documents are made of categories. There are 40 different categories these are added to the document manually in database and indexed. This is how my document looks like:

```
{
  "name": "..",
  "categoryA": "..",
  "categoryB": "..",..
  "categoryDecayScore": 0.0 - 1.0
}

```

The documents are considered well covered if they are part of all 40 categories. So to push documents in all categories to the top I wanted to use the decay function to reduce the score of those who are part of less categories.

For this I use the `categoryDecayScore` property which is set at index time. If document is part of all 40 categories than it's `categoryDecayScore` will be `0.0` if it's missing half but has more than a 1/3 it will get a score of `0.2` and if it has less than 1/3 it will get a score of `0.3`.

Then I also increase `categoryDecayScore` by 0.02 for less relavant scores.

**What I want to do:**  
I would like documents who have `categoryDecayScore > 0.0` to have their score decayed the farther they are from `0.0`.

This is my filter function:

```
"filter": {
        "exp": {
          "categoryDecayScore" : {
            "origin" : 0.0,
            "scale" : 1.0,
            "offset" : 0.0,
            "decay" : 0.5
          }
        }
}

```

The way I understand documentation here:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html)

Is that `origin` is my point of reference and all documents who have `categoryDecayScore > 0.0` will be decayed and any with `categoryDecayScore >= 1.0` will be decayed by `0.5`.

However looking at my results it seems this does not take affect. The top 4 documents all have the same score but here are the `categoryDecayScore` values:

```
{
  _score: 51.970146,
  categoryDecayScore: 0.04
},
{
  _score: 51.970146,
  categoryDecayScore: 0.2
},
{
  _score: 51.970146,
  categoryDecayScore: 0.02
},
{
  _score: 51.970146,
  categoryDecayScore: 0.3
}

```

Is this normal behaviour or am I understanding the decay function incorrectly. My assumption based on docs is:

- origin: point of reference from which distance is calculated
- scale: upper point after which all documents are decayed by value of decay param
- offset: point after which documents are decayed
- decay: decay amount for all documents scored above or at scale value

---

<div class="post-metadata">

**Author:** ![WildVictor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wildvictor/32/49985_2.png) [@WildVictor](https://discuss.elastic.co/u/WildVictor)\
**Post date:** [July 12, 2019, 12:11pm UTC](https://discuss.elastic.co/t/elasticsearch-decay-documents-using-property-value/190205/2 "2019-07-12T12:11:02Z")

</div>

Weird thing I noticed using ?explain=true is:

```
{
    "value" : 1.0,
    "description" : "Function for field categoryDecayScore:",
    "details" : [
      {
          "value" : 1.0,
          "description" : "exp(-0.5*pow(MIN[Math.max(Math.abs(0.0(=doc value) - 0.0(=origin))) - 0.2(=offset), 0)],2.0)/0.7213475204444817)",
          "details" : []
      }
    ]
}

```

is this: `0.0(=doc value)` should this not be 0.3 as in the `_source` ?

---

<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:** [August 9, 2019, 12:11pm UTC](https://discuss.elastic.co/t/elasticsearch-decay-documents-using-property-value/190205/3 "2019-08-09T12:11:07Z")

</div>

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