.NET Core has excellent support for doing searches in the Solr search engine. The search language is not always logical, but the search itself is manageable. Here’s a quick tutorial on how to get started.
STEP 1: THE NUGET PACKAGES
You need the following NuGet packages:
STEP 2: IDENTIFY THE FIELDS YOU WISH TO RETURN IN THE QUERY
You don’t need to return all the fields from the Solr index, but you will need to make a model class that can map the Solr field to a object field.
From the list of fields, I map the ones that I would like to have returned, in a model class:
using SolrNet.Attributes; namespace MyCode { public class MySolrModel { [SolrField("_fullpath")] public string FullPath { get; set; } [SolrField("advertcategorytitle_s")] public string CategoryTitle { get; set; } [SolrField("advertcategorydeprecated_b")] public bool Deprecated { get; set; } } }
STEP 3: INJECT SOLR INTO YOUR SERVICECOLLECTION
Your code needs to know the Solr URL and which model to return when the Solr instance is queried. This is an example on how to inject Solr, your method might differ slightly:
using SolrNet; private IServiceProvider InitializeServiceCollection() { var services = new ServiceCollection() .AddLogging(configure => configure .AddConsole() ) .AddSolrNet<MySolrModel>("https://[solrinstance]:8983/solr/[indexname]") .BuildServiceProvider(); return services; }
STEP 4: CREATE A SEARCH REPOSITORY TO DO SEARCHES:
Now onto the actual code. This is probably the simplest repository that can do a Solr search:
using SolrNet; using SolrNet.Commands.Parameters; using System.Linq; using System.Threading.Tasks; using System.Collections.Generic; namespace MyCode { public class MySolrRepository { private readonly ISolrReadOnlyOperations<MySolrModel> _solr; public AdvertCategoryRepository(ISolrReadOnlyOperations<MySolrModel> solr) { _solr = solr; } public async Task<IEnumerable<MySolrModel>> Search(string searchString) { var results = await _solr.QueryAsync(searchString); return results; } } }
The Search method will do a generic search in the index that you specified when doing the dependency injection. It will not only search in the fields that your model class returns, but any field marked as searchable in the index.
You can do more complex searches by modifying the QueryAsync method. This example will do field based searches, and return only one row:
public async Task<MySolrModel> Search(string searchString) { var solrResult = (await _solr.QueryAsync(new SolrMultipleCriteriaQuery(new ISolrQuery[] { new SolrQueryByField("_template", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"), new SolrQueryByField("_language", "da"), new SolrQueryByField("_latestversion", "true"), new SolrQueryByField("advertcategorydeprecated_b", "false"), new SolrQueryByField("_title", searchString) }, SolrMultipleCriteriaQuery.Operator.AND), new QueryOptions { Rows = 1 })) .FirstOrDefault(); if (solrResult != null) return solrResult; return null; }
That’s it for this tutorial. Happy coding!
MORE TO READ:
- Apache Solr
- Implementing Your Own Enterprise Search from Microsoft
- Solr Query Operations from Solr documentation on GitHub