DEV Community

Daniel Gomez
Daniel Gomez

Posted on

3 1 2 1

Solr Search in Sitecore

When working on specific components in Sitecore that require listing or searching for particular items, conducting an in-depth search is essential. To achieve this, we will use Solr Search, which provides robust indexing and search capabilities tailored to meet our needs.

General search with Solr

Considering the path of the item from which we want to perform the search, whether it is the homepage, a folder or any particular page, we can create an instance of SitecoreIndexableItem, perform a query, and obtain the expected results of that search.

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
}
Enter fullscreen mode Exit fullscreen mode

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; }
}
Enter fullscreen mode Exit fullscreen mode

And this is the example of the 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();
}
Enter fullscreen mode Exit fullscreen mode

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();
}
Enter fullscreen mode Exit fullscreen mode

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.

X / LinkedIn - esdanielgomez.com

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay