Liam Liam - 1 year ago 95
C# Question

SearchManager vs ContentSearchManager?

I've nerver really delved into the search capabilites of sitecore, up until now I've always used what the previous developer used. I was looking at an issue where certain predicates didn't seem to be woking in the below piece of code:

public IEnumerable<IndexedEvent> SearchItems(Expression<Func<IndexedEvent, bool>> predicate)
using (IProviderSearchContext _context = ContentSearchManager.GetIndex(indexName).CreateSearchContext())
IEnumerable<IndexedEvent> results = _context

return results;


I didn't write the above, I'm just using it.

I was looking into my issue when I came across this example question Very basic usage of sitecore search which included the code:

// use id of from the index configuration
using (IndexSearchContext indexSearchContext = SearchManager.GetIndex("my-custom-index").CreateSearchContext())
// MatchAllDocsQuery will return everything. Use proper query from the link below
SearchHits hits = indexSearchContext.Search(new MatchAllDocsQuery(), int.MaxValue);
// Get Sitecore items from the results of the query
List<Item> items = hits.FetchResults(0, int.MaxValue).Select(result => result.GetObject<Item>()).Where(item => item != null).ToList();

Now this seems to use a totally different method to query the index,
where as my code (not written by me) uses
. I can't find any documentation on either and they reside in totally different assemblies.

So it begs the question, when should I use
and when should I use
? Is there any fundermental difference here or is it just two ways of achiving the same nett result?

Answer Source

The question and code you are referring to with the SearchManager and IndexSearchContext is from Sitecore 6. The code with the ContentSearchManager and IProviderSearchContext is for Sitecore 7 or 8 (well, 7+ that is).

So, if your code is for Sitecore8 as your tag and code example suggests, the ContentSearchManager is the way to go.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download