Book Image

Sphinx Search Beginner's Guide

By : Abbas Ali
Book Image

Sphinx Search Beginner's Guide

By: Abbas Ali

Overview of this book

Table of Contents (15 chapters)
Sphinx Search
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface

Time for action - adding code to filter the results


  1. 1. Modify the function do_search() in search.php and add the following highlighted code:

    function do_search($q, $author, $categories)
    {
    global $dbh, $viewVars;
    // Include the api class
    require('sphinxapi.php');
    $client = new SphinxClient();
    // Set search options
    $client->SetServer('localhost', 9312);
    $client->SetConnectTimeout(1);
    $client->SetArrayResult(true);
    // Set the mode to SPH_MATCH_EXTENDED2
    $client->SetMatchMode(SPH_MATCH_EXTENDED2);
    // Match the search term against title and description
    $query = "@(title,description) ($q)";
    // If we have author then match it against author field
    if (!empty($author)) {
    $query .= "@author $author";
    }
    // If categories were selected then filter the results
    if (!empty($categories)) {
    $client->SetFilter('category_id', $categories);
    }
    // Fire the search query against feed-items index (main index)
    $viewVars['results'] = $client->Query($query, 'feed-items');
    $viewVars['items'] = array...