• Resolved carst

    (@carst)


    Our case for using Relevanssi is to enable users getting the same results, whether they type a word with or without a hyphen. For instance: e-learning or elearning.

    However after plugin installation and minimal configuration I see a fatal error while searching for a term like this (/?s=e-learning):

    Uncaught Error: Argument 1 passed to relevanssi_mb_strcasecmp() must be of the type string, null given, called in /public/app/plugins/relevanssi/lib/sorting.php on line 268
    in /public/app/plugins/relevanssi/lib/utils.php on line 517

    I changed these settings to see if they would solve this, but they did not. We are using Woocommerce.

    * Advanced indexing settings > Hyphens and dashes: Remove
    * Shortcodes > Expand shortcodes: disable

    How can I solve this?

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

    (@msaari)

    For a quick fix, change this line in /lib/utils.php:

    function relevanssi_mb_strcasecmp( string $str1, string $str2, string $encoding = '' ) {

    Change it to

    function relevanssi_mb_strcasecmp( $str1, $str2, string $encoding = '' ) {

    That should make things work. This will be fixed in the next version, out soon.

    Plugin Author Mikko Saari

    (@msaari)

    Though it’s quite unusual that this error would happen in the first place, and even with the fix this may lead to strange results. It’d be great if you could explain how I would go about reproducing this problem.

    What sorting is your search using?

    Thread Starter carst

    (@carst)

    Thanks for the fix. The sorting is the default setting (Relevance).

    The problem happens whenever I search for a term with a hyphen that is within the index. These queries do not show up in the Search log. Any search for a term with a hyphen that is not within the index goes through successfully.

    Plugin Author Mikko Saari

    (@msaari)

    The thing is, the error is caused in a sorting routine where strings are compared. That’s something that shouldn’t ever happen in relevance-sorted search results. There must be something unusual going on in the sorting.

    Well, the next version should still fix this, but I can’t guarantee that something funny isn’t still going on.

    Thread Starter carst

    (@carst)

    Yes, it sounds unusual. I’m wondering if there is a pre_get_posts hook that might cause a problem. I will check this.

    Locally (with PHP warnings on) I still see these messages:

    Notice: Undefined index: key1 in /public/app/plugins/relevanssi/lib/sorting.php on line 360
    Notice: Undefined index: key2 in /public/app/plugins/relevanssi/lib/sorting.php on line 360
    • This reply was modified 4 years ago by carst.
    Plugin Author Mikko Saari

    (@msaari)

    The actual problem is in the relevanssi_get_compare_values() function. It should create those keys, but isn’t for some reason. If you can dig into why that is so, that would be very helpful. What is the value of $key that function is getting?

    Thread Starter carst

    (@carst)

    I have found the culprit in the theme that causes this. It is indeed a pre_get_posts hook. Strangely enough it is called through ‘add_filter’ and not through ‘add_action’ like it should be.

    The function modifies the meta_query by changing order and order_by parameters. However it looks like this achieves something which we can now add through Relevanssi Premium.

    function product_search_results_first($query) {
    	if (is_admin() || !$query->is_search()) {
    		return $query;
    	}
    
    	$query->set('meta_query', [
    		'relation' => 'OR',
    		[
    			'key'     => '_wi_course_education_type',
    			'value'   => 'opleiding',
    			'compare' => 'LIKE',
    		],
    		[
    			'key'     => '_wi_course_education_type',
    			'value'   => 'e-learning',
    			'compare' => 'LIKE',
    		],
    		[
    			'key'     => '_wi_course_education_type',
    			'value'   => 'opleiding',
    			'compare' => 'NOT EXISTS',
    		],
    	]);
    	$query->set('orderby', 'meta_value');
    	$query->set('order', 'DESC');
    
    	return $query;
    }
    Plugin Author Mikko Saari

    (@msaari)

    add_action() is actually just an alias for add_filter(), they’re exactly the same. Actions and filters are very much the same thing beneath the surface.

    I’m not sure how that function of yours is supposed to work – when you use meta_value as an orderby parameter, there should be a meta_key parameter as well to tell which meta field is used for sorting. I guess that’s what is confusing Relevanssi here.

    What is the meta field the sorting is supposed to use here?

    If you just want products first, there are simpler methods for that.

    Thread Starter carst

    (@carst)

    I agree. The syntax is off. Normally you either use a meta_query, or you use a meta_value, not both. I will check with my co-worker who wrote this. Thanks for you support, it looks like a ‘problem solved’ from my end.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Searching for Words with a Hyphen generates a Fatal error’ is closed to new replies.