Writing your own Search::GIN extractor

I stumbled into a tricky situation with Search::GIN that required me to have a reverse indexing with set introspection. This situation isn't so rare, so I thought it would be helpful to share how I did it.

Suppose I have an object. The object has an attribute. The attribute is a set of objects. I need to be able to fetch the objects according to the value of an attribute of the objects in the set in the attribute of the original object. Err.. ya know what? Here's an example!

I have a few music preferences (blues, jazz, etc.), a few simpsons characters (Lisa, Homer, Barney). Each of them likes certain types of music. Lisa likes blues and jazz, while Barney only likes blues and Homer just likes stupid commercial jingles.

First define the objects:

The create them:

I want to be able to fetch all the people who like a certain type of music. This is a bit difficult since a person likes a set of music styles. Search::GIN::Extract::Attributes doesn't cover sets (though it would be cool if it could in the future) and I obviously don't want to go over all characters and do it.

Instead I wrote an extractor that does that:

In order to use this extractor, in my DB interface (which extends KiokuX::Model) I do this:

And viola!

Now using Search::GIN::Query::Manual, I can search for any Character that likes Blues music:

I wanted to write all the code in this post but for some reason I can't put more than a few lines of code here and it always double spaces it. How do others put code here?

Leave a comment

About Sawyer X

user-pic Gots to do the bloggingz