public
Created

Running Picky e.g. in a single script.

  • Download Gist
picky_in_a_script.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Possible since Picky 3.2.0.
#
require 'picky'
 
include Picky
 
Thing = Struct.new :id, :name
 
index = Index.new :test do
category :name, similarity: Similarity::DoubleMetaphone.new(3)
end
 
index.replace Thing.new(1, 'Picky')
index.replace Thing.new(2, 'Parslet')
 
things = Search.new(index) do
boost [:name] => +3
end
 
p things.search("Pick").ids
p things.search("Pic").to_hash
p things.search("Parsley~").allocations
# Or, replace the three last lines with:
#
require 'benchmark'

p Benchmark.measure { 1000.times { things.search("Picky") } }    # Exact, ... (  0.112894)
p Benchmark.measure { 1000.times { things.search("Pic") } }      # Partial, ... (  0.118742)
p Benchmark.measure { 1000.times { things.search("Parsley~") } } # Similar, ... (  0.203989)
# Or even, replace them with.
#
class Proxy

  def initialize things
    @things = things
  end

  def search text
    @things.search(text).to_hash
  end

end

require 'msgpack/rpc'
svr = MessagePack::RPC::Server.new
svr.listen '127.0.0.1', 18800, Proxy.new(things)
svr.run

# Run it, and then open another file with
#
require 'msgpack/rpc'
c = MessagePack::RPC::Client.new '127.0.0.1', 18800

require 'benchmark'
p Benchmark.realtime { 1000.times { c.call :search, "Picky" } } # => 0.231717

# And run that :)
#

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.