Tag Archive for 'sflucene'

28
Feb

sfLucene Quick Tip 2: Automatic Re-indexing

Here’s another quick tip when using sfLucene. As you’ll notice when you start using the plugin, in order to index all of your results (after you’ve set up all of your search.yml files) you need to run a pake task to build the index:

symfony lucene-rebuild frontend dev

This will (re)build a search index for the “frontend” application using the “dev” environment. This is great, but this means that the search index is only up-to-date when you run this command. It would be a pain to have to do this manually whenever we wanted our search index updated, so we’ll use a cron job to automate the re-indexing.

Since I’m using a Media Temple Grid Server for this project, I’ll simply use their control panel to enable the cron job. Media Temple actually has a great KnowledgeBase Article on how to setup and configure a cron job through your grid server control panel, so you can see that article for more info on setting this up using a Grid Server.

For those of you using another service or your own brand of linux/unix, there are instructions all over the web that you can use to follow the same methodology, but basically all you have to do is run the same “lucene-rebuild” command on an interval of your choosing. You also have to use the full path to your symfony project’s root folder where the symfony command is located. For a Grid Server, this would be something like this:

php5 /home/XXXXX/domains/example.com/symfony lucene-rebuild frontend prod

…where XXXXX is your Grid Server site number and “example.com” is your domain name. Note the use of “php5” before the path of the actual command. This is required if you’re using a Grid Server as by default it will try to run this script using the php4 CLI. Also, your path my vary depending on how your files are setup in the domain folder itself. I set mine to index once a day, but if your site has a high data turnover rate, you may want to make it index more frequently so that results are fresh. You’ll also notice that I’m using the “prod” environment since that’s what the site is using in production.

Update 2008-03-04: Looks like I had propel.builder.addBehaviors set to false in my propel.ini so the behaviors weren’t being built into the model classes when I was rebuilding them. So new and modified objects are now correctly and automatically re-indexed. I still use this cron job though, albeit less frequently, to update the search index for static files that are being indexed through the Action indexer.

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.