Time for action - filtering the result set

  1. Create a PHP script search_filter.php in your webroot containing the following code:
    <?php
    // Include the api class
    Require_once('sphinxapi.php'),
    // Include the file which contains the function to display results
    Require_once('display_results.php'),
    $client = new SphinxClient();
    $client->SetServer('localhost', 9312);
    $client->SetConnectTimeout(1);
    $client->SetArrayResult(true);
    $client->SetMatchMode(SPH_MATCH_ANY);
    // Returns all documents which match "programming games electronics"
    display_results(
    $client->Query('programming games electronics'),
    'all posts matching "programming games electronics"'),
    // Filter by ID
    $client->SetIDRange(1, 4);
    // Same as above but with ID based filtering
    display_results(
    $client->Query('programming games electronics'),
    'above query with ID based filtering'),
    // Reset the ID based filter
    $client->SetIDRange(0, 0);
    // Filter the posts by author's Aditya Mooley and Dr.Tarique Sani
    $client->SetFilter('author_id', array(2, 4));
    display_results(
    $client->Query('programming games electronics'),
    'posts filtered by author'),
    // Filter the posts by category Games
    $client->SetFilter('category_id', array(2));
    display_results(
    $client->Query('programming games electronics'),
    'posts filtered by categories'),
    // Filter the posts by publish_date using range filter
    $client->SetFilterRange(
    'publish_date',
    strtotime('2010-01-01'),
    strtotime('2010-01-30'));
    display_results(
    $client->Query('programming games electronics'),
    'posts filtered publish date range'),
    
  2. Run the script in a browser (the output is explained in the next section).

What just happened?

We used the Sphinx client API's filtering methods to filter our search results. In all our above queries we searched for the same set of terms, such as "programming games electronics", but with different filters.

The first search query returned all results without any filtering. Before issuing the second search query we used the SetIDRange($min, $max) method. This method filters out the results based on the minimum and maximum ID values passed to it. So in our case we only got those documents whose ID were in between one and four. The un-filtered and filtered results are as shown in the following screenshot:

What just happened?

After that, we reset our ID range filter by passing 0 as minimum and maximum values. We then filtered our search results by author. We filtered them so that we only get posts by Aditya Mooley (author_id 2) and Dr.Tarique Sani (author_id 4).

$client->SetFilter('author_id', array(2, 4));

The filter returns the following result:

What just happened?

SetFilter($attribute, $values, $exclude=false) takes three parameters. The first is the attribute on which the filtering should be done. The second is an array of integer values to be filtered, meaning documents matching any of these values will be returned. The third parameter is an optional Boolean parameter, and if passed as true will actually exclude the values passed, instead of including them.

Next we filtered results based on category_id, which is an MVA. Filtering on normal and MVA attributes works in a similar fashion, as far as calling the SetFilter() method is concerned. If the attribute is MVA, then it matches all those documents where any of the values stored in the MVA field matches any of the passed values. The filter returns the following result:

What just happened?

We previously searched for all posts that are in category 'Games' (category_id 2). Since category_id is an MVA, it holds multiple values, and if any of those values matches 2 then that document is returned.

Note

The filter set for author was not reset when we filtered by category, and hence both the filters were applied. The results were filtered by author as well as category. So our final result returned those posts whose author is either Aditya Mooley or Dr.Tarique Sani, and whose category is Games.

To filter the results based on a range of values we used the SetFilterRange($attribute, $min, $max, $exclude=false) method. All parameters are self explanatory. We filtered our search results so that we only get those posts that were published between 1st January 2010 and 30th January 2010.

$client->SetFilterRange('publish_date', strtotime('2010-01-01'), strtotime('2010-01-30'));

The ranged filter returned the following result:

What just happened?

There are more methods available to filter search results:

  • SetFilterFloatRange ( $attribute, $min, $max, $exclude=false )—Works similar to SetFilterRange() but for float range values
  • SetGeoAnchor ( $attrlat, $attrlong, $lat, $long )—Used for filtering based on geolocation (explained in later chapters)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset