-
Notifications
You must be signed in to change notification settings - Fork 2
Scoping The Search
Sometimes you don’t want to search the entire index. Sometimes you just want to search part of the index, or limit your results to just certain kinds of entries.
The index includes information on what model each search result hit is for, so it’s not that hard to scope your search to only search one model, or a few models.
If you have several models indexed, but you only want to search one model FooThing
the easiest way is this:
results = FooThing.search_entries.find_results(query, page_size, page_number)
# or
results = IndexedSearch::Entry.by_modelid(FooThing.model_id).find_results(query, page_size, page_number)
If you have multiple models indexed, but you only want to search two models FooThing
and BarThing
the easiest way is this:
model_ids = [FooThing.model_id, BarThing.model_id]
results = IndexedSearch::Entry.by_modelid(model_ids).find_results(query, page_size, page_number)
At some point I will likely add a helper method to make this a bit shorter, but for now that’s what should work… :P
If you have multiple models indexed, and you only want to search certain rows of one model:
# use valium gem, faster loading of ids
require 'valium'
# find everything with a certain value
foo_thing_ids = FooThing.where(:something => 'has this value').value_of(:id)
results = FooThing.search_entries.by_rowid(foo_thing_ids).find_results(query, page_size, page_number)
# or find everything _except_ a certain value
except_foo_thing_ids = FooThing.where(:something => 'should not have this value').value_of(:id)
results = FooThing.search_entries.not_rowids(except_foo_thing_ids).find_results(query, page_size, page_number)
This isn’t really optimal if you have a lot of ids… I’ll probably add some way to add extra columns to the index someday to accommodate that better.
Do not loop through and throw away entries after your search. This is wasteful of resources, and messes up the pagination.