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/
138 stars 54 forks source link

Add terms as part of Searchable Posts index? #418

Closed brookecm28 closed 1 month ago

brookecm28 commented 1 month ago

Hi there,

I'm trying to add functionality to my indexing where my taxonomy terms are indexed as pages within the Searchable Posts index. Essentially, the way that the website is built utilizes the taxonomies to build out certain catalog pages, so they need to show up alongside the pages within my search results. The best way to do this is to put them into the same index. Do you have any suggestions on how I can achieve this? I'm already adding some custom fields/shared attributes to the pages, so I can't just merge the arrays before the data is altered.

Let me know if I can provide you any other information that may help out. Thanks!

tw2113 commented 1 month ago

Hi @brookecm28

Are you basically trying to index your product category/tag archives for say WooCommerce, as an item that turns up in your results? Or are these very specifically items in the "page" post type, but for some reason aren't getting indexed?

If it's the archive URLs, then those, I don't have any immediate answer for how to index, as those don't have details like a post ID that we rely on for the Algolia objectID value.

If the latter, where they're actual pages, then we'd want to try and troubleshoot why they're not getting indexed.

brookecm28 commented 1 month ago

Hi @tw2113 :)

Essentially, I've created a custom post type, Products, that has Product Categories and Product Collections associated with each product. Each Product Category and Product Collection is a taxonomy, so it's categorized by wp as a "term" instead of a "post" (see pic). However, I have a page for both the Product Category and the Product Collection that will change the content of the page based on the URL (/product-catalog/categories/combination-toilet-accessories/ will load the Product Catalog page for the Combination Toilet Accessories category, etc.).

So they are not each associated with a page, but have the permalinks to generate the each Product Catalog page. Because they are "terms" and not "posts", they are not getting indexed into the Searchable Posts index, but I need them to.

I hope this clarifies things, let me know what other details I can provide.

Screenshot 2024-06-06 at 08 19 29

tw2113 commented 1 month ago

Forgive me for getting potentially a bit pedantic, but given the types of content in WordPress, phrases do matter.

If I'm following everything correctly, the content found when visiting /product-catalog/categories/combination-toilet-accessories/, combination-toilet-accessories is a child term to a categories parent term from the product-catalog taxonomy. And thus the permalink shown, is an archive showing products that have the combination-toilet-accessories term associated with it. This is why I'm trying to differentiate between a "page" vs an "archive". Page being an individual post type, with "page" posts in it, with their own individual "page" permalink.

Because of that, it's not a standalone object still, in the context of a post, a page, an individual product, etc. A good chunk of why it's not in some way getting indexed. It's a URL that represents a collection of things, not an individual thing.

My biggest question at this point, because of the details above, is what purpose or end goal are you trying to achieve with getting these indexed in the searchable posts index? The ability to search "combination toilet accessories" and have a result? The ability to search that phrase and receive the permalink to the archive itself?

I know out of box, we do index taxonomy terms associated with posts, onto the post object itself, and I think they're considered searchable out of box. If not, we can help make the related Algolia properties searchable.

brookecm28 commented 1 month ago

For the most part, your understanding is correct. The only real clarification I have is that there is no product catalog taxonomy; rather, there are two taxonomoies, product categories and product collections that both fall within the scope of what I'm calling the product catalog, and combination-toilet-accessories is a child term that falls within the product categories taxonomy.

The end goal is, as you phrased it, "The ability to search that phrase and receive the permalink to the archive itself". Yes. I want to be able to type in "combination" and the link to the archive pop up in the autocomplete.

I know they are searchable out of the box and get indexed separately, but I need them to be in the same index for cost purposes. My client does not want to triple the costs of the searches because they see these as more pages. Adding them via the separate indexes for each of the 2 taxonomies would triple the cost because everything else is indexed within one index (searchable posts). So we'd be going from hitting one index on a query to a multi-query hitting 3 separate indices with each keystroke.

Is it possible to achieve this end goal while keeping everything in the one index?

tw2113 commented 1 month ago

What all parts are you wanting to get indexed? Term name (for result display) and term permalink?

I'll give some things some thoughts and ponderings, but honestly the whole setup that we have with the plugin, which was inherited from Algolia themselves, was that the index was meant for searchable posts only, not term archives too.

brookecm28 commented 1 month ago

Yes, both of those, and then also a field called autocomplete_preview_content with a value of 'Product Catalog for ' . $term->name. Those 3 things are really all I need

tw2113 commented 1 month ago

Given the discussion already, and wanting to not attempt to alter the core codebase at all for this, as it's really not a common request that we get, this is the best I can think of at the moment.

I am not claiming perfection on any level, and this solution is really best for a limited amount of archives.

The essence of this is taking an existing non-essential post, and hijacking it. The example code below takes say the "Hello World" post ID 1, and then grabs what would be the "uncategoried" category. Once the category term is found, we take the originally calculated records for the "Hello World" post, fetch only the FIRST record from the potentially many and replace the post title with the term name, replace the permalink with the term archive link, and then the content with the autocomplete_preview_content. I assumed term meta for the sake of this example.

We then return the the mix of original properties from Hello World, alongside the replaced values.

I don't know how I'd go about doing this for 2-to-many as that'd get unwieldy pretty quick, without a set list of planned archive links. Plus there'd need to be the enough proper posts to override for all of that.

function algolia_fake_term_archives_as_post( $records, $post ) {
    if ( 1 !== $post->ID ) {
        return $records;
    }
    $the_taxonomy = 'category';
    $term = get_term_by( 'term_id', 1, $the_taxonomy );

    if ( $term ) {
        $preview = get_term_meta( $term->term_id, 'autocomplete_preview_content', true );

        $new_records                  = []; // Create new array
        $new_records[]                = $records[0]; //Copy first record only
        $new_records[0]['post_title'] = $term->name;
        $new_records[0]['permalink']  = get_term_link( $term->term_id );
        $new_records[0]['content']    = $preview;

        return $new_records;
    }

    return $records;
}
add_filter( 'algolia_searchable_post_records', 'algolia_fake_term_archives_as_post', 10, 2 );
brookecm28 commented 1 month ago

Hi @tw2113!

Thank you for your suggestion about using existing posts. What I ended up doing is creating a new post for each one that had an ACF field to mark it as a product catalog-specific post and changing the on each post when it indexes in Algolia ( was already altering some other on index, so I just put the functionality there). So any time that result is clicked on in the search, it will take the user to the correct page.

Thanks again!

tw2113 commented 1 month ago

Welcome.