Forum Replies Created

Viewing 15 replies - 1 through 15 (of 41 total)
  • To highlight entered characters, you can use:

    $hint .= preg_replace('~'.$q.'()~', '<strong>'.$q.'</strong>', $name);

    instead of

    $hint .= $name;

    You can do it with ajax.

    https://www.w3schools.com/php/php_ajax_php.asp

    You can create this gethint.php

    /**
     *  WordPress initializing
     */
    function find_wordpress_base_path() {
      $dir = dirname(__FILE__);
      do {
          //it is possible to check for other files here
          if( file_exists($dir."/wp-config.php") ) {
              return $dir;
          }
      } while( $dir = realpath("$dir/..") );
      return null;
    }
    
    define( 'BASE_PATH', find_wordpress_base_path()."/" );
    define('WP_USE_THEMES', false);
    global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
    require(BASE_PATH . 'wp-load.php');

    And then call popular searches

    
        global $wpdb, $relevanssi_variables;
        $table            = $relevanssi_variables['log_table'];
        $popular_searches = $wpdb->get_col( "SELECT query FROM $table GROUP BY query ORDER BY COUNT(*) DESC LIMIT 5" );
    foreach ( $popular_searches as $popular_search ) {
    
        $a[] = "$popular_search";
      
    }
    $q = $_REQUEST["q"];
    
    $hint = "";
    
    // lookup all hints from array if $q is different from ""
    if ($q !== "") {
      $q = strtolower($q);
      $len=strlen($q);
      foreach($a as $name) {
        if (stristr($q, substr($name, 0, $len))) {
          
             $hint .=  $name;
      
          }
        }
      }
    }
    
    // Output 
    echo $hint;

    Javascript

    function showHint(str) {
      if (str.length == 0) {
        document.getElementById("txtHint").innerHTML = "";
        return;
      } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
          if (this.readyState == 4 && this.status == 200) {
            document.getElementById("txtHint").innerHTML = this.responseText;
          }
        };
        xmlhttp.open("GET", "gethint.php?q=" + str, true);
        xmlhttp.send();
      }
    }

    Html

    <form action="/" name="s" method="get">
    <input id="search" name="s" onkeyup="showHint(this.value)" placeholder="Search" type="text">                        
    </form>
    <p>Suggestions: <span id="txtHint"></span></p>
    • This reply was modified 2 years, 2 months ago by Ahmet.
    • This reply was modified 2 years, 2 months ago by Ahmet.
    • This reply was modified 2 years, 2 months ago by Ahmet.
    Thread Starter Ahmet

    (@ahmetp83)

    Hi,

    For those who do not want to use instant or autocomplete search.

    When you enter the keyword and hit enter, posts that are found will be listed in the search results. (site.com/?s=keyword)

    We will benefit from typo tolerance, highlighting, and speed.

    Just like, when you search a keyword here, and then it goes to results and listing posts.

    In Algolia, we have this option and no need for javascript.

    Thread Starter Ahmet

    (@ahmetp83)

    Thank you so much! I really appreciate your help.

    And also need to add 'operator' => 'AND',

    Here is the full code for the product tag:

    add_filter( 'relevanssi_modify_wp_query', 'rlv_include_product_cat' );
    function rlv_include_product_cat( $query ) {
      if ( isset( $query->query_vars['product_tag'] ) ) {
    	$query->query_vars['tax_query'][] = array(
             'taxonomy' => 'product_tag',
    	    'field'    => 'slug',
    	    'terms'    => explode( '+', $query->query_vars['product_tag'] ),
    	    'include_children' => false,
            'operator' => 'AND',
               );
    	}
          return $query;
    }
    Thread Starter Ahmet

    (@ahmetp83)

    Hi Mikko,

    Yes, when deactivating the woocommerce, it filters as tag1+tag2. (posts)

    But I need to use woocommerce for my products.

    Thread Starter Ahmet

    (@ahmetp83)

    Hi Mikko,

    Thank you very much.

    I found a filter on your website and added it to functions.php

    add_filter( 'relevanssi_modify_wp_query', 'rlv_include_product_cat' );
    function rlv_include_product_cat( $query ) {
      if ( isset( $query->query_vars['product_tag'] ) ) {
    	$query->query_vars['tax_query'][] = array(
                'taxonomy' => 'product_tag',
    	    'field'    => 'slug',
    	    'terms'    => $query->query_vars['product_tag'],
    	    'include_children' => false,
               );
    	}
          return $query;
    }

    Now this works:

    URL/?s=keyword – found 10 posts
    URL/shop/tag/book/?s=keyword – found 6 posts

    However, this does not work
    URL/shop/tag/book+car/?s=keyword – found 10 posts (without the plugin found 3 posts)

    I tried ‘operator’ => ‘AND’, but did not help.

    Thread Starter Ahmet

    (@ahmetp83)

    Okay, this works:

    URL/?s=keyword&post_type=product&product_cat=ID

    This also works

    URL/?s=keyword&post_type=product&product_tag=ID

    But this does not work:

    URL/?s=keyword&post_type=product&product_cat=ID1+ID2 for example product_cat=5+10

    Changing the operator OR to AND did not help.

    But when the plugin deactivates, this simple URL just works:

    URL/shop/tag/ID1+ID2/?s=keyword

    • This reply was modified 2 years, 2 months ago by Ahmet.
    Thread Starter Ahmet

    (@ahmetp83)

    Thank you for your attention. I hope you guys will solve this.

    I noted the changes, so I can make it again after the update if this problem exists. ??

    Thread Starter Ahmet

    (@ahmetp83)

    Thank you for the answer.

    Okay, I will try with a clean install.

    Definitely the best filter plugin by the way.

    Thread Starter Ahmet

    (@ahmetp83)

    I found a solution to this.

    The plugin uses wp_searchable_posts index for the backend.

    In includes/class-algolia-settings.php there is a function.

    public function get_native_search_index_id() {
    		return (string) apply_filters( 'algolia_native_search_index_id', 'searchable_posts' );
    	}

    We create another function under it.

    public function get_native_search_index_id2() {
    		return (string) apply_filters( 'algolia_native_search_index_id', 'posts_product' );
    	}

    In /includes/class-algolia-plugin.php, we change this:

    $index_id = $this->settings->get_native_search_index_id();
    		$index    = $this->get_index( $index_id );
    
    		if ( null === $index ) {
    			return;
    		}
    
    		new Algolia_Search( $index );
    	}

    to this:

    $index_id = $this->settings->get_native_search_index_id();
    		$index    = $this->get_index( $index_id );
    
    		if ( null === $index ) {
    			return;
    		}
    
    		$index_id2 = $this->settings->get_native_search_index_id2();
    		$index2    = $this->get_index( $index_id2 );
    
    		if ( null === $index2 ) {
    			return;
    		}
    
    		new Algolia_Search($index, $index2 );
    	}

    Now the index is all post types and index2 is only product post.

    In includes/class-algolia-search.php

    We change this

    try {
    			$results = $this->index->search( $query->query['s'], $params, $order_by, $order );
    		} catch ( AlgoliaException $exception ) {
    			error_log( $exception->getMessage() ); // phpcs:ignore -- Legacy.
    
    			return;
    		}

    to this

    
    global $wp_query;   
    $post_type = get_query_var('post_type');   
    if( $wp_query->is_search && $post_type == 'product' )   
    		{   
    			
    		try {
    			$results = $this->index2->search( $query->query['s'], $params, $order_by, $order );
    		} catch ( AlgoliaException $exception ) {
    			error_log( $exception->getMessage() ); // phpcs:ignore -- Legacy.
    
    			return;
    		}
    	}
    	else {
    		try {
    			$results = $this->index->search( $query->query['s'], $params, $order_by, $order );
    		} catch ( AlgoliaException $exception ) {
    			error_log( $exception->getMessage() ); // phpcs:ignore -- Legacy.
    
    			return;
    		}
    
    }

    Now, when you search normally, the results are coming from wp_searchable_posts index. But when you filter the results with post type and post type is the product, the results are coming from wp_posts_product which is only product items are indexed.

    • This reply was modified 2 years, 3 months ago by Ahmet.
    • This reply was modified 2 years, 3 months ago by Ahmet.
    • This reply was modified 2 years, 3 months ago by Ahmet.
    • This reply was modified 2 years, 3 months ago by Ahmet.
    • This reply was modified 2 years, 3 months ago by Ahmet.
    • This reply was modified 2 years, 3 months ago by Ahmet.
    Thread Starter Ahmet

    (@ahmetp83)

    I made a further investigation and posted here what I found.

    Thread Starter Ahmet

    (@ahmetp83)

    // Store the total number of its, so that we can hook into the found_posts.
    // This is useful for pagination.
    $this->total_hits = $results['nbHits'];

    $results[‘nbHits’] outputs the total number of search items.

    If there are 5 posts + 2 products, it prints 7.

    If we can filter this according to post_type, it will solve the problem I think.

    • This reply was modified 2 years, 3 months ago by Ahmet.
    Thread Starter Ahmet

    (@ahmetp83)

    Thank you for the answer. I tried with different WordPress sites and the results are the same.

    This person has the same problem: https://github.com/WebDevStudios/wp-search-with-algolia/issues/225

    https://superstate.no/?s=lmnt (10 pages, each page has 9 posts)

    https://superstate.no/?s=lmnt&post_type=post (fewer posts are found but still 10 pages. Each page has 1 or 2 posts)

    Because it counts all founded posts. Must filter post_type.

    Thread Starter Ahmet

    (@ahmetp83)

    I thought it is a good solution, but deleting that add_filter causes another problem.

    If the maximum number is 5 (in the reading settings) but there are 8 posts found, it only shows 5 posts. And the output of count is “5 results found”

    It does not show the pagination but you can manually go to the URL /page/2?s=keyword

    So on the second page, you see 2 more posts, and it says “2 results found”

    TL;DR: Deleting add_filter helped to show the correct total number of results when filtering by post_type, but now results cannot exceed the max. number in 1 page.

    Thread Starter Ahmet

    (@ahmetp83)

    I found the problem with the total count. (I think now pagination also works as expected because the total count is now correct.)

    includes/class-algolia-search.php line: 202 (This hook returns the actual real number of results available in Algolia)

    Removing the found_posts function, solved the problem.

    If is there a better solution than deleting codes, I would like to hear. (remove_filter(‘found_posts’, ‘found_posts’ ); does not work.)

    Thank you for the plugin.

    • This reply was modified 2 years, 3 months ago by Ahmet.
    • This reply was modified 2 years, 3 months ago by Ahmet.
    • This reply was modified 2 years, 3 months ago by Ahmet.
Viewing 15 replies - 1 through 15 (of 41 total)