This content originally appeared on DEV Community and was authored by Daniel Gomez
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
}
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; }
}
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();
}
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.
X / LinkedIn - esdanielgomez.com
This content originally appeared on DEV Community and was authored by Daniel Gomez
Daniel Gomez | Sciencx (2024-10-07T21:42:24+00:00) Solr Search in Sitecore. Retrieved from https://www.scien.cx/2024/10/07/solr-search-in-sitecore/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.