Time for action - showing search form prefilled with last submitted data

  1. Add the following highlighted code in search.php:
    // Perform the search if we have a search term
    if (!empty($q)) {
    do_search($q, $author, $categories);
    }
    // Get all the categories and their ids
    // This will be used to build the categories filter drop down
    $query = "SELECT id, name FROM categories ORDER BY name";
    foreach ($dbh->query($query) as $row) {
    $viewVars['cat_list'][$row['id']] = $row['name'];
    }
    // Assign the search parameters to view variable
    $viewVars['q'] = $q;
    $viewVars['author'] = $author;
    $viewVars['categories'] = $categories;
    // Render the page
    render('search'),
    
    
  2. Modify search.thtml and change or add the following highlighted code:
    <div class="input">
    <label>Search for:</label>
    <input type="text"
    name="q" value="<?php echo $viewVars['q']; ?>" />
    </div>
    <div class="input">
    <label>Author:</label>
    <input type="text"
    name="author" value="<?php echo $viewVars['author']; ?>" />
    </div>
    <div class="input">
    <label>Category:</label>
    <select multiple="true" size="10" name="categories[]">
    <?php foreach ($viewVars['cat_list'] as $id => $name): ?>
    <?php
    $selected = '';
    if (in_array($id, $viewVars['categories'])) {
    $selected = ' selected';
    }
    ?>
    <option value="<?php echo $id; ?>"<?php echo $selected; ?>><?php echo $name; ?></option>
    <?php endforeach; ?>
    </select>
    </div>
    
  3. Reload the search page and try different search queries. Also, try entering author name and selecting one or more categories.

What just happened?

To give the finishing touches to our application we added code to show the form prefilled with the submitted parameters when search results are displayed.

The final search will look like the following screenshot:

What just happened?

We searched for the term facebook against the title and description of feed items. Additionally, we specified that search query should also match jason or leena against the author field. So, only those items that are authored either by jason or leena will be returned.

Next we filtered the items by Facebook category. So the final result will contain only those items which have Facebook category assigned to them.

Re-indexing

We now have the index ready with us, and we also have the frontend to perform the search. To fetch new feed items and index them we need to run the following commands at regular intervals:

$ /usr/local/sphinx/bin/indexer c /usr/local/sphinx/etc/feeds.conf feed-items-delta --rotate
$ /usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/feeds.conf --merge feed-items feed-items-delta --rotate

--rotate option is required as the index is already being served by searchd. This option creates a second index with the .new suffix, parallel to the first, and in the same place. Once the indexing is complete, indexer notifies the searchd and searchd attempts to rename the existing index to include .old suffix, and renames .new by removing .new to replace the existing index.

Have a go hero - trying different search queries

Try adding new feed URLs and re-indexing multiple times. Try different search queries. You can use all the operators mentioned in the Extended Query Syntax section of Chapter 4,Searching.

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

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