EPiServer  /  Find April 16, 2012

Cool new features in the Truffler .NET API

NOTE Since writing this post the company behind Truffler, 200OK, has been sold to EPiServer and the product Truffler has been replaced by EPiServer Find. Most of the content of this blog post is however applicable to EPiServer Find as well. For questions regarding Find, be sure to visit the forum on EPiServer World.

Geo facets, a method for search-as-you-type and several other powerful new methods in the .NET API.

It’s been a while since I last blogged about Truffler. We’ve been kind of busy with sales, supporting customers and building some really cool new major features. But that doesn’t mean that our .NET API hasn’t evolved. On the contrary, it has been enhanced with quite a few new features, often driven by demand from developers (thanks!). Some of my favorite new features include geographical distance facets, the Include method and the AnyWordBeginsWith method.

Geographical distance facets

As I’ve previously written about ElasticSearch and the Truffler .NET API  has some geo search capabilities that outshines most other search solutions. Just check out the map example ;-). Now however being able to filter and sort by geographical distance as well as filter by geographical bounding boxes has been complemented with support for geographical distance facets.

geographical-distance-facetA geographical distance facet requires a from-location and a number of ranges (as in X kilometers from and Y kilometers to) and returns the number of documents that fall within each of those ranges. To check out how it works in detail be sure to read the documentation, but here’s a snippet of example code that could be used to build filtering functionality such as the one in the image to the right here (in which I used EPiServer CMS’ geolocation API to find the users location).

client.Search<TravelDestination>()
  .GeoDistanceFacetFor(x => x.Coordinates, 
    UserLocation,
    new NumericRange { From = 0, To = 1000 },
    new NumericRange { From = 0, To = 2500 },
    new NumericRange { From = 0, To = 5000 },
    new NumericRange { From = 0, To = 10000 },
    new NumericRange { From = 0, To = 25000 })

The AnyWordBeginsWith filter method

Truffler has always supported filtering on string values by exact match, case insensitive  exact matches as well as by matching the beginning of strings. The AnyWordBeginsWith method however filters by matching the beginning of any word in a string. This of course comes in pretty handy when building autocomplete or search-as-you-type-functionality.

client.Search<BlogPost>()
  .Filter(x => x.Title.AnyWordBeginsWith("Ban"));

An important word of warning though, with great power comes great responsibility and this method can put even the fastest of search engines under strain if used on long texts so we only recommend it’s usage on titles and other short string values. Luckily, in cases such as autocomplete and the like it’s only short string values we’d like to use it on anyways :-)

The Include method

Another of my favorites is the Include method. This method has a similar signature as the BoostMatching method that I’ve previously written about. It too accepts a filter expression as an argument, meaning that we can pass it however complex criteria as we like. But instead of boosting hits that match a filter it ensures that documents that match it is included in the search result, even if they don’t match the free text search query and/or previously applied filters.

What’s it good for? Well, combine it with the For method for free text search and pass in a filter that uses the AnyWordBeginsWith method and you’ve got yourself a pretty cool search-as-you-type-functionality in about four lines of code ;-)

var q = "Bar";

searchResult = client.Search<BlogPost>()
  .For(q)
  .Include(x => x.Title.AnyWordBeginsWith(q), 1)
  .GetResult();

Facets for EPiServer categories with ease

Last but not least I’d like to mention a little utility method that we’ve added to our EPiServer CMS integration; the CategoriesFacet method. This little gem adds a request for a facet for EPiServer categories to a search request. Once we execute the search request and get a result a method with the same name can be used to extract an IEnumerable<CategoryCount>. Each CategoryCount contains a category and the number of pages in that category that matched the search query.

var result = EPiSearchClient.Instance
    .Search<PageData>()
    .For("Banana")
    .CategoriesFacet()
    .GetPagesResult();

foreach (var categoryCount in result.CategoriesFacet())
{
    var categoryName = categoryCount.Category.Name;
    var count = categoryCount.Count;
}

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Joel Abrahamsson

Joel Abrahamsson

I'm a passionate web developer and systems architect living in Stockholm, Sweden. I work as CTO for a large media site and enjoy developing with all technologies, especially .NET, Node.js, and ElasticSearch. Read more

Comments

comments powered by Disqus

My book

Want a structured way to learn EPiServer 7 development? Check out my book on Leanpub!

More about EPiServer Find