1

I have a requirement to implement a custom search box and pull search results based on scope and then present the search results by combining components created using two templates.

I am trying to use the Search method in ISearchService -

IEnumerable<Item> results = new List<Item>();
results = searchService.Search("", scopeItem.ID.ToString(), Sitecore.Context.Language.ToString());

The Search method is expecting a string query as first argument, which if I pass as my search term is not giving me any results. But if I query for an empty string, I am getting all items from the Scope in the result set. I am doing string operations to see if any of the fields contains my search term.

Looking for a better way to implement this something like -

results = searchService.Search(searchTerm, scopeItem.ID.ToString(), Sitecore.Context.Language.ToString());

any thoughts?

1

I would suggest using SearchService to build query like this:

IQueryable<ContentPage> query = SearchService.GetQuery(new SearchQueryModel
{
    Coordinates = model.Coordinates, //not needed if you are not using geo-search
    ItemID = model.ItemID,
    Languages = model.Languages,
    Query = model.Query,
    ScopesIDs = model.ScopesIDs,
    Site = model.Site
}, out indexName);

(implementation took for SXA code - model which you see is not required, you can pass params in a different way)

and then just use that query to pull the items:

IEnumerable<Item> items = query.Select(r => r.GetItem())

This is how it works in the SXA search.

But if you still want to go with Search(QueryModel model) method if SearchService then keep in mind it's using GetQuery method inside anyway. We are using this Search method explicitly only in one of out JSON renderings. Here is an example usage:

SearchService.Search(new QueryModel
{
    Query = HttpContext.Current.Request.Params["q"],
    Offset = Offset,
    PageSize = PageSize,
    Sortings = new List<string>(),
    Languages = new List<string>(),
    ScopesIDs = new List<ID>()
})
  • I tried this way and still getting no search results - IEnumerable<Item> results = searchService.Search(Request.QueryString["searchTerm"], scopeItem.ID.ToString(), Sitecore.Context.Language.ToString()); Not sure if this is the right way of instantiating a query object (1st argument to the method call here) – Bharath Oct 4 '18 at 10:45
  • My examples are form the latest code. I don't know which version of SXA you are using. – Dawid Rutkowski Oct 4 '18 at 12:31
  • We are using Sitecore 9.0.1 + SXA 1.7.1 – Bharath Oct 5 '18 at 4:55
  • So you should have almost the same code. And it should work, I don't remember any issues related to such behaviour as you described. – Dawid Rutkowski Oct 6 '18 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.