Search code examples
sitecoresitecore7sitecore7.2

sitecore How to search once we have lucene index


After searching and reading a lot from Internet I manage create an Index using Lucene. My index name is: my_text_index. I have used Luke and I can see the index is created now with some data inside it (title of the items).

Now I have no idea at all how I can use this index to search using Sitecore API.

It would be very nice of you guys if you can write some steps for a beginner like me about "How to search from index in Sitecore".

Thanks!


Solution

  • There are lot of tutorials and guides in the Internet about Sitecore search. It's very similar in Sitecore 7 and Sitecore 8 so you can use both of them.

    First thing you should check is Sitecore documentation: Developer's Guide to Item Buckets and Search (the most interesting part for me starts from chapter 5.3).

    In the shortcut, create a class for your items (it can inherit from Sitecore SearchResultItem class but it's not required if you want to handle standard Sitecore fields by yourself), e.g.:

    public class Person : SearchResultItem
    {
        [IndexField("firstname_t")]
        public string Firstname { get; set; }
        [IndexField("surname_t")]
        public string Surname { get; set; }
    }
    

    and use code like that to get the results:

    using (var context = ContentSearchManager.GetIndex("my_text_index").CreateSearchContext())
    {
         IQueryable<Person> query = context.GetQueryable<Person>().Where(p=> p.Firstname.Equals("John"));
    }
    

    And that's it. You don't need anything else to start using Sitecore Search API with Sitecore 7.

    And here is a really nice "Sitecore 7 Search - a quickstart guide" article