Tag Archive for 'categories'

12
Feb

sfLucene Quick Tip: Displaying Categories In Model Results

If any of you are using the great sfLucene Plugin, here’s a quick tip on making your search results a bit more intuitive.

This really only applies to model results (not action results) as there’s an option to add a category to each model. Here’s an example search.yml file that would go in your project’s config folder:

MyIndex:
  models:
    Podcast:
      fields:
        id: unindexed
        title:
          type: text
        description:
          type: text
      title: title
      description: description
      validator: isPublished
      categories: [Podcast]

You can see that I’ve added the category “Podcast” to the model’s definition. Now, you can override the plugin’s default display for model results by creating a module in your project named “sfLucene” and overriding the default _modelResult.php partial (in the sfLucene/templates directory) with something like this:

<?php echo link_to($result->getsfl_category() .' &raquo; '. highlight_keywords($result->getInternalTitle(), $query, sfConfig::get('app_lucene_result_highlighter', '<strong class="highlight">%s</strong>')), add_highlight_qs($result->getInternalUri(), $query)) ?> (<?php echo $result->getScore() ?>%)
<p><?php echo highlight_result_text($result->getInternalDescription(), $query, sfConfig::get('app_lucene_result_size', 200), sfConfig::get('app_lucene_result_highlighter', '<strong class="highlight">%s</strong>')) ?></p>

The only thing we’re really changing from the default template is that we’re adding this:

$result->getsfl_category() .' &raquo; '.

…to the beginning of the call to the link_to() helper. The category is stored automatically by sfLucene in this field in the lucene document, so we have access to it in the template. This new partial effectively inserts the category in front of the “title” field (along with a “»” for separation) while building the link, which produces something like this:

Podcast » My Great Podcast Title

As you can see, if you have several model definitions this comes in handy while looking at a list of results since you’d get a nice listing that shows which category each link belongs to:

Podcast » My Great Podcast Title

Blog » This Is Some Article

Podcast » Another Awesome Podcast

… instead of just a bunch of titles in a list.