WebDevStudios / wp-search-with-algolia

Improve search on your site. Autocomplete is included, along with full control over look, feel and relevance.
https://wordpress.org/plugins/wp-search-with-algolia/
141 stars 54 forks source link

No highlighting in search results #421

Closed mrfsrf closed 3 months ago

mrfsrf commented 3 months ago

Describe the bug For some reason i dont have search highlight on results. Tried to error log inside class-algolia-search.php inside method pre_get_posts

        try {
            $results = $this->index->search( $query->query['s'], $params, $order_by, $order );
            error_log('Algolia search results: ' . print_r($results, true));

        } 

inside the debug.log there is: [_highlightResult] => Array. Also made sure that highlightning is enabled

    private function highlighting_enabled() : bool {
        $enabled = apply_filters( 'algolia_search_highlighting_enabled', true );
    error_log('Highlighting enabled: ' . var_export($enabled, true));
    return $enabled;
        // return apply_filters( 'algolia_search_highlighting_enabled', true );
    }

which is true. Do i miss something?

Im using Use Algolia with the native WordPress search template as an option

tw2113 commented 3 months ago

Can you verify whether or not you have this small style block being output in the <head> of your search results page?

<style>
    .algolia-search-highlight {
        background-color: #fffbcc;
        border-radius: 2px;
        font-style: normal;
    }
</style>

Also worth checking/confirming the Highlighting section of your index's configuration. If you've set anything specific for highlighting, you'd want to add in post_title, and post_excerpt. If you haven't customized that spot, then it should be applying to all searchable attributes of which, both of these are out of box with the plugin.

mrfsrf commented 3 months ago

yup. the algolia-search-highlight css class is in the head. Second part i dont fully understand. Do you mean this?

Screenshot 2024-06-20 at 19 55 37
tw2113 commented 3 months ago

Specifically the "UI Demos" around the top middle space in the image. If you're not using that "UI Demos" part, then I'm not quite sure which Display Preferences you're referring to.

Also make sure you're working with the wp_searchable_posts index, with whatever prefix you chose, as that's going to be the index used for with Native Search template.

mrfsrf commented 3 months ago
Screenshot 2024-06-20 at 21 30 09 Screenshot 2024-06-20 at 21 29 56

I created the UI demo and those are the results. What im baffled about is that method highlight_the_title (inside Algolia_Search class) for example is not logging out

    public function highlight_the_title( $title, $post_id ) {
        error_log("Highlighting title for post ID: $post_id");

        $highlighted_title = $this->current_page_hits[ $post_id ]['_highlightResult']['post_title']['value'] ?? null;

        if ( ! empty( $highlighted_title ) ) {
            $title = $highlighted_title;
        }

        return $title;
    }
tw2113 commented 3 months ago

I'd be able to troubleshoot a lot better with a link to the live page where the issue is happening, as opposed to their UI Demos area. The demo area is not where the issue is.

The highlight_the_title() method should be getting called on the the_title filter, so if you're not using that in your theme, that may be why that one is failing.

Just to be certain, this spot on the right of the screenshot, for your index, is empty, correct?

Screen Shot 2024-06-20 at 3 05 44 PM

If empty, then as the text says, all searchable attributes should be getting the highlight treatment.

mrfsrf commented 3 months ago

Thanks! Manged to solved it. Had to call correct filters on title and excerpt

tw2113 commented 3 months ago

Glad to hear you got things figured out :)