Forum Replies Created

Viewing 14 replies - 1 through 14 (of 14 total)
  • Btw, when using a self hosted typesense server, the collections that are being created should be prefixed by the sitename.

    Something like example.com.post, example.com.product etc.

    To make the self hosted server work, i used a reverse proxy on my domain. When the TYPESENSE-API-KEY is present, the requests are redirected to typesense.

    RewriteEngine On
    RewriteCond %{HTTP:X-TYPESENSE-API-KEY} !^$ [OR]
    RewriteCond %{QUERY_STRING} (^|&)x-typesense-api-key=.+(&|$)
    RewriteRule ^(.*)$ https://127.0.0.1:8108/$1 [P,L]
    
    Header set Access-Control-Allow-Origin "*"
    • This reply was modified 2 years, 4 months ago by sannin. Reason: Improve code to include query parameters
    Thread Starter sannin

    (@sannin)

    Nice, i already have the addon, but i thought that i would be done in the core plugin first because the code would be common.

    That works only for instant search and not for autocomplete right?

    Thread Starter sannin

    (@sannin)

    So the js file of woocommerce addons just adds the instant search functionality?

    If i just use the autocomplete, can i safely remove it?

    Thread Starter sannin

    (@sannin)

    Hello,

    Do you have any update on that? It would be great to have some more control on how typesense behaves. For example in a woocommerce shop:

    – query_by_weights:

    Fields that contain SKU or EAN information should have a greater weight than the rest of them.

    – sort_by:

    I could affect the result ordering maybe by sales or pageviews, and only by “_text_match:desc” that is by default.

    – prioritize_exact_match:

    The default value of true works for me but it would be nice to have control.

    – num_typos

    In certain fields like SKU and MPN it is unacceptable to have typo tolerance.

    Isn’t it possible to have a filter hook that gives access to all typesense parameters?

    Thread Starter sannin

    (@sannin)

    You could have told us, that you were planning a premium plugin, it could save me several hours of trying to do it by myself! ??

    For support issues / enhancements should i contact you directly? Because i think that some things can be done in the core plugin.

    • This reply was modified 2 years, 9 months ago by sannin. Reason: Solved
    Thread Starter sannin

    (@sannin)

    It works fine for me too now, maybe some caching issue, i am not sure!

    There are something i have in mind, i will study the typesense api some more and get back to you.

    Thread Starter sannin

    (@sannin)

    Thank you for this one, too!

    This is my custom code i confirm it works:

    function intercept_ts_template( $template, $file, $args ) {
    
            $template_directory = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'templates/typesense/';
            $path = $template_directory . $file;
    
            return file_exists( $path ) ? $path : $template;
    
    }
    add_filter( 'cm_typesense_locate_template', 'intercept_ts_template', 10, 3 );

    I don’t know if it will work for anyone i write all my custom code to a plugin i have made for that purpose.

    Thread Starter sannin

    (@sannin)

    It is the first time i have seen integration of a new feature within an hour of the request! I am speechless, you guys rock!

    I tried the query_by argument, it works with the default post collection but it doesn’t work with my custom product collection. When i use “query_by” with the product collection it does not fetch results any more.

    Thread Starter sannin

    (@sannin)

    I can confirm the patch has fixed the issue, thanks a lot!

    • This reply was modified 2 years, 10 months ago by sannin. Reason: Typo
    Thread Starter sannin

    (@sannin)

    Yes that is the problem, the shortcode exists but autocomplete does not. Caching should not be an issue for that page, i had disabled optimization for all typesense assets as well, but just in case i disabled it completely.

    You can see that all typesense js and css files load correctly and i dont have any console log errors.

    View post on imgur.com

    Thread Starter sannin

    (@sannin)

    I changed the field names of my code to be more compatible to the ones already used in the plugin. I got instant search working, but not autocomplete.

    Some documentation and more customization options on the templates would be nice!

    Thread Starter sannin

    (@sannin)

    The fields i use are customized to the custom fields and attributes in my shop. Maybe if you remove the relevant code for supplier, brand, ean and product_cost it could work.

    Thread Starter sannin

    (@sannin)

    Hey again,

    I found the problem, the schema in typesense was from an earlier attempt. I manually deleted the schema and reindexed the products. After some tries, i successfully indexed the products. I had some issues with field types, so i tried to use string on most of them. Any suggestions are welcome, this is my full code:

    
    /*** Adds the post type book under available post_types ***/
    function cm_typesense_add_available_post_types( $available_post_types ) {
        $available_post_types['product'] = [ 'label' => 'Products', 'value' => 'product' ];
    
        return $available_post_types;
    }
    add_filter( 'cm_typesense_available_post_types',  'cm_typesense_add_available_post_types');
    
    //only necessary if the default post schema is not necessary
    function cm_typesense_add_product_schema( $schema, $name ) {
        if ( $name == 'product' ) {
            $schema = [
                'name'                  => 'product',
                'fields'                => [
                    [ 'name' => 'description', 'type' => 'string' ],
                    [ 'name' => 'name', 'type' => 'string' ],
                    [ 'name' => 'short_description', 'type' => 'string' ],
                    [ 'name' => 'date_modified', 'type' => 'string' ],
                    [ 'name' => 'id', 'type' => 'int64' ],
                    [ 'name' => 'permalink', 'type' => 'string' ],
                    [ 'name' => 'image', 'type' => 'string' ],
    		[ 'name' => 'ean', 'type' => 'string' ],
    		[ 'name' => 'sku', 'type' => 'string' ],
    		[ 'name' => 'popularity', 'type' => 'int32' ],
                    [ 'name' => 'product_cost', 'type' => 'string' ],
    		[ 'name' => 'price', 'type' => 'string', 'facet' => true ],
                    [ 'name' => 'supplier', 'type' => 'string' ],
                    [ 'name' => 'categories', 'type' => 'string[]', 'facet' => true ],
                    [ 'name' => 'tags', 'type' => 'string[]', 'facet' => true ],
                    [ 'name' => 'brand', 'type' => 'string', 'facet' => true ],
                    //[ 'name' => 'stock_status', 'type' => 'string', 'facet' => true ],
                    [ 'name' => 'date_modified', 'type' => 'string' ],
                ],
                'default_sorting_field' => 'popularity',
            ];
        }
    
        return $schema;
    }
    add_filter( 'cm_typesense_schema', 'cm_typesense_add_product_schema', 10, 2 );
    
    //you can modify the code as needed 
    //if your schema is simillar to the default post type then you should only change what fields you need to add
    //example here we are only adding genre
    function cm_typesense_format_product_data ( $formatted_data, $raw_data, $object_id, $schema_name ) {
        if ( $schema_name == 'product' ) {
    
    	$product = wc_get_product( $object_id );
    
    	if ( ! $product ) {
    		return $formatted_data;
    	}
    
    	$formatted_data = array();
    
    	$tags  = get_the_terms( $object_id, 'product_tag' );
    	$product_tags = array();
    	foreach ( $tags as $tag ) {
    		$product_tags[] = $tag->name;
    	}
    
            $cats  = get_the_terms( $object_id, 'product_cat' );
            $product_cats = array();
            foreach ( $cats as $cat ) {
                    $product_cats[] = $cat->name;
            }
    
    	$formatted_data[ 'name' ] = $product->get_title();
            $formatted_data[ 'description' ] = $product->get_description();
            $formatted_data[ 'short_description' ] = $product->get_short_description();
            $formatted_data[ 'id' ] = (string) $object_id;
            $formatted_data[ 'permalink' ] = $product->get_permalink();
    	$formatted_data[ 'date_modified' ] = $product->get_stock_status();
            $formatted_data[ 'image' ] = wp_get_attachment_url( $product->get_image_id() );
            $formatted_data[ 'ean' ] = $product->get_meta('we_skroutzxml_ean_barcode');
            $formatted_data[ 'sku' ] = $product->get_sku();
            $formatted_data[ 'popularity' ] = (int) $product->get_meta('total_sales');
            $formatted_data[ 'price' ] = $product->get_price();
            $formatted_data[ 'product_cost' ] = $product->get_meta('_wc_cog_cost');
            $formatted_data[ 'categories' ] = $product_cats;
            $formatted_data[ 'tags' ] = $product_tags;
            $formatted_data[ 'supplier' ] = $product->get_attribute('pa_supplier');
            $formatted_data[ 'brand' ] = $product->get_attribute('pa_manufacturer');
            //$formatted_data[ 'stock_status' ] = $product->get_stock_status();
            $formatted_data[ 'date_modified' ] = (string) $product->get_date_modified();
        }
    
        return $formatted_data;
    }
    add_filter( 'cm_typesense_data_before_entry', 'cm_typesense_format_product_data', 10, 4 );
    
Viewing 14 replies - 1 through 14 (of 14 total)