Skip to content

Instantly share code, notes, and snippets.

@floere
Created November 6, 2012 21:42
Show Gist options
  • Save floere/4027781 to your computer and use it in GitHub Desktop.
Save floere/4027781 to your computer and use it in GitHub Desktop.
Infix search example (copy & paste)
require 'picky'
Person = Struct.new :id, :age, :name
data = Picky::Index.new :people do
category :age, partial: Picky::Partial::None.new
category :name, partial: Picky::Partial::Infix.new(min: 1, max: -1)
end
data.replace Person.new(1, 34, 'Florian')
data.replace Person.new(2, 77, 'Elfriede')
people = Picky::Search.new data
p people.search '34 f' # Finds 1 (0.00013s).
results = people.search 'f' # Finds 1 and 2 (0.00004s).
p results # For logging
p results.allocations # => Returns ids, weight etc.
@floere
Copy link
Author

floere commented Nov 6, 2012

You can also give the index a source:

data = Picky::Index.new :people do
  source { Person.order('name DESC') }
  category :age, partial: Picky::Partial::None.new
  category :name, partial: Picky::Partial::Infix.new(min: 1, max: -1)
end

And call Picky::Indexes.index, or data.index before searching. This will call #each on Person.order('name DESC') and index each thing it gets.

@glebm
Copy link

glebm commented Nov 7, 2012

Really cool, will try it out
🍺 to the octopus mastermind!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment