asp.net-coresolrsolrnet

SolrNet: using different models for indexing vs querying


In an asp.net core api solution, I am using solr for full-text search. I want to understand how to configure SolrNet to use different models for indexing and searching on the same collection.

For my use case, I store only the id of my model object in solr - other fields are indexed but are not stored. This is because I use the database to retrieve models, once solr tells me which ids are search hits.

In this case, suppose my model is "Folder". I have a collection for folders and have it registered in DI for indexing, using a FolderDto object to define mappings.

services.AddSolrNet<FolderDto>($"{solrEndpoint}/folders");
services.AddScoped<IFolderIndex, FolderIndex>();

I have indexing methods within my implementation of IFolderIndex.

What do I need to do in DI, to define a separate SolrNet connection service to use for searching, which I would want to respond with a collection of int ids?


Solution

  • It turns out I just needed to provide the query-time model in DI:

    services.AddSolrNet<FolderDto>($"{solrEndpoint}/folders");
    services.AddSolrNet<FolderQueryResultDto>($"{solrEndpoint}/folders"); // this bit
    services.AddScoped<IFolderIndex, FolderIndex>();
    

    Then I can instantiate a suitable object for the querying in my FolderIndex:

    public FolderIndex(ISolrOperations<FolderDto> solr, ISolrOperations<FolderQueryResultDto> solrSearcher)
    {
        _solr = solr;
        _solrSearcher = solrSearcher;
    }
    

    And that causes SolrNet to know that when I am using that solrSearcher it should generate queries to the right endpoint, and parse the responses to the right type.