Subscribe

Moriarty Facets

The latest batch of changes to Moriarty made it into subversion at the end oflast week (svn revision 655). The main change is the addition of a new FacetService class. You use it in the usual way. Either indirectly via the Store:

$store = new Store("http://api.talis.com/stores/mystore");
$fs = $store->get_facet_service();

Or directly if you know its URI:

$fs = new FacetService("http://api.talis.com/stores/mystore/service/facet");

Using the FacetService class is pretty simple: just call the facets method passing in the query, an array of fields to facet on and optionally the number of terms to return for each facet. As usual this method returns an HttpResponse:

$response = $fs->facets('query', array('field1','field2'));
if ($response->is_success()) {
  // do something useful
}
else {
  // mummy...
}

You can parse the XML response using the parse_facet_xml method which returns a nested array of data representing the facet data:

array (
  'field1' => array (
        0 => array ( 'value' => 'term1', 'number' => '5' ),
        1 => array ( 'value' => 'term2', 'number' => '4' ),
        1 => array ( 'value' => 'term3', 'number' => '2' ),
       ),
  'field2' => array (
        0 => array ( 'value' => 'term4', 'number' => '5' ),
        1 => array ( 'value' => 'term5', 'number' => '4' ),
        1 => array ( 'value' => 'term6', 'number' => '2' ),
       ),
) 

If you like living dangerously then you can combine both the previous steps into one using facets_to_array. If an error occurs this method simply returns an empty array:

$facets = $fs->facets_to_array('query', array('field1','field2'));

That’s it. A simple class for a simple but powerful service. You can read more about the Facet Service on the n² wiki.

There are a couple of big changes that I want to make pretty soon and I’m giving a heads up here because they may not be backwards compatible. The version of ARC I’m using is quite out of date (January 2008) so I need to update to the latest version. I’m not sure what that will involve. Maybe it’ll be completely smooth with no significant changes needed.

The second change is needed to make SimpleGraph’s index compatible with our RDF/PHP specification. I can see at least one major breaking change: I need to rename the hash key “val” to “value”. That is a pretty major breakage but I want to make Moriarty compatible with the RDF/PHP spec and with ARC2. I’m going to try and do that very soon.

About Moriarty… Moriarty is a simple PHP library for accessing the Talis Platform. It follows the Platform API very closely and wraps up many common tasks into convenient classes while remaining very lightweight. It also provides some simple RDF classes that are based on the excellent ARC2 class library. Moriarty is primarily being developed by Ian Davis and is in continual alpha, subject to occasional rapid bursts of change. You can read more about Moriarty on the n² wiki and get its source from the n² subversion repository