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 Ravindra Soni <ra...@gmail.com> on 2018/01/24 14:50:12 UTC

Post-processing of Solr responses in Velocity templates by calling external Java methods

Hi,

*Quick context of the application first.*

I am currently using Solr in standalone mode to index thousands of
text-like documents (not exactly textual documents).

A single document has a structure like following:

   - *id* - unique file id
   - *line_text* - textual data
   - *file_local_url* - file's directory location path
   - *line_no* - line number of the line_text

*What I am trying to achieve?*

After I search on the line_text field, as a result I should see the
matching text snippets. A text snippet can be the lines from (line_no - 3)
to (line_no + 3). Currently for search front-end I am using Solr's in-built
Velocity response writer and its templates.

*How I am thinking of doing this?*

I am using the default Velocity templates which are shipped with Solr. In
the template file hit.vm, following is the way it currently fetches,
processes and displays the responses:

#foreach( $fieldName in $doc.fieldNames )

    <tr>
      <th align="right" valign="top" style="field-name">
        $esc.html($fieldName):
      </th>

      <td align="left" valign="top">
        #field($fieldName)
      </td>
    </tr>#end

Now to get the snippet text I would like to define an external function
somewhere in a Java class something of this form:

public String getSnippet(file_local_url, line_no)

which will return the snippet in a string format.

Now I want to consume this response in the velocity template. There I am
thinking of something like this:

 ## get the snippet string by calling the external ava function
 #set($snippet = $someClass.getSnippet(#field("file_local_url"),
#field("line_no)))

 ## print the snippet
  snippet

(I am not sure if this is the correct syntax.)

*Questions:*

   1. What kind of file should contain someClass.getSnippet()? Java file?
   Class file? A jar?
   2. Where should I keep this file? How will velocity know where to find
   this class?
   3. Am I using the write syntax to call the method and use its results in
   above Velocity template?

I am quite not getting the bigger picture yet (especially the question 2
above).

Please provide some direction. Thanks.
Ravi.

-- 

What if the cure for cancer is trapped inside the mind of someone who
can't afford an education? - anonymous

Re: Post-processing of Solr responses in Velocity templates by calling external Java methods

Posted by Erik Hatcher <er...@gmail.com>.
Ravindra -

So you have documents that represent *lines*, but for each line document you want to render the 3 lines (documents) before and after.

Hmmm - tricky!   

Velocity itself isn’t going to help you here.   You’ll need to do additional searches to get the “context documents”.   

Given that you’re using VrW, I’d suggest an Ajaxy solution.   For each hit being rendering, the browser (not server-side Velocity) would use `line_no` and do a query, something like q=line_no:[<current line_no -  3> TO <current line_no + 3>] and render those as desired.

If you attempt this server-side, whew - honestly I wouldn’t do that within Solr myself - so I’m not sure what exactly to advise.

To your specific question - it is possible to add custom Velocity “tools” to the mix.   The test cases for this look like this:

    https://github.com/apache/lucene-solr/blob/e2521b2a8baabdaf43b92192588f51e042d21e97/solr/contrib/velocity/src/test/org/apache/solr/velocity/VelocityResponseWriterTest.java#L122-L144

(specifically note the $mytool.star(…) usage in that test method)

The configuration to plug in a custom tool looks like this:

   https://github.com/apache/lucene-solr/blob/e2521b2a8baabdaf43b92192588f51e042d21e97/solr/contrib/velocity/src/test-files/velocity/solr/collection1/conf/solrconfig.xml#L40-L54

Here’s how the tool itself is written, which comes out pretty lean and clean:

   https://github.com/apache/lucene-solr/blob/e2521b2a8baabdaf43b92192588f51e042d21e97/solr/contrib/velocity/src/test/org/apache/solr/velocity/MockTool.java

Again, though - I wouldn’t advise using Velocity trickery to do Solr searches internal to template rendering (even though you can).

	Erik


