10up / ElasticPress

A fast and flexible search and query engine for WordPress.
https://elasticpress.io
GNU General Public License v2.0
1.25k stars 313 forks source link

Enhancement: Add ability to boost specific keywords #3534

Open oscarssanchez opened 1 year ago

oscarssanchez commented 1 year ago

Is your enhancement related to a problem? Please describe.

We can weight specific fields but Elastic also gives you the chance to boost specific keywords via a query string query. See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_boosting

We would probably need to add this to the queries we set by default, and also think about scalability issues when adding keywords.

Designs

No response

Describe alternatives you've considered

No response

Code of Conduct

felipeelia commented 1 year ago

I made some exploration on this one and it seems it is not possible to use boosting and function_score in the same query. For anyone wanting to continue the discovery, here is how far I went: https://gist.github.com/felipeelia/8fc76ae6e2eda6d69d8cc7f21086f6eb

oscarssanchez commented 1 year ago

An alternative could be using an additional should clause

add_filter( 'ep_formatted_args', function( $formatted_args, $args ) {
$formatted_args["query"]["function_score"]["query"]["bool"]["should"][3]["multi_match"]["fields"] = 'terms.category.name';
$formatted_args["query"]["function_score"]["query"]["bool"]["should"][3]["multi_match"]["query"] = 'myterm';

return $formatted_args;
}, 9999999, 2 );

If we make the enhancement this wouldn't go in the ep_formatted_args. We would probably need to work on limiting the available fields with this approach as well.