在运行时连接到多个核心

时间:2015-12-28 09:19:13

标签: solrnet

有没有办法根据动态数据动态定义与新Solr核心的连接?

我们有一个场景,我们的Solr安装有相同类型文档的多个核心/索引,按日期分隔(因此给定周的文档将在索引1上,前一周在索引2上等)。

因此,当我收到查询时,我会检查所需的日期范围,并根据它,我想查询特定的核心。我不知道在启动时,我将拥有哪些核心,因为可以在运行时动态创建新核心。

使用内置的ServiceLocation提供程序,无法将两个不同的Core链接到同一文档类。但即使我使用不同的DI容器(在我的情况下目前是Autofac),我仍然需要在组件注册期间提前指定所有核心URL。

有没有办法绕过它,除了总是创建一个新的Autofac容器,生成ISolrOperation<>从它上课,并将其发布到下次我需要连接到核心时?

1 个答案:

答案 0 :(得分:1)

Mauricio Scheffer(Solr.Net的开发人员)的comment证实,没有内置支持即时连接到不同的索引URL。因此,我不是自己实例化内部对象,而是在我现有的基于Autofac的DI容器上使用了hack:

public ISolrOperations<TDocument> ConnectToIndex<TDocument>(string indexUrl)
{
    // Create a new AutoFac container environment.
    ContainerBuilder builder = new ContainerBuilder(); 

    // Autofac-for-Solr.Net config element.
    var cores = new SolrServers 
    {
        new SolrServerElement 
        {
            Id = indexUrl,
            DocumentType = typeof (TDocument).AssemblyQualifiedName,
            Url = indexUrl,
        }
    };

    // Create the Autofac container.
    builder.RegisterModule(new SolrNetModule(cores));
    var container = builder.Build();

    // Resolve the SolrNet object for the URL.     
    return container.Resolve<ISolrOperations<TDocument>>();
}