Time for action - adding code to perform full-text search

  1. Start the searchd daemon (as root):
    $ /usr/local/sphinx/bin/searchd c /usr/local/sphinx/etc/properties.conf
    
    Time for action - adding code to perform full-text search
  2. Modify index.php and add the following (highlighted) code:
    <?php
    /**
    * File: /path/to/webroot/properties/index.php
    */
    include('init.php'),
    // Get the list of cities
    $query = "SELECT id, name FROM cities";
    foreach ($dbh->query($query) as $row) {
    $viewVars['cities'][$row['id']] = $row['name'];
    }
    $q = !empty($_POST['q']) ? $_POST['q'] : '';
    $city = !empty($_POST['city_id']) ? $_POST['city_id'] : '';
    // If we have the search term
    if (!empty($q)) {
    // Include the api class
    require_once('sphinxapi.php'),
    $client = new SphinxClient();
    // Set search options
    $client->SetServer('localhost', 9312);
    $client->SetConnectTimeout(1);
    $client->SetArrayResult(true);
    // Set the mode to SPH_MATCH_ANY
    $client->SetMatchMode(SPH_MATCH_ANY);
    // Weights for each field
    $weights = array(
    'description' => 1,
    'address' => 10,
    'zip_code' => 50,
    );
    $client->SetFieldWeights($weights);
    if (!empty($city)) {
    $client->SetFilter('city_id', array($city));
    }
    $viewVars['results'] = $client->Query($q);
    }
    $viewVars['q'] = $q;
    $viewVars['city_id'] = $city;
    render('index'),
    
  3. Modify the index.thtml view and add code to display the results:
    <!-- File: /path/to/webroot/properties/views/index.thtml -->
    <div class="input">
    <label>&nbsp;</label>
    <input type="submit" name="submit" value="Search" />
    </div>
    </fieldset>
    </form>
    <div class="results">
    <?php if (isset($viewVars['results']) && empty($viewVars['results']['matches'])): ?>
    <div class="information">No matching properties found!!!</div>
    <?php endif; ?>
    <?php
    if (!empty($viewVars['results']['matches'])) {
    print '<div class="information">Total '.$viewVars['results']['total_found'].' properties found</div>';
    print '<ul>';
    foreach ($viewVars['results']['matches'] as $match) {
    print '<li><a href="view.php?id=' . $match['id']
    . '">Listing #' . $match['id'] .', '
    . $match['attrs']['bedrooms'] . ' Bedrooms '
    . $match['attrs']['area']. 'sq feet, $'
    . $match['attrs']['price']. '</a></li>';
    }
    property search applicationproperty search applicationfull-text search performing, by adding codeprint '</ul>';
    }
    ?>
    </div>
    
  4. Reload the index.php page in your browser, select any city and search for a term (this depends on the data you provided while adding properties).
    Time for action - adding code to perform full-text search

What just happened?

Firstly, we started the searchd daemon. If the daemon is already serving another configuration file, then you first need to stop the daemon and then start it for this configuration file. If you try to start two instances of searchd, with the same port assigned to the listen option, then you will get an error as shown next:

FATAL: failed to lock pid file '/usr/local/sphinx/var/log/searchd.pid': Resource temporarily unavailable (searchd already running?)

We then modified the index.php file and added code to handle the search form submission. We used SPH_MATCH_ANY matching mode and set weights for each individual fields. We assigned the following weights to the fields:

  • description—1
  • address—10
  • zip_code—50

We assigned the least weight to description and the highest weight to the zip_code field. This was done so that if a keyword matches the zip_code of a document, then that document gets the highest rank, and thus, the zip_code field is assigned the maximum weight. Then comes address and description in that order. We chose the weights so that there is maximum difference between the weights of description and zip_code. The higher the difference, the higher the weightage.

We used the SetFieldWeights()API method, which binds per-field weights by name. The parameter passed to this method is an associative array, mapping string field names to integer weights.

The default value for weight given to each field is 1 and the specified weights must be a positive 32 bit integer.

After adding the search logic, we modified the view to add HTML code in order to display the results.

Have a go hero - try setting different field weights

In add.php, try your hand by setting different weights for each field. See how it affects the ranking of results.

Also try other matching modes and see which mode works best for you. Of course, you would first need to add quite a few properties to make the search worthwhile.

Advanced search

Now, let's jump on to a comprehensive search form which will essentially filter search results based on all the attributes; such as city, price, number of bedrooms, area, and so on.

We will build the advanced search form step by step, that is, we will add one filter at a time.

..................Content has been hidden....................

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