DEV Community

Daniel Gomez
Daniel Gomez

Posted on

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

Top comments (0)