# How to prioritize more recent content?

**URL:** <https://discuss.elastic.co/t/how-to-prioritize-more-recent-content/134100>\
**Category:** Elasticsearch\
**Created:** [May 31, 2018, 7:17pm UTC](https://discuss.elastic.co/t/how-to-prioritize-more-recent-content/134100 "2018-05-31T19:17:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![techliminal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/techliminal/32/29996_2.png) [@techliminal](https://discuss.elastic.co/u/techliminal)\
**Post date:** [May 31, 2018, 7:17pm UTC](https://discuss.elastic.co/t/how-to-prioritize-more-recent-content/134100/1 "2018-05-31T19:17:43Z")

</div>

Hello there,

I'm using ElasticSearch to search through a WordPress database. I'd like to create a query that returns results based on score, where the score is improved when a document is found with a more recent date, along with whether the text appears in the title or description.

Here's a query that searches through several fields, and returns results ordered by score:

```
GET server.local/_search
{
"size":24,
"from":0,
"query":{
 "multi_match":{
   "query":"bagel",
   "fields":["post_title^3","post_excerpt^2","post_content","post_author.display_name","terms.category.name", "terms.resource-category.name","terms.post_tag.name"],
   "type":"cross_fields",
   "operator":"or"}
  
},
    "_source": ["post_id", "post_title", "post_author.display_name", "post_date.date"]
}

```

I tried sorting by date by adding this clause:

```
"sort":[{"post_date.date":"desc"}]

```

But this removed the scores from the results altogether.

I noticed that i can sort by score and then by date, but the scores come back with far too much precision for this to be useful. (e.g. a score is 15.125123 and the next one is 15.031, but the document is more recent).

Is there a way to either (a) round down the score so that I can then sort all the 15's by date or (b) add a clause that subtracts from the score when a document is older than a certain date?

Thank you in advance for any pointers.

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [May 31, 2018, 7:44pm UTC](https://discuss.elastic.co/t/how-to-prioritize-more-recent-content/134100/2 "2018-05-31T19:44:56Z")

</div>

I believe the truncating scores will lead you down some inconsistent paths.

You can wrap your query in a function score query and do some boosting based on timestamps: [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)

The blog has an example that works in your context: [https://www.elastic.co/blog/found-function-scoring](https://www.elastic.co/blog/found-function-scoring)

I found that the decay functions have a big performance impact. I prefer to use pre-determined ranges for boost levels., Makes the mental model easier as well. For example: if within the past week, boost by 4. For the two weeks after that, boost by 2. Ranges are dependent on your use case.

---

<div class="post-metadata">

**Author:** ![techliminal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/techliminal/32/29996_2.png) [@techliminal](https://discuss.elastic.co/u/techliminal)\
**Post date:** [June 4, 2018, 6:15pm UTC](https://discuss.elastic.co/t/how-to-prioritize-more-recent-content/134100/3 "2018-06-04T18:15:24Z")

</div>

Thank you Ivan! I didn't know what to search for, so that helped a lot. I wound up adding scoring functions that multiply the score by 5 if it's within the past year, and by 2 if it's within the last 4.

```
  "function_score" : {
      "query":{
          "multi_match":{
              "query":"bagel",
               "fields": ["post_title^3","post_excerpt^2","post_content","post_author.display_name","terms.category.name","terms.resource-category.name","terms.post_tag.name"],
              "type":"cross_fields",
              "operator":"or"}
   },
"functions": [
     {"filter": { 
         "range" : { "post_date.date": { "gte" : "now-1y", "lte" : "now" } }} ,
         "weight": 5
     },
     {"filter": { 
        "range" : { "post_date.date": { "gte" : "now-4y", "lt" : "now-1y" } }} ,
        "weight": 2
      }
   ],
  "boost_mode": "multiply"
```

---

<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 2, 2018, 6:15pm UTC](https://discuss.elastic.co/t/how-to-prioritize-more-recent-content/134100/4 "2018-07-02T18:15:30Z")

</div>

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