These are a few simple examples that will get you into Picky quickly.
gem install picky
# Copy this into a Ruby file "objectsearch.rb", then:
# ruby objectsearch.rb
require 'picky'
# Create an index which is saved into './index' when you
# run index.dump(). Note that :id is implied - every input
# object must supply an :id!
#
index = Picky::Index.new :people do
category :age
category :name
end
# Define a data input class. Any object that responds to
# :id, :age, :name can be added to the index.
#
Person = Struct.new :id, :age, :name
# Add some data objects to the index.
# IDs can be any unique string or integer.
#
index.add Person.new(1, 34, 'Florian is the author of picky')
index.add Person.new(2, 77, 'Floris is related to Florian')
# Create a search interface object.
#
people = Picky::Search.new index
# Do a search and remember the results.
#
results = people.search 'floris'
# Show the results.
#
p results.ids # => [2]
gem install picky
Copy the CSV data into ./people.csv:
id,name,age
1,Florian Hanke,37
2,Kaspar Schiess,36
Then run this code:
# Copy this into a Ruby file "csvsearch.rb", then:
# ruby csvsearch.rb
require 'picky'
require 'csv'
require 'ostruct'
require 'fileutils'
# Prepare CSV data.
#
options = {
headers: true,
header_converters: ->(header) { header.to_sym }
}
csv = CSV.open('./people.csv', options)
.to_a
.map { |row| OpenStruct.new row.to_hash }
# Define an index.
#
data = Picky::Index.new :people do
source { csv }
category :age
category :name
end
# The index is saved into './index'.
#
data.index
# Create a search interface object.
#
people = Picky::Search.new data
# Do a search and remember the results.
#
results = people.search 'age:36'
# Show the results.
#
p results.ids # => ["2"]
gem install picky
Copy the poem "The Raven" by E. A. Poe into ./story.txt. Here is the ending (that's enough to run the example):
And the raven, never flitting, still is sitting, still is sitting On the pallid bust of Pallas just above my chamber door; And his eyes have all the seeming of a demon's that is dreaming, And the lamp-light o'er him streaming throws his shadow on the floor; And my soul from out that shadow that lies floating on the floor Shall be lifted - nevermore!
Then run this code:
# Copy this into a Ruby file "document_search.rb", then:
# ruby document_search.rb
require 'picky'
# Define an index.
#
data = Picky::Index.new :people do
# Only keep alpha/blank characters.
indexing removes_characters: /[^\p{Alpha}\p{Blank}]/i
# Only index full words.
category :text, partial: Picky::Partial::None.new
end
# Define a data input class. Any object that responds to
# :id, :age, :name can be added to the index.
#
Document = Struct.new :id, :text
# Add some data objects to the index.
# IDs can be any unique string or integer.
#
File.open 'story.txt' do |story|
data.add Document.new(1, story.read)
end
# Create a search interface object.
#
people = Picky::Search.new data
# Do two searches and remember the results.
#
found = people.search 'nevermore'
only_full_words = people.search 'nevermor'
not_found = people.search 'peter'
# Show the results.
#
p found.ids # => [1]
p only_full_words.ids # => []
p not_found.ids # => []
Adding to an in-memory index using
Index#add
will not automatically
store the index data on permanent storage.
To write the index to disk, call
Index#dump
.
To load the index from disk, call
Index#load
.
gem install picky
Then run this code:
# Copy this into a Ruby file "dump_load_search.rb", then:
# ruby dump_load_search.rb
require 'fileutils'
require 'picky'
# Make Picky be quiet.
#
Picky.logger = Picky::Loggers::Silent.new
# Create an index.
# Note that :id is implied - every input
# object must supply an :id!
#
index = Picky::Index.new :people do
category :age
category :name
end
# Define a data input class. Any object that responds to
# :id, :age, :name can be added to the index.
#
Person = Struct.new :id, :age, :name
# Add some data objects to the index.
# IDs can be any unique string or integer.
#
index.add Person.new(1, 34, 'Florian is the author of picky')
index.add Person.new(2, 77, 'Floris is related to Florian')
# Create a search interface object to search the index.
#
people = Picky::Search.new index
# The index data is saved into './index' when you
# run index.dump().
# But you still find results.
#
index.dump
p people.search('flori').ids # => [2, 1]
# Clearing the index will empty it in memory.
#
index.clear
p people.search('flori').ids # => []
# Loading the index will fill the index again.
#
index.load
p people.search('flori').ids # => [2, 1]
gem install picky
Then run this code:
# Copy this into a Ruby file "inspection.rb", then:
# ruby inspection.rb
require 'picky'
# Create an index which is saved into './index' when you
# run index.dump(). Note that :id is implied - every input
# object must supply an :id!
#
index = Picky::Index.new :people do
category :age
category :name
end
# Define a data input class. Any object that responds to
# :id, :age, :name can be added to the index.
#
Person = Struct.new :id, :age, :name
# Add some data objects to the index.
# IDs can be any unique string or integer.
#
index.add Person.new(1, 34, 'Florian is the author of picky')
index.add Person.new(2, 77, 'Floris is related to Florian')
# Look at pieces of the index.
#
puts index[:name].exact.inverted
puts index[:name].exact.weights
puts index[:name].partial.inverted
puts index[:name].partial.weights