1

I'm working on the site search module where the user searches for a particular item.

Currently, the site searches and matches only the item name.

But I'm trying for searching the keyword in item fields also(If any field contains the search value that one also needs to return).

How I achieve this? Is there any API available for this?

Attaching the code.

 var Index = ContentSearchManager.GetIndex("sitecore_" + IndexName + "_index");
 using (var context = Index.CreateSearchContext())
 {
    var allItems = context.GetQueryable<SearchResultItem>().ToList();
    var searchResults = allItems.Where(x => x.Path.ToLower().Contains(pathToBeSearch) && x.Name.ToLower().Contains(searchkey.ToLower())).ToList();
 }
5
  • 2
    I could be mistaken but your code appears to return all documents from the search engine before applying any filtering criteria. Can you reference some documentation links from Sitecore that helped you? I suspect their samples will get you closer to what you need. Mar 30, 2020 at 4:52
  • I tried in the Sitecore developer course material
    – Rajasekar
    Mar 30, 2020 at 4:59
  • 1
    I don't think you need an allItems variable. If I recall correctly using ToList will return all the items, this would be wasteful in your case. Solr should be doing the filtering but as you have it the web server is receiving results and tossing most of it out. Mar 30, 2020 at 14:38
  • 1
    @Rajasekar Michael is correct, calling ToList() on the IQueryable will invoke the search call at that point, and then your subsequent calls will be doing standard LINQ operations on a giant in-memory list. You should be doing the ToList() AFTER your LINQ statements so that they get translated to Lucene/SOLR statements. Either you copied from the Sitecore coursebook incorrectly, or the coursebook is incorrect. Mar 30, 2020 at 19:56
  • thanks. I updated my code.
    – Rajasekar
    Mar 31, 2020 at 3:41

1 Answer 1

1

You can search by any field using out of the box functionality. SearchResultItem contains Content field. This field is a computed field and accumulate all text from each item text fields. I adapted your code with one additional condition. See below:

  var searchResults = context.GetQueryable<SearchResultItem>()
.Where(x => (x.Content.Contains(searchkey) || x.Name.Contains(searchkey)) && x.Path.Contains(pathToBeSearch))     
.Where(x => x.Language == ConfigurationItem.Language.Name)     
.Where(x => !excludeTempltes.Contains(x.TemplateName))    
.GetResults().Hits.Select(h => h.Document).ToList();

At the same time, if your site is build using rendering data sources what means that the item content stores not only in the item itself, you might need to search by text in item data sources as well. In this case, you can extend the SearchResultItem.Content field with any texts you want.

4
  • I'm getting null in the Content Field
    – Rajasekar
    Mar 30, 2020 at 8:19
  • 1
    @Rajasekar right! You can’t get its value. This field is non-storage type. You can use it for search. Mar 30, 2020 at 9:21
  • 1
    The reason it's null is because you are calling doing ToList before your query statements, which means that your query is executing in-memory, not against your search index. If you move your ToList to the end, it should work. That said, I recommend you get more familiar with how this all works before just hacking at it. Mar 30, 2020 at 20:00
  • yes that's correct. Thnaks
    – Rajasekar
    Mar 31, 2020 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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