> On Jan 24, 2018, at 9:50 AM, Ravindra Soni <ra...@gmail.com> wrote:
> 
> Hi,
> 
> *Quick context of the application first.*
> 
> I am currently using Solr in standalone mode to index thousands of
> text-like documents (not exactly textual documents).
> 
> A single document has a structure like following:
> 
>   - *id* - unique file id
>   - *line_text* - textual data
>   - *file_local_url* - file's directory location path
>   - *line_no* - line number of the line_text
> 
> *What I am trying to achieve?*
> 
> After I search on the line_text field, as a result I should see the
> matching text snippets. A text snippet can be the lines from (line_no - 3)
> to (line_no + 3). Currently for search front-end I am using Solr's in-built
> Velocity response writer and its templates.
> 
> *How I am thinking of doing this?*
> 
> I am using the default Velocity templates which are shipped with Solr. In
> the template file hit.vm, following is the way it currently fetches,
> processes and displays the responses:
> 
> #foreach( $fieldName in $doc.fieldNames )
> 
>    <tr>
>      <th align="right" valign="top" style="field-name">
>        $esc.html($fieldName):
>      </th>
> 
>      <td align="left" valign="top">
>        #field($fieldName)
>      </td>
>    </tr>#end
> 
> Now to get the snippet text I would like to define an external function
> somewhere in a Java class something of this form:
> 
> public String getSnippet(file_local_url, line_no)
> 
> which will return the snippet in a string format.
> 
> Now I want to consume this response in the velocity template. There I am
> thinking of something like this:
> 
> ## get the snippet string by calling the external ava function
> #set($snippet = $someClass.getSnippet(#field("file_local_url"),
> #field("line_no)))
> 
> ## print the snippet
>  snippet
> 
> (I am not sure if this is the correct syntax.)
> 
> *Questions:*
> 
>   1. What kind of file should contain someClass.getSnippet()? Java file?
>   Class file? A jar?
>   2. Where should I keep this file? How will velocity know where to find
>   this class?
>   3. Am I using the write syntax to call the method and use its results in
>   above Velocity template?
> 
> I am quite not getting the bigger picture yet (especially the question 2
> above).
> 
> Please provide some direction. Thanks.
> Ravi.
> 
> -- 
> 
> What if the cure for cancer is trapped inside the mind of someone who
> can't afford an education? - anonymous


Re: Post-processing of Solr responses in Velocity templates by calling external Java methods

Posted by Emir Arnautović <em...@sematext.com>.
Hi Ravi,
It seems to me that you are trying to simulate some of highlighting features, but might be wrong. Can you explain your problem not the problem you run into with your solution.

With what you explained, it seems to me that the easiest solution would be to have snippet field that is stored and not indexed and you can use it in your template.

HTH,
Emir
--
Monitoring - Log Management - Alerting - Anomaly Detection
Solr & Elasticsearch Consulting Support Training - http://sematext.com/



> On 24 Jan 2018, at 15:50, Ravindra Soni <ra...@gmail.com> wrote:
> 
> Hi,
> 
> *Quick context of the application first.*
> 
> I am currently using Solr in standalone mode to index thousands of
> text-like documents (not exactly textual documents).
> 
> A single document has a structure like following:
> 
>   - *id* - unique file id
>   - *line_text* - textual data
>   - *file_local_url* - file's directory location path
>   - *line_no* - line number of the line_text
> 
> *What I am trying to achieve?*
> 
> After I search on the line_text field, as a result I should see the
> matching text snippets. A text snippet can be the lines from (line_no - 3)
> to (line_no + 3). Currently for search front-end I am using Solr's in-built
> Velocity response writer and its templates.
> 
> *How I am thinking of doing this?*
> 
> I am using the default Velocity templates which are shipped with Solr. In
> the template file hit.vm, following is the way it currently fetches,
> processes and displays the responses:
> 
> #foreach( $fieldName in $doc.fieldNames )
> 
>    <tr>
>      <th align="right" valign="top" style="field-name">
>        $esc.html($fieldName):
>      </th>
> 
>      <td align="left" valign="top">
>        #field($fieldName)
>      </td>
>    </tr>#end
> 
> Now to get the snippet text I would like to define an external function
> somewhere in a Java class something of this form:
> 
> public String getSnippet(file_local_url, line_no)
> 
> which will return the snippet in a string format.
> 
> Now I want to consume this response in the velocity template. There I am
> thinking of something like this:
> 
> ## get the snippet string by calling the external ava function
> #set($snippet = $someClass.getSnippet(#field("file_local_url"),
> #field("line_no)))
> 
> ## print the snippet
>  snippet
> 
> (I am not sure if this is the correct syntax.)
> 
> *Questions:*
> 
>   1. What kind of file should contain someClass.getSnippet()? Java file?
>   Class file? A jar?
>   2. Where should I keep this file? How will velocity know where to find
>   this class?
>   3. Am I using the write syntax to call the method and use its results in
>   above Velocity template?
> 
> I am quite not getting the bigger picture yet (especially the question 2
> above).
> 
> Please provide some direction. Thanks.
> Ravi.
> 
> -- 
> 
> What if the cure for cancer is trapped inside the mind of someone who
> can't afford an education? - anonymous