ellipses

. ...

Persistent URLs in Ajax-solr

The ajax-solr library is a great starting point for using Solr directly via the rest interface. There are various widgets and managers for storing state, and to ensure that back/forward work correctly on the browser.

For a part of a current project, we wanted to have the ability to share urls and the simplest thing to enable seemed to be the ParameterHashStore which uses the stored paramenters in the ParameterStore. To get this fully working I had to make the following changes.

Ensure the stores are referenced in reuters.js:

'core/ParameterStore',
'core/ParameterHashStore',

Set the manager to use the ParameterHashStore.

Manager.setStore(new AjaxSolr.ParameterHashStore());

And a condition to add a default query as the search was being called from an external page which may or may not set the search criteria.

var param = Manager.widgets.currentsearch.manager.store.params['q'];
if (!param)
{
  Manager.store.addByValue('q', '*:*');
}

In ParameterHashStore.js, the init function that sets up the event handlers didn’t seem to work in my configuration (this.exposed was always empty):

init: function () {
  if (this.exposed.length) {

The if condition was removed to ensure the events were always initialised.

Unfortunately, this worked only for the query string, rather than facets, page number, or another parameters, which are required if sharing the url, so I modified the the exposedString() method in ParameterStore.js.

exposedString: function () {
  var params = [], string;
  for (var i = 0, l = this.exposed.length; i < l; i++) {
    if (this.params[this.exposed[i]] !== undefined) {
      if (this.isMultiple(this.exposed[i])) {
        for (var j = 0, m = this.params[this.exposed[i]].length; j < m; j++) {
          string = this.params[this.exposed[i]][j].string();
          if (string) {
            params.push(string);
          }
        }
      }
      else {
        string = this.params[this.exposed[i]].string();
        if (string) {
          params.push(string);
        }
      }
    }
  }
  return params.join('&');
}

This was changed to (added reference to underscore.js):

exposedString: function() {
  var paramToString = function(param){
    if (param.name) return param.string();
    return _.map(param, paramToString);
  };

  return _.flatten(
    _.map(this.params, paramToString)).join('&');
}

This ensures that all the parameters are exposed, and will update window.location. The hash/url is now consistent with the selected facets and search query, and so far it seems to work OK.