< Return to Feed
Mike Matthes - 07.15.2015

Sitecore 8 Basic Search

The Sitecore search API has a lot of functionality, but it's not completely clear on how to get started.  Here is the most basic example.

1. Import the following Sitecore dll's into your Visual Studio project.  You can find each file in your Sitecore/Website/bin/ directory.

  • Lucene.Net.dll
  • Sitecore.ContentSearch.dll
  • Sitecore.ContentSearch.Linq.dll


2. Add these using statements to your code file

using Sitecore.Data.Items;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Linq;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.ContentSearch.Linq.Utilities;

3. Add this code to query the index and get the first item.  Note: this query doesn't search a specific field.  Instead it does a general search of all fields.

string query = "sitecore";
if (!string.IsNullOrEmpty(query))
    using (var context = ContentSearchManager.GetIndex("simpleblog_content_index").CreateSearchContext())
        var predicate = PredicateBuilder.True<SearchResultItem>();
        predicate = predicate.Or(x => x.Content.Contains(query));

        var results = context.GetQueryable<SearchResultItem>().Where(predicate).GetResults();

        int count = results.Count();
        Item theItem = results.First().Document.GetItem();