algoliasearch-wordpress
algoliasearch-wordpress copied to clipboard
Indexed attributes not appearing in search
What did you expect to happen?
Using the code below (modified from https://community.algolia.com/wordpress/advanced-custom-fields.html), Algolia should add the attributes indicated and make them searchable. The attributes are being added to the index, but the search returns an empty result. Any ideas what's missing here?
add_filter( 'algolia_post_shared_attributes', 'my_item_attributes', 10, 2 );
add_filter( 'algolia_searchable_post_shared_attributes', 'my_item_attributes', 10, 2 );
/**
* @param array $attributes
* @param WP_Post $post
*
* @return array
*/
function my_item_attributes( array $attributes, WP_Post $post ) {
if ( 'item' !== $post->post_type ) {
// We only want to add an attribute for the 'item' post type.
// Here the post isn't a 'item', so we return the attributes unaltered.
return $attributes;
}
// Get the field value with the 'get_field' method and assign it to the attributes array.
// @see https://www.advancedcustomfields.com/resources/get_field/
$image = get_field('ii_image_upload', $post->ID);
$attributes['display_image_url'] = $image['url'];
$attributes['display_image_alt'] = $image['alt'];
$attributes['type'] = get_field('ii_item_type', $post->ID);
$attributes['description'] = get_field('ii_item_description', $post->ID);
$attributes['access'] = get_field('iap_access_levels', $post->ID);
$attributes['keywords'] = get_field('ii_keywords', $post->ID);
for ($i = 0; $i < 2; $i++) {
$attributes['section' . $i] = get_field('ii_content_sections_' . $i . '_ii_section_text', $post->ID);
}
// Always return the value we are filtering.
return $attributes;
}
add_filter( 'algolia_posts_index_settings', 'my_items_index_settings' );
/**
* @param array $settings
*
* @return array
*/
function my_items_index_settings( array $settings ) {
if ( 'item' !== $post->post_type ) {
// We only want to add an attribute for the 'item' post type.
// Here the post isn't a 'item', so we return the attributes unaltered.
return $settings;
}
// Make Algolia search into the fields when searching for results.
// Using ordered instead of unordered would make words matching in the beginning of the attribute
// make the record score higher.
// @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestoindex
$settings['attributesToIndex'][] = 'unordered(description)';
$settings['attributesToIndex'][] = 'unordered(keywords)';
for ($i = 0; $i < 2; $i++) {
$settings['attributesToIndex'][] = 'unordered(section' . $i . ')';
}
// Make Algolia return a pre-computed snippet of 50 chars as part of the result set.
// @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestohighlight
$settings['attributesToSnippet'][] = 'description:50';
// Always return the value we are filtering.
return $settings;
}
Technical info
- WordPress version: 4.9.4
- Algolia Search plugin version: 2.8.0
Hey @jdozierezell ,
That looks rather good to me. Did you hit the "push settings" button after having introduced the usage of the 2 filters?
If that doesn't work, maybe deleting the indices and re-indexing?
Thanks @rayrutjes. I've been pushing settings and reindexing without success. I just tried deleting but that didn't work either. Any chance that this is related to the Algolia shift from attributesToIndex to searchableAttributes? I tried both and neither worked, but I'm wondering if there's a conflict somewhere.
Hey @jedrzejchalubek could it be that you are testing on the searchable posts index? Because I don't see the settings applied on that index:
add_filter( 'algolia_searchable_posts_index_settings', 'my_items_index_settings' );