I have wanted to add search to this site for a while. This site is built on EPiServer and also uses PageTypeBuilder so I had previously toyed with the idea of the built in EPiServer search. However I never quite got round to it and wanted to learn something new (working with EPiServer and its features are part of the day job after all).

I learned about Truffler.net through all round coding/EPiServer guru Joel Abrahamsson so decided it was a great place to start when adding search to this site.

My objective was simple: Add Truffler to an EPiServer site (i.e. this site) and see how long it would take. I only wanted a simple "Google-like" simple site search using Truffler (I can always add more advanced search options later).

Fortunately there is already a Truffler integration for EPiServer that is available on the EPiServer Nuget feed (thanks Joel :)!).

The process was pretty simple:

  • Install the Truffer EPiServer CMS integration package from the EPiServer Nuget feed
  • Configure Truffler in web.config
  • Manualy run the "Truffler CMS indexing job" job to index existing pages
  • Write some search code (see below)
  • Wire into a template (I have missed this part out for brevity)

The search is faily rudamentary, taking advantage of the followig Truffler features:

The code for the search is below and you can see the results by searching for EPiServer on this site or by using the search box at the top right of this page:

IClient trufflerClient = EPiSearchClient.Instance;

string query = GetQueryFromQueryString();
var searchResults = trufflerClient.Search()
    .For(query)
    .InFields(x => x.PageName, x => x.IntroText, x => x.BodyText)
    .Select(x => new SearchResult
    {
        TitlePlain = x.PageName,
        Title = string.IsNullOrWhiteSpace(x.PageName.AsHighlighted()) ? x.PageName : x.PageName.AsHighlighted(new HighlightSpec { PreTag = "", PostTag = "" })
        ,
        Content =
            x.IntroText.AsHighlighted(
                new HighlightSpec
                {
                    FragmentSize = 100,
                    NumberOfFragments = 1,
                    PreTag = "",
                    PostTag = ""
                }) + " ... " +
            x.BodyText.AsHighlighted(
                new HighlightSpec
                {
                    FragmentSize = 100,
                    NumberOfFragments = 1,
                    PreTag = "",
                    PostTag = ""
                })
        ,
        PageID = x.PageLink.ID

    })
    .GetResult();

As you can see from the code I have a page type called BlogPageType which I could search on directly through Truffler. Bringing EPiServer, PageTypeBuilder and search together was incredibly simple. I can honestly say this was one of the quickest and easiest search integrations I have ever done.

Conclusion

The total time taken to add this to this site was two hours. This shows how a powerful search like Truffler can be added very quickly.

It could also allow me to add more advanced features like Facets and More Like functionality later.

This was a learning exercise for me and it allowed me to add a Truffler search to this site quickly and easily so will certainly be looking at this for use on other projects.

Feedback

I'd be happy to hear any feedback on the comments below or @davidknipe


Comments