dependency injection in solr

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

dependency injection in solr

Federico Fissore-2
Hello everyone

I need to hack solr by adding a couple custom search components.
One small inconvenience is about configuring all the stuff. AFAIK
solrconfig.xml is not a place where to do dependency injection, not yet
at least.

Have you ever had the need to use DI on a solr configuration? How have
you managed it? Hard coding params in some delegate SearchComponent?
Getting a reference of a spring application context via some static
method? Any more elegant ways?

thanks in advance

federico
Reply | Threaded
Open this post in threaded view
|

Re: dependency injection in solr

Tomás Fernández Löbbe
You can do a lot of dependency injection though solrconfig.xml and
schema.xml, Specify search components, update processors, filters,
similarity, etc. Solr doesn't use any DI framework, everything is built-in
in a pluggable manner. What kind of customizations do you need to apply?
maybe we can point you better.

On Mon, Aug 29, 2011 at 10:20 AM, Federico Fissore <[hidden email]> wrote:

> Hello everyone
>
> I need to hack solr by adding a couple custom search components.
> One small inconvenience is about configuring all the stuff. AFAIK
> solrconfig.xml is not a place where to do dependency injection, not yet at
> least.
>
> Have you ever had the need to use DI on a solr configuration? How have you
> managed it? Hard coding params in some delegate SearchComponent? Getting a
> reference of a spring application context via some static method? Any more
> elegant ways?
>
> thanks in advance
>
> federico
>
Reply | Threaded
Open this post in threaded view
|

Re: dependency injection in solr

Federico Fissore-2
Tomás Fernández Löbbe, il 29/08/2011 16:39, ha scritto:
> You can do a lot of dependency injection though solrconfig.xml and
> schema.xml, Specify search components, update processors, filters,
> similarity, etc. Solr doesn't use any DI framework, everything is built-in
> in a pluggable manner. What kind of customizations do you need to apply?
> maybe we can point you better.


for example, I have a classification component: it can retrieve
classification data either from the file system or via a rest call.

the "retriever" is a parameter of the component and we want to set it
depending if we host the searcher or if the customer choose to install
it locally

i would like to inject a custom bean into the search component: at init
time, it will just call the retrieve method and that will return the
classification

(ps: we are trying to migrate our existing system to solr)

thanks in advance

federico
Reply | Threaded
Open this post in threaded view
|

Re: dependency injection in solr

Tomás Fernández Löbbe
mmmm I think I get it. Many of the objects that depend on the configuration
are instantiated by using reflection, is that an option for you?

On Mon, Aug 29, 2011 at 12:33 PM, Federico Fissore <[hidden email]> wrote:

> Tomás Fernández Löbbe, il 29/08/2011 16:39, ha scritto:
>
>  You can do a lot of dependency injection though solrconfig.xml and
>> schema.xml, Specify search components, update processors, filters,
>> similarity, etc. Solr doesn't use any DI framework, everything is built-in
>> in a pluggable manner. What kind of customizations do you need to apply?
>> maybe we can point you better.
>>
>
>
> for example, I have a classification component: it can retrieve
> classification data either from the file system or via a rest call.
>
> the "retriever" is a parameter of the component and we want to set it
> depending if we host the searcher or if the customer choose to install it
> locally
>
> i would like to inject a custom bean into the search component: at init
> time, it will just call the retrieve method and that will return the
> classification
>
> (ps: we are trying to migrate our existing system to solr)
>
> thanks in advance
>
> federico
>
Reply | Threaded
Open this post in threaded view
|

Re: dependency injection in solr

Federico Fissore
Tomás Fernández Löbbe, il 29/08/2011 17:58, ha scritto:
> mmmm I think I get it. Many of the objects that depend on the configuration
> are instantiated by using reflection, is that an option for you?
>

yes it is
what do you propose?
Reply | Threaded
Open this post in threaded view
|

Re: dependency injection in solr

Federico Fissore-2
In reply to this post by Tomás Fernández Löbbe
Tomás Fernández Löbbe, il 29/08/2011 17:58, ha scritto:
> mmmm I think I get it. Many of the objects that depend on the configuration
> are instantiated by using reflection, is that an option for you?
>

yes it is
what do you propose?
Reply | Threaded
Open this post in threaded view
|

Re: dependency injection in solr

Tomás Fernández Löbbe
You can use reflection to instantiate the correct object (specify the class
name on the parameter on the solrconfig and then invoke the constructor via
reflection). You'll have to manage the life-cycle of your object yourself.
If I understand your requirement, you probably have created a
SearchComponent that uses that "retriever", right?

On Mon, Aug 29, 2011 at 1:30 PM, Federico Fissore <[hidden email]> wrote:

> Tomás Fernández Löbbe, il 29/08/2011 17:58, ha scritto:
>
>  mmmm I think I get it. Many of the objects that depend on the
>> configuration
>> are instantiated by using reflection, is that an option for you?
>>
>>
> yes it is
> what do you propose?
>
Reply | Threaded
Open this post in threaded view
|

Re: dependency injection in solr

Federico Fissore-2
Tomás Fernández Löbbe, il 29/08/2011 20:32, ha scritto:
> You can use reflection to instantiate the correct object (specify the class
> name on the parameter on the solrconfig and then invoke the constructor via
> reflection). You'll have to manage the life-cycle of your object yourself.
> If I understand your requirement, you probably have created a
> SearchComponent that uses that "retriever", right?
>


sorry for the delay: I was experimenting.

Raw reflections do not suffice: you can't specify a dependency required
by your reflection-constructed object

I've ended up using spring this way (will cut some code for brevity)

I've enabled spring the usual way, adding a ContextLoaderListener in
web.xml and configuring the spring xml or java configuration files (I do
java configuration)

I've declared a spring bean named myComponentDeclaredInTheSpringConf,
that is an extension of SearchComponent, with it's collaborators

I've created SpringAwareSearchComponent, that is a delegate of
SearchComponent

public SpringAwareSearchComponent() {
  this.ctx = ContextLoader.getCurrentWebApplicationContext();
}
...
public void init(NamedList args) {
  super.init(args);
  inner = ctx.getBean(args.get("__beanname__").toString(),
SearchComponent.class);
  inner.init(args);
}

public void prepare(ResponseBuilder rb) throws IOException {
  inner.prepare(rb);
}

public void process(ResponseBuilder rb) throws IOException {
  inner.process(rb);
}

In solrconfig.xml I've declared the search component as
<searchComponent name="myComponent" class="SpringAwareSearchComponent">
  <str name="__beanname__">myComponentDeclaredInTheSpringConf</str>
  ...other bean specific parameters
</searchComponent>

and added myComponent to the list of search components

And it works like a charm. Maybe I can implement some other solr class
delegate and add hooks between spring and solr as needed

any comment will be appreciated

best regards

federico