Solr
  1. Solr
  2. SOLR-7915

Provide pluggable Velocity context tool facility

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 5.3
    • Fix Version/s: 5.4, 6.0
    • Component/s: contrib - Velocity
    • Labels:
      None

      Description

      Currently the "tools" placed in the VelocityResponseWriter's context are hard-coded. It can be very handy to be able to plug in 3rd party or custom tools (just any ol' Java object a "tool" can be).

      Here's a list of the currently hard-coded tools: https://github.com/apache/lucene-solr/blob/trunk/solr/contrib/velocity/src/java/org/apache/solr/response/VelocityResponseWriter.java#L189-L199


      The implementation committed allows custom tools to be registered as part of the VelocityResponseWriter definition in solrconfig.xml like this:

              <queryResponseWriter name="velocityWithCustomTools" class="solr.VelocityResponseWriter">
                <lst name="tools">
                  <str name="mytool">com.example.solr.velocity.MyTool</str>
                  <!-- ... -->
                </lst>
              </queryResponseWriter>
      
      1. SOLR-7915.patch
        9 kB
        Erik Hatcher

        Activity

        Hide
        Erik Hatcher added a comment -

        Here's a patch to implement this feature, including passing tests.

        The one debatable piece in here is the use of SolrCore.createInstance. Noble Paul - thoughts? What's the best way moving forward to instantiate classes that are dynamically "plugged in" either via <lib>, lib/, or blob store.

        Show
        Erik Hatcher added a comment - Here's a patch to implement this feature, including passing tests. The one debatable piece in here is the use of SolrCore.createInstance. Noble Paul - thoughts? What's the best way moving forward to instantiate classes that are dynamically "plugged in" either via <lib>, lib/, or blob store.
        Hide
        Noble Paul added a comment -

        ideally the design could be as follows

        VRW can implement ResourceLoaderAware . It should have the appropriate ResourceLoader depending on whether it had runtimeLib=true specified or not it would get the right ResourceLoader

        use the resourceLoader on core.createInstance() to instantiate your tool

        Show
        Noble Paul added a comment - ideally the design could be as follows VRW can implement ResourceLoaderAware . It should have the appropriate ResourceLoader depending on whether it had runtimeLib=true specified or not it would get the right ResourceLoader use the resourceLoader on core.createInstance() to instantiate your tool
        Hide
        Erik Hatcher added a comment -

        Unfortunately VrW has no `awareCompatibility` entry (see SolrResourceLoader), so that technique doesn't work without other changes, but the SolrCore.createInstance using SolrCore's getResourceLoader works well in the tests (and also in a deployed environment with real custom tools in a separate JAR file referred to in <lib>).

        Show
        Erik Hatcher added a comment - Unfortunately VrW has no `awareCompatibility` entry (see SolrResourceLoader), so that technique doesn't work without other changes, but the SolrCore.createInstance using SolrCore's getResourceLoader works well in the tests (and also in a deployed environment with real custom tools in a separate JAR file referred to in <lib>).
        Hide
        ASF subversion and git services added a comment -

        Commit 1700691 from Erik Hatcher in branch 'dev/trunk'
        [ https://svn.apache.org/r1700691 ]

        SOLR-7915: Provide pluggable context tool support for VelocityResponseWriter

        Show
        ASF subversion and git services added a comment - Commit 1700691 from Erik Hatcher in branch 'dev/trunk' [ https://svn.apache.org/r1700691 ] SOLR-7915 : Provide pluggable context tool support for VelocityResponseWriter
        Hide
        ASF subversion and git services added a comment -

        Commit 1700692 from Erik Hatcher in branch 'dev/branches/branch_5x'
        [ https://svn.apache.org/r1700692 ]

        SOLR-7915: Provide pluggable context tool support for VelocityResponseWriter (merged from trunk r1700691)

        Show
        ASF subversion and git services added a comment - Commit 1700692 from Erik Hatcher in branch 'dev/branches/branch_5x' [ https://svn.apache.org/r1700692 ] SOLR-7915 : Provide pluggable context tool support for VelocityResponseWriter (merged from trunk r1700691)
        Hide
        ASF subversion and git services added a comment -

        Commit 1700693 from Erik Hatcher in branch 'dev/branches/branch_5x'
        [ https://svn.apache.org/r1700693 ]

        SOLR-7915: Provide pluggable context tool support for VelocityResponseWriter (merged from trunk r1700691)

        Show
        ASF subversion and git services added a comment - Commit 1700693 from Erik Hatcher in branch 'dev/branches/branch_5x' [ https://svn.apache.org/r1700693 ] SOLR-7915 : Provide pluggable context tool support for VelocityResponseWriter (merged from trunk r1700691)
        Hide
        Alexandre Rafalovitch added a comment -

        Will this also be configurable through the other ways we do it? paramsets, REST, etc? Not sure if <lst name="tools"> would cause issues. Noble Paul?

        Show
        Alexandre Rafalovitch added a comment - Will this also be configurable through the other ways we do it? paramsets, REST, etc? Not sure if <lst name="tools"> would cause issues. Noble Paul ?
        Hide
        Erik Hatcher added a comment -

        paramsets are for search handlers, so doesn't relate to this. But definitely the REST API should be able to handle this. I've not tested that, but it is legitimate response writer configuration capabilities being used here.

        Show
        Erik Hatcher added a comment - paramsets are for search handlers, so doesn't relate to this. But definitely the REST API should be able to handle this. I've not tested that, but it is legitimate response writer configuration capabilities being used here.
        Hide
        Erik Hatcher added a comment - - edited

        I just tested this, thanks for asking Alexandre Rafalovitch (on a clean branch_5x checkout) -

        bin/solr create -c files -d example/files
        bin/post -c files ~/Documents/TestDocs
        curl http://localhost:8983/solr/files/config -H 'Content-type:application/json' -d '{
          "add-queryresponsewriter":{
            "name":"celeritas",
            "class":"solr.VelocityResponseWriter",
            "params.resource.loader.enabled": "true",
            "tools":{
              "just_a_string":"java.lang.String"
            }
          }
        }'
        

        It modifies configoverlay.json with:

        {"queryResponseWriter":{"celeritas":{
              "name":"celeritas",
              "class":"solr.VelocityResponseWriter",
              "params.resource.loader.enabled":"true",
              "tools":{"just_a_string":"java.lang.String"}}}}
        

        And that behaves as expected leveraging the new tool, http://localhost:8983/solr/files/select?q=*:*&wt=celeritas&v.template=foo&v.template.foo=$response.results.numFound:%20(and%20btw:%20just_a_string.class%20=%20$just_a_string.class) responds with:

        328: (and btw: just_a_string.class = class java.lang.String)
        

        The example really is a glorified version of outputting, through a Velocity template (provided in the request, and enabled by params resource loader), $just_a_string.class, showing that the tool $just_a_string was created and methods can be called on it.

        Show
        Erik Hatcher added a comment - - edited I just tested this, thanks for asking Alexandre Rafalovitch (on a clean branch_5x checkout) - bin/solr create -c files -d example/files bin/post -c files ~/Documents/TestDocs curl http: //localhost:8983/solr/files/config -H 'Content-type:application/json' -d '{ "add-queryresponsewriter" :{ "name" : "celeritas" , "class" : "solr.VelocityResponseWriter" , "params.resource.loader.enabled" : " true " , "tools" :{ "just_a_string" : "java.lang. String " } } }' It modifies configoverlay.json with: { "queryResponseWriter" :{ "celeritas" :{ "name" : "celeritas" , "class" : "solr.VelocityResponseWriter" , "params.resource.loader.enabled" : " true " , "tools" :{ "just_a_string" : "java.lang. String " }}}} And that behaves as expected leveraging the new tool, http://localhost:8983/solr/files/select?q=*:*&wt=celeritas&v.template=foo&v.template.foo=$response.results.numFound:%20(and%20btw:%20just_a_string.class%20=%20$just_a_string.class ) responds with: 328: (and btw: just_a_string.class = class java.lang. String ) The example really is a glorified version of outputting, through a Velocity template (provided in the request, and enabled by params resource loader), $just_a_string.class, showing that the tool $just_a_string was created and methods can be called on it.

          People

          • Assignee:
            Erik Hatcher
            Reporter:
            Erik Hatcher
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development