0

I'm currently using Algolia's Search plugin within Worpdress. I've managed to push some custom fields and their values to custom attributes within Algolia. Now, I'm trying to include a custom attribute named 'program_description' in my search results.

By default, the search is only returning the value of the 'post_title' and 'content' attributes. I'm interested in replacing the 'content' attribute output with with my custom attribute's output('program_description').

I thought I would simply modify the instantsearch.php template by adding 'program_description' to the attributes array like so:

<div class="ais-hits--content">
    <h3 itemprop="name headline"><a href="{{ data.permalink }}" title="{{ data.post_title }}" itemprop="url">{{{ data._highlightResult.post_title.value }}}</a></h3>
    <div class="ais-hits--tags">
        <# for (var index in data.taxonomies.post_tag) { #>
        <span class="ais-hits--tag">{{{ data._highlightResult.taxonomies.post_tag[index].value }}}</span>
        <# } #>
    </div>
    <div class="excerpt">
        <p>
            <#
            var attributes = ['program_description', 'content', 'title6', 'title5', 'title4', 'title3', 'title2', 'title1'];
            var attribute_name;
            var relevant_content = '';
            for ( var index in attributes ) {
                attribute_name = attributes[ index ];
                if ( data._highlightResult[ attribute_name ].matchedWords.length > 0 ) {
                    relevant_content = data._snippetResult[ attribute_name ].value;
                }
            }

            relevant_content = data._snippetResult[ attributes[ 0 ] ].value;
            #>
            {{{ relevant_content }}}
        </p>
    </div>
</div>

In doing so, none of my results are returned and I'm met with the following console error: Uncaught TypeError: Cannot read property 'matchedWords' of undefined

Any help would be greatly appreciated.

1 Answers1

1

Cannot read property 'matchedWords' of undefined tells me that your custom parameter for attributes may not be defined in the scope of Algolia.

I would suggest initializing attributesToIndex with your custom attributes (or any attributes you'll be using)- that way you can ensure that attributesToHighlight has definitions to work with.

Check out this answer~ it might help shed light on why you're getting an undefined definition for your custom attribute while trying to access the matchedWords member of your _highlightResult object.

Community
  • 1
  • 1
  • Thanks for your reply! I'm positive that the custom attribute is being indexed. I can see it in the Algolia search dashboard. Also, here is the output from the last API call which shows that 'program_description' has a snippet – Jim Kulakowski Oct 19 '16 at 16:58
  • @JimKulakowski - since the error is being thrown on a `matchedWords` attribute, it's clear that the problem lies within the definition of your `_highlightResult`- can you edit into your OP an output that shows the structure of the `_highlightResult` object? Much like the test query provided within the answer I linked. –  Oct 19 '16 at 17:10
  • Thanks again for the help. I'm not sure how to edit the output to include the _highlightResult object. However, I did change my $settings to this: $settings = array( 'attributesToIndex' => array( 'unordered(organisation_name)', 'unordered(program_description)' ), 'attributesToHighlight' => array( 'unordered(organisation_name)', 'unordered(program_description)' ) ); – Jim Kulakowski Oct 19 '16 at 17:28
  • I think I can also change those settings in the Algolia dashboard. I guess I'm just confused on how I can pull those into my search results given the template that provided with the Algolia search plugin for Wordpress. – Jim Kulakowski Oct 19 '16 at 17:30
  • @JimKulakowski are you using the [`setSettings`](https://www.algolia.com/doc/api-client/php/getting-started#quick-start) function? [This](https://www.algolia.com/doc/api-client/php/search#search-in-an-index) part of the documentation gives example code that shows `_highlightResult` being returned with a query. Sorry I can't really be of more help! –  Oct 19 '16 at 18:02
  • No Matt, I haven't messed with setSettings yet. I assume this is being called somewhere within the WP plugin. Thanks again for your help. I'm going to continue to fumble through the documentation. – Jim Kulakowski Oct 19 '16 at 18:08
  • I'm essentially following this documentation on the Algoli WP plugin site: [https://community.algolia.com/wordpress/customize-search-page.html] – Jim Kulakowski Oct 19 '16 at 18:10
  • @JimKulakowski Have you tried taking out the extraneous indices of the `attributes` array? The error might actually be thrown when it can't find a `content` or `title*` index. –  Oct 19 '16 at 18:35