-
Notifications
You must be signed in to change notification settings - Fork 49
Searches Configuration
Indexes alone cannot be searched. You need a Search
object and give it all indexes you want to search: Search.new(*indexes_to_search)
Searches operate on Indexes.
books_index = Picky::Index.new :books do
# ...
end
books = Search.new books_index
books.search 'test'
You can search over an arbitrary number of indexes. Say, you have multiple indexes, one for books, one for dvds, one for music. You can combine them to search through them all with a single query!
books_index = index # ...
dvd_index = index # ...
music_index = index # ...
books = Search.new books_index, dvd_index, music_index
books.search 'test'
This will return results found in all three indexes.
Apart from a list of indexes, searches take options in their options block.
The options are currently:
- boost(hash_or_object): Define a number of combinations that should receive positive or negative weights (see example below).
-
searching(options_or_tokenizer): The Tokenizer Options to use. Either pass in a hash or an object that responds to
tokenize(text)
and returns[ [token1, token2], [original1, original2] ]
. - max_allocations(amount): The maximum number of allocations to calculate.
- terminate_early(extra_allocations = 0): Only calculate enough allocations for the ids. Good for when you just need the result ids and want to speed up the search a little.
- ignore_unassigned_tokens: If a token cannot be assigned to a category, simply ignore it. As opposed to returning 0 results because the one token cannot be found in any category.
Call them as methods in the block:
books = Search.new books_index do
searching splits_text_on: /\s\_/,
max_words: 4
terminate_early
end
books = Search.new books_index do
boost [:author] => 6, # Eg. 'Goethe'. Note that this only boosts on a single word.
[:title, :author] => 5, # Eg. 'hobbit tolkien'
[:author, :year] => 2 # Eg. 'orwell 1948'
end
Giving
[:author] => 6
means that if results are found where Picky thinks that one or all search terms are in the title, it is weighed by 6 (a lot) higher.
[:title, :author] => 5
will add 5 to the weight, if Picky finds a title followed by an author, e.g. “ulysses joyce”. If it finds an author followed by a title, 5 points will not be added.
The order is important!
So for example you can give more weight to [:street, :streetnumber]
, but subtract points for [:streetnumber, :street]
, an unlikely order of search terms.