- πΊπΈ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.