I’ve spent a lot of time this weekend on added features to Pundit. Most noticeable is the Solr integration. For all you guys who don’t know what Solr is, its a search-thingy which is really fast and just utterly cool. Go read up on it at their site.
It only took 221 lines of code and its basically transparent to you, the developer. And that makes it a Good ThingTM.
1. Creating the model
You create your model pretty much the way you normally do it, except for three changes highlighted in bold:
<?php
class MySearchObject extends SolrModel {
public $search_id = PUNDIT_DTYPE_ID;
public $search_textfield = PUNDIT_DTYPE_LINE;
public $search_date = PUNDIT_DTYPE_DATE;
// Yes, I’ve just added the PUNDIT_DTYPE_DATE, and if you want to be
// able to do proper date-searches, use it.
public $someobjectid = PUNDIT_DTYPE_PUNDITMODEL;
// Yes, PUNDIT_DTYPE_PUNDITMODEL will work like it always did.
}
class MySearchObjectHandler extends SolrModelHandler {
protected $solrPath = ‘http://localhost:8080/solr/’;
public function __construct() {
parent::__construct(’MySearchObject’, null, ’search_id’);
// Okay, this isn’t strictly needed if you just use standard naming.
}
?>
Of course you will need to create a Solr schema.xml that fits your model. You can only have one data type per Solr-instance unless you create some middle-layer that will convert your results into a generic format, and then saves that. It would be possible, but its not per default built into Pundit.
2. Adding data
You can add data the way you normally add data. That is
<?php
// Load all things Pundit first, of course
$obj = Pundit::Handler('MySearchObject')->create();
$obj->setVar('search_id', 1, true); // You will need to force an import on this one to override the id variable. No biggie.
$obj->setVar('search_textfield', 'Some random value..');
$obj->setVar('search_date', strtotime('-30 minutes') ); // Just some timestamp.
$ob->setVar('someobjectid', 42);
$obj->save();
// This will add the object to Solrs queue. You might wanna do the following:
Pundit::Handler('MySearchObject')->commit();
Pundit::Handler('MySearchObject')->optimize();
?>
3. Searching your index
It should come to no surprise that you fetch data just the way you are used to.. You might want to use the following:
<?php
// Load all things Pundit first, of course
$one_object = Pundit::Handler('MySearchObject')->get(1);
$another_object = Pundit::Handler('MySearchObject')->find('someobjectid', 42);
$some_objects = Pundit::Handler('MySearchObject')->findAll('someobjectid', 42);
$more_objects = Pundit::Handler('MySearchObject')->getObjects( new PunditCriteria('search_textfield', 'random') );
// Note, it will do searches like %value% like you would expect, so don't add % yourself.
$count = Pundit::Handler('MySearchObject')->getCount( new PunditCriteria('search_textfield', 'random') );
?>
And you can of course use PunditCriteriaComboes as well, and use the setOrder and setLimit functions of the criteria.
I also added the function serialize() to the PunditCriteria family which will return the criteria as an array instead of a “rendered” SQL-string which is what enables the Solr functionality to use PunditCriterias instead of some custom functionality to accomplish the same.
I’m now up to revision 106, which makes it pretty clear that revision 100 has been passed. Wow. Who would have thought.
I’ve been adding more RSS-feeds to Uniteit. One of the more funky one is the Notifications feed which collects all notifications you have recieved because of tag-subscription or comment-subscriptions and puts then in one central feed for you to follow.
The reason why I did the Solr integration was of course to add search to Uniteit, and I’m pretty much down to just not having done the interface right now. So sit tight and wait, for search will be here!
That was all for now folks..
-fangel
March 11th, 2007 | PHP, Pundit