• Resolved rockitdizajn

    (@rockitdizajn)


    Hello,

    I’m trying to set a fuzzy search, which would allow me to search multiple terms that are a part of the same product.

    The search should go trough product names and product SKU-s.

    Example – Searching for product:
    Title: “LED nadgradna lampa”
    SKU: “LN-02-5 BK”
    ***(note there are two dashes and one space in SKU – for some reason this gives me a false results)

    So if typed:

    “LN” [space/no space/dash] “02” [space/nospace/dash] “5” [space] “BK” [space] “mpa” – need to get the exact result without getting all results from all other products that could contain any of these separate parts.

    ***

    If i type the exact sku and part of the title, i will get the right result
    https://staging1.rasveta.net/?s=LN-02-5+BK+mpa&post_type=product
    https://staging1.rasveta.net/?s=LN+02-5+BK+mpa&post_type=product

    but if i type it without dashes (using spaces; or without spaces in SKU),
    or if i don’t put dash between “02” and “5” i will get “no results”
    https://staging1.rasveta.net/?s=LN025BK+mpa&post_type=product
    https://staging1.rasveta.net/?s=LN+02+5+BK+mpa&post_type=product

    This is the ISSUE that I can’t figure out

    ***

    or

    “LN”[space/no space/dash] “0” [space] “mpa” – need to get only the products that are containing all of these three terms. (this is working)

    ***

    I have already removed “Hyphens and dashes”,
    set “Minimum word length” to “1” (filter added)
    added “_sku, ” in “Custom fields”,
    added “products” in “Product types”,
    set “Default operator” to “AND”,
    disabled “Fallback to OR”,
    set “Keyword matching” to “Partial words”.

    I really hope that i have explained and described this well…
    Thanks in advance.

    Best regards,
    Rockit

    The page I need help with: [log in to see the link]

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

    (@msaari)

    The spaces are the biggest problem here. You’ve set Relevanssi to remove the hyphens, so searching for “LN-02-5 BK” and “LN025 BK” should return the same results, but the space inside the SKU is not removed, and must appear in the search terms.

    How easy it is to recognise a SKU on your site? If it’s easy, then the best solution would probably be to index the SKUs without any spaces and dashes, so “LN-02-5 BK” becomes “LN025BK”, and then do the same for search terms that look like SKUs. That way the searching would be most precise.

    Thread Starter rockitdizajn

    (@rockitdizajn)

    Hello Mikko,

    Thanks for your quick reply.

    How to index products without spaces and hyphens, and how to do that separately for search terms? I have create custom field “search_field” that contains full product name and sku, (exact copy of terms separated by space) and added it next to “_sku” in Relevanssi.

    *** Advanced indexing settings are all set to “Remove”.

    And just a small addition to previews post, not sure that matters:
    If typed “LN” [space] “02” [space] “5” (without [space]”BK”), still getting no results
    https://staging1.rasveta.net/?s=LN+02+5&post_type=product

    Thanks again,
    Rockit

    Plugin Author Mikko Saari

    (@msaari)

    You can do it like this:

    add_filter( 'relevanssi_content_to_index', 'rlv_spaceless_sku' );
    function rlv_spaceless_sku( $content, $post ) {
        $sku      = get_post_meta( $post->ID, '_sku', true ),
        $content .= ' ' . str_replace( ' ', '', $sku );
        return $content;
    }

    This will automatically index the SKU without spaces.

    Thread Starter rockitdizajn

    (@rockitdizajn)

    Hello once again,

    Code that you have send me solved the issue.
    Just wanted to thank you for your help and effort.

    Best regards,
    Rockit

    Thread Starter rockitdizajn

    (@rockitdizajn)

    Hello Mikko once again,

    I’m sorry for changing the post status to unresolved, but i have experienced even more “funny” results in search for the last few days.

    SKU that i’m trying to find is (ex.): “lp-301-18”

    So if i type: “lp 30”
    I should be getting all products with these two parts of SKU – it should be 9 results exactly, but i’m getting 29 results. All of those have “lp” in it, while 20 of these results does not have “30” in any part of the SKU.

    I have also figured out that one letter search is not working, i have set “Minimum word length” to 1, added “add_filter(‘relevanssi_block_one_letter_searches’, ‘__return_false’);” and tried to rebuild the index several times.

    At the end, I’ve used this custom solution that is working (almost, since i still have issues with my theme – Woodmart). I am wondering could this code be implemented with Relevanssi (maybe with this one or Premium solution)?

    One more question: Can you delete/hide links form my previous posts?

    Sorry about my English and thanks in advance.
    Best regards

    product_search_join($join)
    {
        if (is_admin() || !is_search() || !is_woocommerce()) {
            return $join;
        }
    
        global $wpdb;
    
        $join .= " LEFT JOIN {$wpdb->postmeta} meta ON {$wpdb->posts}.ID = meta.post_id ";
    
        return $join;
    }
    
    add_filter('posts_join', 'x_product_search_join');
    
    function x_product_search_where($where)
    {
        if (is_admin() || !is_search() || !is_woocommerce()) {
            return $where;
        }
    
        global $wpdb;
    
        if($_GET['s']) {
            $searchString = stripslashes( trim($_GET['s']) );
            $s = explode(' ', $searchString);
            $newWhere = '';
    
            foreach ($s as $item) {
                if (reset($s) === $item) {
                    $newWhere .= "
                        ({$wpdb->posts}.post_title LIKE '%{$item}%'
                        OR {$wpdb->posts}.post_content LIKE '%{$item}%'
                        OR (meta.meta_key = '_sku' AND meta.meta_value LIKE '%{$item}%'))
                    ";
                } else {
                    $newWhere .= "
    					AND ({$wpdb->posts}.post_title LIKE '%{$item}%'
    					OR {$wpdb->posts}.post_content LIKE '%{$item}%'
    					OR (meta.meta_key = '_sku' AND meta.meta_value LIKE '%{$item}%'))
    				";
                }
            }
    
            $where = preg_replace(
                "/\(\s*{$wpdb->posts}.post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
                $newWhere, $where);
        }
    
        return $where;
    }
    
    add_filter('posts_where', 'x_product_search_where');
    Plugin Author Mikko Saari

    (@msaari)

    I don’t have edit access to posts here, I can’t edit even my own posts after a while.

    Does searching for “lp30” work as expected? The problem here is that while you’re removing spaces from the SKUs when indexing, the spaces are not removed when searching. If you have OR as your default operator, searching for “lp 30” will match to all SKUs that begin with “lp”. Try switching to the “AND” operator, that may help you get stricter results.

    Relevanssi doesn’t use posts_where and posts_join, and the search queries are completely different, so no, those functions won’t work directly with Relevanssi.

    Thread Starter rockitdizajn

    (@rockitdizajn)

    Hello Mikko,

    Actually the default operator is “AND”, while “Disable the OR fallback” is also checked.

    That’s the main reason of my troubles ?? since, it should show exact results (containing all of the presented search terms) as i understand.

    Yes, “lp30” is showing the right results, but is showing only products that start with “lp-30x-xx.”, but if want to search for the product that starts with “lp” and contains “30” (ex. “lp-xxx-30”) as soon as i type “space” between the terms, the search shows unexpected results (or just showing the results that start with “lp”) – ex. “LPB-09-48R”.

    Like i said before, if this is something that could be solved through premium plugin, i’ll have no doubts to get it, since i really like your support efforts and the fact it would resolve the issue/question about this that i have.

    Best regards,
    Rockit

    Plugin Author Mikko Saari

    (@msaari)

    Just switching to Premium won’t help, because it uses exactly the same matching engine.

    What kind of results you get when you search for “30” alone? Can it be the “LPB-09-48R” post is found with “30”, because it has “30” somewhere in it that’s not the SKU?

    Thread Starter rockitdizajn

    (@rockitdizajn)

    Hey Mikko, thanks for the straight and honest answer, appreciate that very much. ??

    I have tried literally everything, ended up with updating and removing plugins, updating and removing theme, optimizing and “cleaning” database, clearing browser, website and hosting cache, switching through different combinations of Relevanssi filters. Really, never-ending weekend.

    I really didn’t know what more to do, so i switched everything back manually. And now… it works :). How and why? Don’t have any clue. Don’t getting anymore nonsense results.

    Two days ago, when i searched for number “30” one of many false results (700+) was the product with SKU: F7247-3V without “30” in product id, sku, name, attributes, price, categories, cat. ids, image title or name, url, quantities, height, width, length. Anywhere.

    Now i have this in functions.php (just leaving this if anybody ever needs it):

    /** Search **/
    
    add_filter( 'woocommerce_redirect_single_search_result', '__return_false' ); /** Disables single search result redirect **/
    
    add_filter( 'relevanssi_index_content', '__return_false' ); /** Disables content indexing **/
    
    add_filter('relevanssi_block_one_letter_searches', '__return_false'); /** Allows single-letter search **/
    
    /** Removes space form SKU **/
    add_filter( 'relevanssi_content_to_index', 'rlv_spaceless_sku' );
    function rlv_spaceless_sku( $content, $post ) {
        $sku      = get_post_meta( $post->ID, '_sku', true );
        $content .= ' ' . str_replace( ' ', '', $sku );
        return $content;
    }
    /** Removes hyphens **/
    add_filter('relevanssi_remove_punctuation', 'remove_hyphens', 9);
    function remove_hyphens($a) {
        $a = str_replace('-', '', $a);
        return $a;
    }
    /** Allows inside words search **/
    add_filter( 'relevanssi_fuzzy_query', 'rlv_partial_inside_words' );
    function rlv_partial_inside_words( $query ) {
    	return "(term LIKE '%#term#%')";
    }

    Still one letter search doesn’t work, so if you can help me with that?

    I would also need to have results when i search for ex. “LP 3” or “L 30”.
    As soon as i type one character separately, search shows no results at all.
    You can probably see that i have added – “add_filter(‘relevanssi_block_one_letter_searches’, ‘__return_false’);” and number “1” is set for Minimum word length in advanced options.

    Thanks again and best regards,
    Rockit

    Plugin Author Mikko Saari

    (@msaari)

    I noticed one problem here: since you’ve disabled content indexing, the relevanssi_content_to_index filter function is useless, because that filter hook is never run. So that explains something.

    This’ll work better:

    add_filter( 'relevanssi_custom_field_value', 'rlv_spaceless_sku', 10, 2 );
    function rlv_spaceless_sku( $values, $field ) {
    	if ( '_sku' === $field ) {
    		$new_values = array();
    		foreach ( $values as $value ) {
    			$new_values[] = str_replace( ' ', '', $value );
    		}
    		$values = array_merge( $values, $new_values );
    	}
    	return $values;
    }

    The single-letter searches don’t work, because Relevanssi blocks fuzzy matching for single-letter search terms. Otherwise most single-letter search terms would return every post in the database, which in general is a major performance and search accuracy problem.

    You can force it with this function:

    add_filter( 'relevanssi_query_filter', 'rlv_query_filter' );
    function rlv_query_filter( $query ) {
    	$query = preg_replace( "/relevanssi.term = '(.*?)'/", "relevanssi.term LIKE '%$1%'", $query );
    	return $query;
    }

    With this, you don’t need the rlv_partial_inside_words() function.

    Thread Starter rockitdizajn

    (@rockitdizajn)

    Hello Mikko,

    Everything is solved. Thank you very, very much.
    This is a great plugin and even better support.

    Best regards,
    Rockit

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Fuzzy Search by Title and SKU (dashes/spaces)’ is closed to new replies.