<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>first look &#8211; Solr.pl</title>
	<atom:link href="https://solr.pl/en/tag/first-look/feed/" rel="self" type="application/rss+xml" />
	<link>https://solr.pl/en/</link>
	<description>All things to be found - Blog related to Apache Solr &#38; Lucene projects - https://solr.apache.org</description>
	<lastBuildDate>Wed, 11 Nov 2020 20:56:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>Solr 4.0: DocTransformers first look</title>
		<link>https://solr.pl/en/2011/12/05/solr-4-0-doctransformers-first-look/</link>
					<comments>https://solr.pl/en/2011/12/05/solr-4-0-doctransformers-first-look/#respond</comments>
		
		<dc:creator><![CDATA[Rafał Kuć]]></dc:creator>
		<pubDate>Mon, 05 Dec 2011 20:55:51 +0000</pubDate>
				<category><![CDATA[Solr]]></category>
		<category><![CDATA[4.0]]></category>
		<category><![CDATA[doc]]></category>
		<category><![CDATA[document]]></category>
		<category><![CDATA[first]]></category>
		<category><![CDATA[first look]]></category>
		<category><![CDATA[look]]></category>
		<category><![CDATA[solr]]></category>
		<category><![CDATA[transformer]]></category>
		<category><![CDATA[transformers]]></category>
		<guid isPermaLink="false">http://sematext.solr.pl/?p=386</guid>

					<description><![CDATA[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]]></description>
										<content:encoded><![CDATA[<p>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.</p>
<p><span id="more-386"></span></p>
<h3>Do I need it ?</h3>
<p>Till now, we didn&#8217;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 <em>DocTransformers</em>. 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 <em>QueryElevationComponent</em>. Right now there are only a few implementation, but implementing your own <em>DocTranformer </em>is not hard.</p>
<h3>What is already available ?</h3>
<p>At the exact moment we are writing this, the following transformers are available:</p>
<ul>
<li>One that enables you to mark the documents that were added by the <em>QueryElevationComponent</em>.</li>
<li>One that enables you to add the explain information to the document.</li>
<li>One that enables you to add static value as a field of the document.</li>
<li>One that enables you to add the shard if from which the document was fetched.</li>
<li>One that enables you to add the <em>docid</em> as the document field (identifier used by Lucene).</li>
</ul>
<h3>How to use DocTransformers ?</h3>
<p>Lets look at how to use <em>DocTransformers</em>. To do that I&#8217;ve downloaded <em>trunk</em> version of Apache Solr (4.0) from the svn repository and I&#8217;ve run the example deployment. Next, I&#8217;ve indexed the example data and I&#8217;ve run the following query:
</p>
<pre class="brush:xml">http://localhost:8983/solr/select?q=encoded&amp;fl=name,score,[docid],[explain]</pre>
<p>If you look at the <em>fl</em> parameter you will notice that we told Solr that we want the <em>name</em> field in the results, the <em>score</em> of the document and two <em>DocTransformers</em>: <em>[docid]</em> and <em>[explain]</em>. In result I&#8217;ve got the following XML:
</p>
<pre class="brush:xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;response&gt;
 &lt;lst name="responseHeader"&gt;
  &lt;int name="status"&gt;0&lt;/int&gt;
  &lt;int name="QTime"&gt;2&lt;/int&gt;
  &lt;lst name="params"&gt;
    &lt;str name="q"&gt;encoded&lt;/str&gt;
    &lt;str name="fl"&gt;name,score,[docid],[explain]&lt;/str&gt;
  &lt;/lst&gt;
 &lt;/lst&gt;
 &lt;result name="response" numFound="2" start="0" maxScore="0.50524884"&gt;
 &lt;doc&gt;
  &lt;str name="name"&gt;Test with some GB18030 encoded characters&lt;/str&gt;
  &lt;float name="score"&gt;0.50524884&lt;/float&gt;
  &lt;int name="[docid]"&gt;0&lt;/int&gt;
  &lt;str name="[explain]"&gt;
  0.50524884 = (MATCH) weight(text:encoded in 0) [DefaultSimilarity], result of:
    0.50524884 = score(doc=0,freq=1.0 = termFreq=1), product of:
      1.0000001 = queryWeight, product of:
        3.2335923 = idf(docFreq=2, maxDocs=28)
        0.3092536 = queryNorm
      0.5052488 = fieldWeight in 0, product of:
        1.0 = tf(freq=1.0), with freq of:
          1.0 = termFreq=1
        3.2335923 = idf(docFreq=2, maxDocs=28)
        0.15625 = fieldNorm(doc=0)
  &lt;/str&gt;
 &lt;/doc&gt;
 &lt;doc&gt;
  &lt;str name="name"&gt;Test with some UTF-8 encoded characters&lt;/str&gt;
  &lt;float name="score"&gt;0.4041991&lt;/float&gt;
  &lt;int name="[docid]"&gt;25&lt;/int&gt;
  &lt;str name="[explain]"&gt;
  0.4041991 = (MATCH) weight(text:encoded in 25) [DefaultSimilarity], result of:
    0.4041991 = score(doc=25,freq=1.0 = termFreq=1), product of:
      1.0000001 = queryWeight, product of:
        3.2335923 = idf(docFreq=2, maxDocs=28)
        0.3092536 = queryNorm
      0.40419903 = fieldWeight in 25, product of:
        1.0 = tf(freq=1.0), with freq of:
          1.0 = termFreq=1
        3.2335923 = idf(docFreq=2, maxDocs=28)
        0.125 = fieldNorm(doc=25)
  &lt;/str&gt;
 &lt;/doc&gt;
&lt;/result&gt;
&lt;/response&gt;</pre>
<p>As you can see, Solr did what we asked for.</p>
<h3>Your own implementation</h3>
<p>Let&#8217;s discuss, who to implement you own <em>DocTransformer</em>. Below, you have an example class named <em>RenameFieldsTransformer </em>from the <em>org.apache.solr.response.transform</em> package in Apache Solr source code. In general, all you have to do is override the following two methods from the <em>DocTransformer</em> class from <em>org.apache.solr.response.transform</em> package:</p>
<ul>
<li><code>String getName()</code> &#8211; method returning transformers name,</li>
<li><code>void transform(SolrDocument doc, int docid)</code> &#8211; method which makes the actual transformation.</li>
</ul>
<p>Implementation looks like this:
</p>
<pre class="brush:java">public class RenameFieldsTransformer extends DocTransformer {
 final NamedList&lt;String&gt; rename;

 public RenameFieldsTransformer( NamedList&lt;String&gt; rename ) {
  this.rename = rename;
 }

 @Override
 public String getName() {
  StringBuilder str = new StringBuilder();
  str.append( "Rename[" );
  for( int i=0; i&lt; rename.size(); i++ ) {
   if( i &gt; 0 ) {
    str.append( "," );
   }
   str.append( rename.getName(i) ).append( "&gt;&gt;" ).append( rename.getVal( i ) );
  }
  str.append( "]" );
  return str.toString();
 }

 @Override
 public void transform(SolrDocument doc, int docid) {
  for( int i=0; i&lt;rename.size(); i++ ) {
   Object v = doc.remove( rename.getName(i) );
   if( v != null ) {
    doc.setField(rename.getVal(i), v);
   }
  }
 }
}</pre>
<p>The code shown above enables us to rename the fields returned in the results. As you can see the <em>transform</em> method iterates through all the values in <em>rename</em> class variable. The <em>rename</em> 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&#8217;s configuration to the <em>solrconfig.xml</em> file. Here is the example which can be found on Solr wiki page:
</p>
<pre class="brush:xml">&lt;transformer name="elevated" class="org.apache.solr.response.transform.EditorialMarkerFactory" /&gt;</pre>
<h3>To sum up</h3>
<p>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.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://solr.pl/en/2011/12/05/solr-4-0-doctransformers-first-look/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
