• I’m running both Relevanssi and Advanced Custom Fields Pro and I’m getting some skewed results. I have a page with a single instance of ‘Tom’ in the body which is returning a score of 72 body hits in the relevanssi results. I’ve checked the database table and I can see this page logged with a single (1) hit count in the body column.

    I’m pretty sure that the skew in results is linked to ACF as removing a single item from the ACF Repeater reduces the score by 4 in the results. The content in the custom fields does not contain anything that matches the search term.

    Any idea if this is a bug and if it can be addressed? I’ve tried disabling custom fields from being indexed but this doesn’t seem to have made any difference.

    Any help much appreciated.

    https://www.ads-software.com/plugins/relevanssi/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Mikko Saari

    (@msaari)

    What is a ACF Repeater? I don’t know. I have no experience of using ACF, so I can’t tell, but closer debugging might shed some light on the issue.

    Thread Starter tgedny

    (@tgedny)

    The ACF repeater is part of the Advanced Custom Fields plugin:

    https://www.advancedcustomfields.com/add-ons/repeater-field/

    It appears that Relevanssi hit count is being affected by the number of rows in the repeater field. I’ve tried for other search terms on the same page and they also seem to be affected in the same way.

    Plugin Author Mikko Saari

    (@msaari)

    Since the repeater is a premium feature, I have no way to test it. I really have no idea how it could be interfering in the first place. The only thing that should affect the number of body hits is the number of hits in the body…

    Do you have custom code in place in order to index the repeater fields? I don’t think Relevanssi can even index those fields without additional code. If you have some custom code, can I see it?

    Thread Starter tgedny

    (@tgedny)

    I’ve not used any custom code to index those fields. The wp_relevanssi database row for the problematic page with the search term shows the following:

    term: tom
    term_reverse: mot
    content: 1
    title: 0
    comment: 0
    tag: 0
    link: 0
    author: 0
    category: 0
    excerpt: 0
    taxonomy: 0
    customfield: 0
    mysqlcolumn: 0
    taxonomy_detail: null
    customfield_detail: null
    mysqlcolumn_detail: null
    type: post
    item: 0

    I have custom field indexing set to ‘visible’ but shouldn’t I expect to see this reflected in the above database row? In the results this returns as:

    (Search hits: 72 in body, 0 in title, 0 in tags, 0 in comments. Score: 3287.27)

    Switching off custom field indexing seems to give the correct result. This then returns:

    (Search hits: 1 in body, 0 in title, 0 in tags, 0 in comments. Score: 45.63)

    I’ll do some further investigation and see if I can find any way to replicate this without using premium features of the ACF plugin.

    Thread Starter tgedny

    (@tgedny)

    I’ve done some more investigation and I think this is actually related to pre_get_posts action I’ve got in my functions.php file. We filter content based on custom field for location so in pre_get_posts we have the following logic:

    $locationFilterMetaQueryArray = array(
    ‘relation’ => ‘OR’,
    array(
    ‘key’ => ‘location_filter’,
    ‘compare’ => ‘NOT EXISTS’
    ),
    array(
    ‘key’ => ‘location_filter’,
    ‘value’ => ”,
    ‘compare’ => ‘=’
    ),
    array(
    ‘key’ => ‘location_filter’,
    ‘value’ => $selected_location,
    ‘compare’ => ‘LIKE’
    )
    );

    $query->set(‘meta_query’, $locationFilterMetaQueryArray);

    Commenting out this code returns the results as expected. Any ideas? I’m pretty sure the skew in the results is still linked to the ACF plugin in some way but this code seems to be instrumental in the error in calculating the body score.

    Plugin Author Mikko Saari

    (@msaari)

    Hmm… Relevanssi sees the meta_query and acts upon it, but I don’t know how it would manifest as the increased hits count.

    I’d debug the main matches loop in Relevanssi. It’s in lib/search.php, this loop:

    foreach ($matches as $match) {

    There’s this line that counts the body matches:

    isset($body_matches[$match->doc]) ? $body_matches[$match->doc] += $match->content : $body_matches[$match->doc] = $match->content;

    Does that trigger in some wrong way?

    Thread Starter tgedny

    (@tgedny)

    I think I’ve concluded that the issue is linked to that meta query in some way but what am I looking for in the ‘matches’ loop – should there be a single match for the page in the loop if it only contains 1 instance of the search term? In my initial tests I’ve got a single term on the page but 115 instances of that page in $matches.

    Heres the query in case that helps:

    SELECT relevanssi.*, relevanssi.title * 5 + relevanssi.content + relevanssi.comment * 0.75 + relevanssi.tag * 0.75 + relevanssi.link * 0 + relevanssi.author + relevanssi.category * 0.75 + relevanssi.excerpt + relevanssi.taxonomy + relevanssi.customfield + relevanssi.mysqlcolumn AS tf
    FROM wp_relevanssi AS relevanssi
    LEFT JOIN wp_postmeta
    ON (relevanssi.doc = wp_postmeta.post_id
    AND wp_postmeta.meta_key = ‘location_filter’ )
    LEFT JOIN wp_postmeta AS mt1
    ON ( relevanssi.doc = mt1.post_id )
    WHERE relevanssi.term = ‘tom’
    AND ( wp_postmeta.post_id IS NULL
    OR ( mt1.meta_key = ‘location_filter’
    AND CAST(mt1.meta_value AS CHAR) = ” )
    OR ( mt1.meta_key = ‘location_filter’
    AND CAST(mt1.meta_value AS CHAR) LIKE ‘%159%’ ) )
    AND ((relevanssi.doc IN (SELECT DISTINCT(posts.ID)
    FROM wp_posts AS posts
    WHERE posts.post_type IN (‘post’, ‘page’, ‘attachment’, ‘new_releases’, ‘blog’, ‘classified’, ‘featured-publication’, ‘resource’, ‘event’, ‘office_location’)))
    OR (doc = -1))

    Plugin Author Mikko Saari

    (@msaari)

    Well, start with dumping out the $matches array. It should contain every wp_relevanssi database row that matches the search term, but only one per document.

    All those LEFT JOINs are probably the problem here – I think they’re adding rows that shouldn’t be added.

    One solution would be to get rid of the meta_query altogether, and instead do the location filter after the search in a relevanssi_hits_filter filter function.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Advanced Custom Fields Pro skews body hit count’ is closed to new replies.