10up / ElasticPress

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

Default Search Order Should Be An Option #3815

Open realrellek opened 9 months ago

realrellek commented 9 months ago

Is your enhancement related to a problem? Please describe.

Hi,

I'm playing around with ElasticPress because bots are spamming our search function with, well, spam and this puts unnecessary pressure on our database.

So if you have it index everything (even protected content), search from within wp-admin is also done using Elastic. Which is nice. What is not so nice is that the default order is by score. I think this is not very intuitive for the editor.

So when playing around, I found that there is a filter, ep_set_default_sort, but that only handles the normal listing of posts. Its default is by post_date desc, which is fine because that is how WP defaults too.

When you actually do a search, you need to hook into ep_set_sort. And it is not very obvious what you have to return. For Google: This would be the answer:

add_filter( 'ep_set_sort', 'rellek_ep_sort_results_by_date_desc', 10, 2 );
function rellek_ep_sort_results_by_date_desc($sort, $order)
{
    return array(
        'post_date' => array(
            'order' => 'DESC',
        ),
    );
}

Well and I think this should be an UI option. Because first of all, you find all sorts of confusing answers to this problem (maybe leftovers from older versions) with wrong information - such as you need to return "date" (while for this hook, you need to return "post_date"). And then I feel like you could offer all the other possible values, like score.

Designs

No response

Describe alternatives you've considered

I guess adding this filter see above does the trick... For now. But when you change your internal data structure, it might stop working. And being a filter, it also would not throw an error, so it'd be hard to debug why the behaviour changes.

Also WP's default order (if nothing else is specified) is by post_title (if the queried word is included) and by post_date. So having the default changed to at least post_date desc would come closer to what stock WP does.

Code of Conduct

felipeelia commented 2 months ago

We can add a new checkbox to the Protected Content feature to sort by score on searches by default or not.