Boost recently created content

Created on 19 March 2018, about 7 years ago
Updated 13 June 2024, 10 months ago

Hi there,

Firstly, thanks for the useful module.

One request I have is on how to boost the relevancy score based on a date field.

For example, "Published date" how to add the boost to the search at query time.

I've found how to do it with Solr, but am evaluating Elasticsearch and need to try there too.

I'd like to do it via a "decay scoring function", to decrease the scoring versus time :

i.e: To add something like adding this ES syntax:

{
 "query": {
 "function_score": {
    "functions": [
     {
      "linear": {
        "published_date": {
          "origin": 1398673886,
          "scale": "1h",
          "offset": 0,
          "decay": 0.1
        }
      }
    }
    ]
   }
  }
 }

Please advise if possible, thanks.

πŸ’¬ Support request
Status

Active

Version

7.0

Component

Code

Created by

πŸ‡³πŸ‡ΏNew Zealand davidwhthomas

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States bburg Washington D.C.

    @Renrhaf How does this work? I don't see that this hook exists anywhere. The documented hooks in the 8.0-7.x version of the module are

    • hook_elasticsearch_connector_load_library_options_alter
    • hook_elasticsearch_connector_supported_data_types_alter

    ...and those are being removed in 8.x in favor of event subscribers. I'm struggling myself on figuring out how to alter ES queries to do just what OP is stating, boost by recency. Trying to do this has been challenging to figure out, as I assume I can alter the query with the query object. But it seems like what I actually need to do is add some additional parameters, similar to what Renrhaf is trying to do. When I try their example code, I get back zero results. Here is my non-working code at the moment. At least it doesn't break the query:

      public function boostSearchByRecencyParams($event) {
        //return;
        $params = $event->getElasticSearchParams();
    
        // Current time in milliseconds.
        $now = time() * 1000;
    
        $queryString = $params['body']['query'];
    
        $params['body']['query'] = [
          'function_score' => [
            'score_mode' => 'sum',
            'boost_mode' => 'multiply',
            'functions' => [
              [
                'weight' => 1,
              ],
              [
                'weight' => 20,  // Increase the weight to see a more significant boost
                'gauss' => [
                  'created' => [
                    'origin' => $now,
                    'scale' => 365 * 24 * 60 * 60 * 1000, // 365 days in milliseconds
                    'decay' => 0.5,
                  ],
                ],
              ],
              [
                'weight' => 10,  // Increase the weight to see a more significant boost
                'gauss' => [
                  'created' => [
                    'origin' => $now,
                    'scale' => 2 * 365 * 24 * 60 * 60 * 1000, // 2 years in milliseconds
                    'decay' => 0.5,
                  ],
                ],
              ],
            ],
            'query' => $queryString,
          ],
        ];
    
        $event->setElasticSearchParams($params);
      }
    

    Perhaps the difference here is I am using the core "created" property instead of a date field, which is a unix timestamp instead of an actual date field in ES.

Production build 0.71.5 2024