(Tutorial) Understanding the ApacheSolr CCK API

Tutorial : Understanding the ApacheSolr CCK API

In this article I will show you how you can write a tiny bit of code that will reveal new fields and facets for searching with the ApacheSolr module and Acquia Search. Using Acquia Drupal we'll write an example module that takes the file type from CCK file and image fields and makes them into their own search fields. This results in us being able to filter our search results based on file type. This code fulfils the situation where you want, for example, to find a specific post that has a JPEG image, or all of the posts with PDFs that match a particular keyword.

To start you may want to download the PDF file of screenshots that trace all of the steps I took to set up Acquia Drupal, Acquia Search, and the custom module. The broad steps are to:

1. Sign up for a free trial.
2. Download and install Acquia Drupal.
3. Follow along with the code examples below to create the example.module.
4. Set up a content type to have image and file fields. Create some content and upload a variety of files.
5. Run cron to make sure your content has been indexed.
6. Enable the new filters and blocks that the example.module is responsible for having created.
7. Search!

What are facets?

The ApacheSolr module is a revolution in Drupal search. It allows you to search for the most general keyword that applies to what you're looking for, and then use the provided facet links to drill down to exactly the right content. An example would be searching for "Drupal search" on Drupal.org, then filtering by project and robertDouglass to get just the modules that I have written that deal with search. Facets, in other words, are the better version of advanced search forms, which, to be honest, suck.

What facets are available?

By default, ApacheSolr makes facets available for content type, author, language, taxonomy terms, and all CCK fields that are text fields with option widgets (select, radio, checkbox). This article assumes that you want some different facets, and you're using CCK fields. The file field and image field both have some interesting information that would make a great facet - their file type. Every file you upload has a distinct type: pdf, png, gif, tiff, doc, and so forth. Wouldn't it be nice to have this available as a facet? Of course!
What needs to happen to add new facets?

The ApacheSolr module comes equipped with an API for extending what gets indexed and how searching works. One of the important hooks in this API is hook_apachesolr_cck_field_mappings(). We're going to write a module that implements this hook, and use it to tell ApacheSolr how to make facets out of the file type on file and image fields.

To do this the hook is only going to have to tell ApacheSolr three things:
1. What data type should be used in the index.
2. What CCK widget types to be looking for during indexing.
3. A callback function to use for extracting the data from the CCK field. We write this function ourselves.

| Read more..

Courtesy: Acquia.Com