Support » Plugin: Relevanssi - A Better Search » API for my custom search?

  • Resolved nicmare

    (@nicmare)


    Hi There,
    i build my custom theme from scratch and also the search form. it appends an ajax suggestion before submitting the form to the search.php
    therefore i wonder if your plugin provides a simple api to catch the search terms to appear in your statistics?! any sample code please? thanks in advance

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

    (@msaari)

    Please elaborate. What exactly you want to capture? How are the ajax suggestions generated?

    If the search form does an ajax search request, you need to include the parameter relevanssi in the WP_Query and set it to true, and the search will be captured by Relevanssi, run and logged.

    Thread Starter nicmare

    (@nicmare)

    thanks for the quick response mikko! you can see it working live here: mybricks.net
    try to search for term “42000” or “technic” or “harry potter”. would be great to capture the successful responses.
    is there a code example somewhere for the param relevanassi in conjunction with wp query? sounds promising!

    Plugin Author Mikko Saari

    (@msaari)

    What’s the PHP code that runs your search?

    There’s no code examples, but it’s really simple. If you’re using WP_Query or get_posts() to fetch posts, you’re sending a bunch of parameters there, at least the s for search terms. Just set the relevanssi parameter to true:

    $args = array(
      's' => $search_terms,
      'posts_per_page' => 10,
      'post_status' => 'publish',
      'post_type' => array( 'post', 'page' ),
      'relevanssi' => true,
    );
    $query = new WP_Query( $args );

    Something like that.

    Thread Starter nicmare

    (@nicmare)

    alright. in my case i have a dedicated custom table to boost search because of all the meta fields and wp_query is to slow. this is my query for the auto suggestion:
    $results = $wpdb->get_results($wpdb->prepare('SELECT setid,'.$column.' FROM '.$wpdb->prefix.'sets WHERE setid LIKE %s ORDER BY id DESC LIMIT 8',"%".$setname."%"));

    any idea how to trigger relevanssi from there?

    Plugin Author Mikko Saari

    (@msaari)

    Well, if you’re just querying the meta data there, there’s no reason to involve Relevanssi in it – though Relevanssi can index the metadata and is probably quite a bit faster with it than WP_Query.

    But if you just want to have those queries added to the Relevanssi search logs, you can do

    relevanssi_update_log( $setname, count( $results ) );

    (The first parameter is the search query, the second parameter is the number of results found.)

    Thread Starter nicmare

    (@nicmare)

    i think that function is what i was looking for! many thanks! in the end i want to track what my visitors are searching for.

    FYI: i also discovered the hook “relevanssi_index_custom_fields” which is a MUST HAVE if you deal with a lot of posts and custom fields! i reduced wp_relevanassi table from over 100 MB down to 24 MB. i have over 4000 posts and each one round about 30 custom fields but most of them irrelevant for the visitor. therefore i excluded them from the index

    • This reply was modified 8 months ago by nicmare.
    • This reply was modified 8 months ago by nicmare.
    Plugin Author Mikko Saari

    (@msaari)

    You can also just set Relevanssi to index only the custom fields you want from the Relevanssi settings page 😉

    But the filter hook is good if you have a more complex case with different kinds of fields, some of which you want to index and some of which you don’t want.

    Thread Starter nicmare

    (@nicmare)

    FYI i integrated it now this way:

    if(function_exists("relevanssi_update_log"))
       relevanssi_update_log( $setname." (AJAX)", count( $results ) );
            
    wp_send_json_success($results);
    wp_die();

    then in the log it appears as:

    now i can clearly identify what is a classic post search on search.php or an ajax search from my code. i like.

    • This reply was modified 8 months ago by nicmare.
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘API for my custom search?’ is closed to new replies.