simplest way to search a lucene Index in Sitecore

this is the simplest code to search a lucene index in Sitecore…

 using (IndexSearchContext indexSearchContext = SearchManager.GetIndex("CustomIndex" ).CreateSearchContext())
            {
                var query = new BooleanQuery();
                query.Add(
                    new TermQuery( new Term( FieldNameLegacyId, legacyId)),
                    BooleanClause.Occur .MUST);
                SearchHits hits = indexSearchContext.Search(query, int.MaxValue);
                List< Item> results =
                    hits.FetchResults(0, int.MaxValue)
                        .Select(r => r.GetObject< Item>())
                        .Where(c => c != null && c.Parent != null)
                        .ToList();

                if (results.Any())
                {
                    return results.First().ID.ToGuid().ToString( "N");
                }
            }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s