Queries++ in RavenDBFacets of information
RavenDB has a lot of functionality that is available just underneath the surface. In addition to just finding documents, you can use RavenDB to find a lot more about what is going on in your database. This series of posts is aimed at exposing some of the more fun things that you can do with RavenDB that you are probably not aware of.
One of the those things is the idea of not just querying for information, but also querying for the facets of the results. This can be very useful if you are likely to search for something that would return a lot of results and you want to quickly filter these out without having the user do a lot of trial an error. This is one of those cases where it is much easier to explain what is going on with a picture.
Imagine that you are searching for a phone. You might have a good idea what you are looking for a phone on eBay. I just did that and it gave me over 300 thousands results. The problem is that if I actually want to buy one of them, I’m not going to scroll through however many pages of product listings. I need a way to quickly narrow down the selection, and facets allow me to do that, as you can see in the image. Each of these is a facet and I can filter out things so only the stuff that I’m interested in will be shown, allowing me to quickly make a decision (and purchase).
Using the sample dataset in RavenDB, we’ll explore how we can run faceted searches in RavenDB. First, we’ll define the “Products/Search” index:
Using this index, we can now ask RavenDB to give us the facets from this dataset, like so:
This will give us the following results:
And we can inspect each of them in turn:
These are easy, because they give us the count of matching products for each category and supplier. Of more interest to us is the Prices facet.
And here we can see how we sliced and diced the results. We can narrow things further with the user’s choices, of course, let’s check out this query:
Which gives us the following Prices facet:
This means that you can, in a very short order, produce really cool search behavior for your users.