Skip to content

Reuters tutorial: step 1

jpmckinney edited this page Sep 13, 2010 · 29 revisions

In this first iteration, we make sure we can talk to the Solr instance. AJAX Solr is JavaScript framework-agnostic; it only requires an AJAX implementation to send requests to Solr. In this example, we use jQuery.

In AJAX Solr, the Manager sends these requests, and passes the responses to each widget for handling. (The Manager behaves as the controller in a MVC framework. All public calls should be performed on the manager.)

In the head tag, we add the JavaScript files for AJAX Solr and the Manager:

<script type="text/javascript" src="../core/Core.js"></script>
<script type="text/javascript" src="../core/AbstractManager.js"></script>
<script type="text/javascript" src="../managers/Manager.jquery.js"></script>

In the reuters.js file, once the DOM is ready, we’ll initialize a instance of the Manager. (We wrap the code in (function ($) { ... })(jQuery); to permit local usage of the $ variable while preventing conflicts over it):

var Manager;
(function ($) {
  $(function () {
    Manager = new AjaxSolr.Manager({
      solrUrl: 'http://example.solrstuff.org/solrjs/select',
    });
    Manager.init();
  });
})(jQuery);

The manager takes as a parameter either solrUrl – if talking to Solr directly – or proxyUrl – if talking to Solr through a proxy. (Usually, you want to talk to the instance through a proxy, for security. We will go over writing proxies for AJAX Solr in another tutorial. Here, we communicate with the instance directly.)

AJAX Solr stores Solr parameters in a ParameterStore. In your web application, some parameters won’t change, e.g. facet and facet.field parameters, while others will, e.g. the q and fq parameters. We add the JS files for the ParameterStore:

<script type="text/javascript" src="../core/Parameter.js"></script>
<script type="text/javascript" src="../core/ParameterStore.js"></script>

(To use another ParameterStore, see the AbstractManager#setStore method and the ParameterStore class.) Let’s use the ParameterStore right now to build a basic query.

Manager.store.addByValue('q', 'oil');

To finish this iteration, check if we can talk to the Solr instance.

Manager.doRequest();

If you’re running Firefox with Firebug or Safari, you can open the JavaScript console and type manager.response to view the Solr response.

View iteration 1