Reducing Overhead

one filter at a time

Fork me on GitHub

Description

Every one of the filters described above can take an optional final parameter in the constructor of true or false, specifying whether the filter created should be cached by the Query API or not. This is very useful for faceted search. For example, suppose you are searching an index of news articles where the text property contains the term "Obama". You would normally create a filter as such:

Examples

var filter = new Filter({'text':'Obama'});

and pass it to the Query API. Now, suppose you want to search all articles containing the text "Obama" and seperate your results by state (e.g. all "Obama" articles in VA, all "Obama" articles in CA, all "Obama" articles in NY, etc.). That means you would have to execute 50 queries with filters such as:

Examples

var filter = new AndFilter(new Filter({'text':'Obama'}), new Filter({'state':'fill_in_chosen_state_here'}));

Executing queries on 50 of these filters can be expensive, but if you notice, all 50 of these AndFilters will have one component in common: new Filter({'text':'Obama'}). So why not cache the result of that and use it for all 50 filters to minimize the need for requerying? Thats exactly the concept behind Cached Filters. What we will do now is create a cached filter for the "Obama" text search by specifying a boolean value of true for the final argument to the filter constructor:

Examples

var cached_filter = new Filter({'text':'Obama'}, true);

Now, we can use this cached_filter object to create the AndFilters necessary to narrow the results down by state. So, to get all "Obama" articles in the states of VA, CA, and NY:

Examples

var va_filter = new AndFilter(cached_filter, new Filter({'state':'VA'}));
var ca_filter = new AndFilter(cached_filter, new Filter({'state':'CA'}));
var ny_filter = new AndFilter(cached_filter, new Filter({'state':'NY'}));

This is how faceting is achieved with efficience and optimal performance within the stack.