Solr 4.0: DocTransformers first look

In todays entry we will look at the next feature that will come with version 4.0 of Apache Solr. We will look at the functionality which enables us to modify the fields in Solr result list.

Do I need it ?

Till now, we didn’t have much choice when it comes to the results returned by Solr. When Solr 4.0 will be published we will be given a new tool, so called DocTransformers. This feature enables us to modify the fields of the documents returned in the search results by Solr. Looking at what is available now we can for example change the names of the fields returned or mark the documents that were added by the QueryElevationComponent. Right now there are only a few implementation, but implementing your own DocTranformer is not hard.

What is already available ?

At the exact moment we are writing this, the following transformers are available:

  • One that enables you to mark the documents that were added by the QueryElevationComponent.
  • One that enables you to add the explain information to the document.
  • One that enables you to add static value as a field of the document.
  • One that enables you to add the shard if from which the document was fetched.
  • One that enables you to add the docid as the document field (identifier used by Lucene).

How to use DocTransformers ?

Lets look at how to use DocTransformers. To do that I’ve downloaded trunk version of Apache Solr (4.0) from the svn repository and I’ve run the example deployment. Next, I’ve indexed the example data and I’ve run the following query:

If you look at the fl parameter you will notice that we told Solr that we want the name field in the results, the score of the document and two DocTransformers: [docid] and [explain]. In result I’ve got the following XML:

As you can see, Solr did what we asked for.

Your own implementation

Let’s discuss, who to implement you own DocTransformer. Below, you have an example class named RenameFieldsTransformer from the org.apache.solr.response.transform package in Apache Solr source code. In general, all you have to do is override the following two methods from the DocTransformer class from org.apache.solr.response.transform package:

  • String getName() – method returning transformers name,
  • void transform(SolrDocument doc, int docid) – method which makes the actual transformation.

Implementation looks like this:

The code shown above enables us to rename the fields returned in the results. As you can see the transform method iterates through all the values in rename class variable. The rename variable consist of name value pairs which are field name and the name it should have after the transformation. You must also remember that in order to use your own transformer you need to add it’s configuration to the solrconfig.xml file. Here is the example which can be found on Solr wiki page:

To sum up

You should remember that the describes functionality is marked as experimental and can change its behavior when Lucene and Solr 4.0 will be released. We will get back to this topic as soon as Solr 4.0 will be released.

3 thoughts on “Solr 4.0: DocTransformers first look

  • 3 April 2014 at 15:28

    Nice post. I have one question, can we sort Solr Documents inside Solr DocTransformer. I have one field stored in database. I like to sort solr documents inside solr doc transformer by using this database field. If u have any Idea then can anybody share with me

    • 15 June 2014 at 12:36

      DocTransformers don’t have information about all the documents (you can see that in the code). I would advice indexing the document you want to sort on and let Solr sort on it. Although this is a general advice – I would have to have more information about your use case.

  • 3 June 2016 at 10:12

    Hi Sir,
    When I tried the above example I am getting error like Error Instantiating TransformerFactory, failed to instantiate org.apache.solr.response.transform.TransformerFactory.

    Please help me to resolve this.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.