You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Jörg Agatz <jo...@googlemail.com> on 2009/05/25 17:15:18 UTC

problem with Solrits (schow only some of the fields)

Hallo...

I have a Problem...

I will use Solitas for solr..
But i have a Problem...
At the moment, Solitas present All fields in the Results, but i musst change
it and present only some, Like id, name, cat and inStock (exampledocuments)

i think that is the code to post all fields..

<div class="result-document">
  #foreach($fieldname in $doc.fieldNames)
     <p>
       <span class="field-name">$fieldname :</span>
       <span>
       #foreach($value in $doc.getFieldValues($fieldname))
         $value
       #end
       </span>
     </p>
  #end
  #if($params.getBool("debugQuery",false))
    <a href="#" onclick='jQuery(this).siblings("pre").toggle(); return
false;'>toggle explain</a>
    <pre style="display:none">$response.getExplainMap().get($doc.getFirstValue('id'))</pre>
  #end

</div>

Maby someone can explain me how i can change the code to get some fields
Jörg Agatz

Re: problem with Solrits (schow only some of the fields)

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
On May 25, 2009, at 11:15 AM, Jörg Agatz wrote:
> I will use Solitas for solr..

Yay!  Our first customer ;)

> At the moment, Solitas present All fields in the Results, but i  
> musst change
> it and present only some, Like id, name, cat and inStock  
> (exampledocuments)
>
> i think that is the code to post all fields..
>
> <div class="result-document">
>  #foreach($fieldname in $doc.fieldNames)
>     <p>
>       <span class="field-name">$fieldname :</span>
>       <span>
>       #foreach($value in $doc.getFieldValues($fieldname))
>         $value
>       #end
>       </span>
>     </p>
>  #end

Right - this is just generic code to show all stored fields from the  
document (TODO: and really should be adjusted to be fl parameter aware).

> Maby someone can explain me how i can change the code to get some  
> field

Sure... first, this page describes the objects you have available in  
the template (Velocity) context:  http://wiki.apache.org/solr/VelocityResponseWriter 
  - you can link off to javadocs from there to see more about what  
each object provides in terms of getters and such.

There is a $response.  From the default browse.vm template, $doc is a  
single item in an iteration over $response.results

$doc is an org.apache.solr.common.SolrDocument (http://lucene.apache.org/solr/api/org/apache/solr/common/SolrDocument.html 
)

So from a $doc, you can do this:

   $doc.getFirstValue("name")

getFirstValue is used when it is known to be a single valued field  
(experiment with the other getters on SolrDocument to see how they  
work with various fields).

In the custom templates I have been using, I define a macro to make  
this even easier - you can define it in VM_global_library.vm in conf/ 
velocity to make it global to all templates:

    #macro(field $f)$!{esc.html($doc.getFirstValue($f))}#end

Now you can use #field("name") instead, making templates much  
cleaner.  The $!{esc.html(...)} bit is there to HTML escape the field  
value, otherwise it leaves the possibility of malformed rendering or  
even the possibility of a JavaScript injection vulnerability.  The  
explanation point is a Velocity templating feature to not render  
anything if the value is null, otherwise it'll literally render "$ 
{....}" in the output.

Perhaps more than you were asking for, but I wanted to be thorough  
since this is a feature of Solr I'd like to see get some more, ahem,  
visibility.

	Erik