Tuesday 8 October 2024

Solr Search in Sitecore

Leave a Comment

An extensive search is necessary when working on Sitecore components that call for listing or looking for certain things. We will use Solr Search, which has strong indexing and search capabilities suited to our requirements, to do this.

General search with Solr
We may construct an instance of SitecoreIndexableItem, run a query, and get the anticipated results of that search by taking into consideration the path of the item from which we want to execute the search, whether it is the homepage, a folder, or any specific page.

var rootItem = database.GetItem("ITEM_PATH");
var indexable = new SitecoreIndexableItem(rootItem);

using (var context = ContentSearchManager.GetIndex(indexable).CreateSearchContext())
{
    var query = context.GetQueryable<SearchResultItem>()
        .Filter(x => x.TemplateId == new ID("TEMPLATE_ID_FOR_SEARCH"));

    var resultItems = query?.Select(s => s.GetItem()).ToList() ?? new List<Item>();

    // YOUR LOGIC WITH THE RESULTS
}

Now, let's look at scenarios where additional attributes need to be analyzed in the query conditional.

Solr Search with specific attributes

To search for items using a specific field, we can create a BaseSearchResultItem inheritance to define the solr attributes we want to use to establish the search query.

For example, below we will see an example where we have items from authors, and we want to search for a particular one according to its ID.

public class AuthorSearchResultItem : BaseSearchResultItem
{
    [IndexField("author_id_s")]
    public string AuthorId { get; set; }

    [IndexField("author_name_s")]
    public string AuthorName { get; set; }
}

This is an example of a search.

var db = Sitecore.Context.Database;
var locationRoot = db.GetItem("ITEM_PATH");

var indexable = new SitecoreIndexableItem(locationRoot);

using (var context = ContentSearchManager.GetIndex(indexable).CreateSearchContext())
{
    var query = context.GetQueryable<AuthorSearchResultItem>()
        .Filter(x => x.TemplateId == new ID("TEMPLATE_ID_FOR_SEARCH"))
        .Filter(x => x.AuthorId == authorId);

    var authorResult = query.FirstOrDefault();
    var authorItem = author.GetItem();
}

Let's look at an additional case.

Solr Search with predicates

To establish specific conditionals of type AND or OR, we can use predicates when establishing the query to obtain the search results.

var predicateOr = PredicateBuilder.True<SearchResultItem>();
var predicateAnd = PredicateBuilder.True<SearchResultItem>();

predicateAnd = predicateAnd.And(s => s.AuthorId == authorId);

using (var context = ContentSearchManager.GetIndex(indexable).CreateSearchContext())
{
    var query = context.GetQueryable<SearchResultItem>()
        .Where(predicateAnd)
        .Where(predicateOr)
        .Take(10);

    var results = query.GetResults();
}

And that's it! With the previous examples, we can perform a Solr search in Sitecore to obtain a list of items or a particular item, considering conditionals and additional parameters in a query.

Thanks for reading!

If you have any questions or ideas in mind, it'll be a pleasure to be able to be in communication with you, and together exchange knowledge with each other.

ASP.NET Core 9.0 Hosting Recommendation

One of the most important things when choosing a good ASP.NET Core 9.0 hosting is the feature and reliability. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core, their servers are optimized for PHP web applications. The performance and the uptime of the hosting service are excellent and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. 

At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core. And the engineers do regular maintenance and monitoring works to assure its Orchard hosting are security and always up.

0 comments:

Post a Comment