Looking for the old docs site? You can still view it for a limited time here.

Custom JavaScript Query Stage Examples

Add a parameter to the query request

function(request,response , ctx, collection, solrServer, solrServerFactory) {
   request.addParam("foo", "bar");
}

Add a parameter to the query response

This example contains a simple JavaScript function which copies information from the ctx (Context) object into the query response. Requirements:

  • The response parameter cannot be used in query pipeline stages prior to the Solr query stage. This example assumes use in a later stage.

  • The response.initialEntity.appendStringList() function only works if the query’s wt (writer type) parameter is set to json or xml, for example: wt=json.

(function () {
    "use strict";

    var List = Java.type('java.util.List');

    function add_to_response(key, list) {
      if (list.length > 0) {
         response.initialEntity.appendStringList(key, Java.to(list, List));
      }
    }

    return function(request,response , ctx, collection, solrServer, solrServerFactory) {
       add_to_response('banners', ctx.getProperty('banners'));
       add_to_response('landing-pages', ctx.getProperty('redirects'));
    };
})();

Manually Adding Dependencies

To install dependencies manually, jar files must be placed in the ./apps/libs folder. The jar file path must be included in .apps/jetty/api/webapps/api-extra-classpath.txt and apps/jetty/connectors-classic/webapps/connectors-extra-classpath.txt.

For example, to create script objects that access and reference Java types from Javascript use the Java.type() function:

var TwitterFactory = Java.type("twitter4j.TwitterFactory");
var twitter = TwitterFactory.getSingleton();