Subscribe

Query Profiles in Moriarty

I just committed another batch of changes to Moriarty (svn revision 482). This version contains some important changes to the way classes are included (thanks to prompting by kwijibo on #talis over the weekend). Previously Moriarty assumed that your classes were in directories in your include path. Now Moriarty expects its classes to reside in the directory defined by MORIARTY_DIR. If this isn’t already defined then Moriarty will define it to be the same directory as that containing constants.inc.php. A similar constant MORIARTY_ARC_DIR defines the directory where Moriarty expects to find ARC2.php. If this isn’t set then it will assume ARC is in a sibling directory. Take a look at constants.inc.php for the logic.

I also added support for query profiles which control the relative weights applied to each field in a text search. In Moriarty this class is a NetworkResource so you can easily populate the object by getting it from the network:

  $qp = new QueryProfile("http://api.talis.com/stores/mystore/config/queryprofiles/1");
  $response = $qp->get_from_network();
  if ( $response->is_success() ) {
    // do something with qp...
  }

Setting a query profile for a store is also quite easy. This example shows how to create a new query profile, set some field weights and then save it to the Platform:

  $qp = new QueryProfile("http://api.talis.com/stores/mystore/config/queryprofiles/1");
  $qp->add_field_weight('name', '2.0'); // the name field is twice as important than average
  $qp->add_field_weight('comments', '0.5'); // the name field is half as important as average
  $response = $qp->put_to_network();
  if ( $response->is_success() ) {
    // do something with qp...
  }

You can also remove field weights and replace them with alternate ones:

  $qp = new QueryProfile("http://api.talis.com/stores/mystore/config/queryprofiles/1");
  $response = $qp->get_from_network();
  if ( $response->is_success() ) {
    $qp->remove_field_weight('comments');
    $qp->add_field_weight('comments', '3');
  }

Finally, I added a utility function to assist when copying a query profile from one store to another. It recalculates all the URIs so they apply to the new store rather than the old one. Here’s how you could use it to clone a query profile from one store to another:

  $qp = new QueryProfile("http://api.talis.com/stores/mystore/config/queryprofiles/1");
  $response = $qp->get_from_network();
  if ( $response->is_success() ) {
    $new_qp = $qp->copy_to("http://api.talis.com/stores/otherstore/config/queryprofiles/1");
    $new_qp->put_to_network();
  }

You might be wondering why I chose the long method names get_from_network and put_to_network over shorter ones like load or save? The reason is that I strongly believe that it’s wrong to hide the network from the application. One of Moriarty’s principles is that it is the thinnest wrapper around HTTP that is possible. That’s why many network operations return the actual response object from the HTTP interaction. The developer can then inspect any headers that the server sends. Naming these methods explicitly reminds the developer that these are network operations and not local ones. The developer needs to be aware because networked applications need to be written differently to those operating on a single machine. Networks have latency, so it’s not wise to be calling these methods a thousand times a second and they are unreliable so the developer needs to be able to handle failure gracefully and be prepared to retry (these are a couple of the 8 Fallacies of Distributed Computing). Moriarty doesn’t try to hide these issues from the developer.

I also added query profile support to the Config class. The get_first_query_profile method is guaranteed to get you the query profile of your store, regardless of its URI. As explained in the FAQ query profiles can exist in a number of locations depending on the store configuration. I worked out the logic for every existing store on the platform, so this code will always get your query profile:

  $store = new Store("http://api.talis.com/stores/mystore");
  $config = $store->get_config();
  $qp = $config->get_first_query_profile();

If you just want the query profile URI then you can call $config->get_first_query_profile_uri()

About Moriarty… Moriarty is a simple PHP library for accessing the Talis Platform. It follows the Platform API very closely and wraps ups 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