Skip to content

Commit

Permalink
+ independent recipes, + geolocation, + range/area/volumetric searches
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed Dec 30, 2011
1 parent e4df484 commit ce3d034
Show file tree
Hide file tree
Showing 33 changed files with 197 additions and 5 deletions.
2 changes: 2 additions & 0 deletions recipes/README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ h2. Basics
h2. Intermediate

* /searches (How to configure your search so it rocks)
* /range (How to use range queries)
* /geolocation (How to configure geosearches)

h2. Advanced

Expand Down
2 changes: 2 additions & 0 deletions recipes/basic/multi_category.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

# We want to search 2 indexes at once.
# One index uses a source, the indexes at realtime.
#
Expand Down
2 changes: 2 additions & 0 deletions recipes/basic/realtime_index.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

# Loading the Picky index without a source,
# but purely by adding (replacing) items.
#
Expand Down
2 changes: 2 additions & 0 deletions recipes/basic/static_index.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

# An example where you give the Picky index
# an #each source.
#
Expand Down
2 changes: 2 additions & 0 deletions recipes/boosting/customized.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first
category :last
Expand Down
2 changes: 2 additions & 0 deletions recipes/boosting/standard.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first
category :last
Expand Down
4 changes: 4 additions & 0 deletions recipes/geolocation/README.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
h1. Geolocation Categories

* basic.rb
* multi_radius.rb (multiple radii)
22 changes: 22 additions & 0 deletions recipes/geolocation/basic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require File.expand_path '../../../server/lib/picky', __FILE__

GeoCoords = Struct.new :id, :lat, :lng

data = Picky::Index.new :people do
# 1.0 is the radius in k around which to search.
# precision 1 is low precision (20% error margin), but fast
#
geo_categories :lat, :lng, 1000.0 # 1000kms radius
end
cities = Picky::Search.new data

data.replace GeoCoords.new(1, -37.813611, 144.963056) # Melbourne
data.replace GeoCoords.new(2, -33.859972, 151.211111) # Sydney
data.replace GeoCoords.new(3, 47.366667, 8.55) # Zurich
data.replace GeoCoords.new(4, 41.9, 12.5) # Rome

# Picky just returns results in range, not ordered
# by distance. Usually that is fine for showing results
# in a graph or on a map.
#
fail __FILE__ unless cities.search('lat:-33.85 lng:150.2').ids == [2, 1]
29 changes: 29 additions & 0 deletions recipes/geolocation/multi_radius.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require File.expand_path '../../../server/lib/picky', __FILE__

GeoCoords = Struct.new :id, :lat, :lng

data = Picky::Index.new :people do
# 1.0 is the radius in k around which to search.
# precision 1 is low precision (20% error margin), but fast
#
geo_categories :lat1, :lng1, 1.0, :lat_from => :lat, :lng_from => :lng
geo_categories :lat10, :lng10, 10.0, :lat_from => :lat, :lng_from => :lng
geo_categories :lat100, :lng100, 100.0, :lat_from => :lat, :lng_from => :lng
geo_categories :lat1000, :lng1000, 1000.0, :lat_from => :lat, :lng_from => :lng
end

data.replace GeoCoords.new(1, -37.813611, 144.963056) # Melbourne
data.replace GeoCoords.new(2, -33.859972, 151.211111) # Sydney
data.replace GeoCoords.new(3, 47.366667, 8.55) # Zurich
data.replace GeoCoords.new(4, 41.9, 12.5) # Rome

cities = Picky::Search.new data

# Picky just returns results in range, not ordered
# by distance. Usually that is fine for showing results
# in a graph or on a map.
#
fail __FILE__ unless cities.search('lat1:-33.85 lng1:150.2').ids == []
fail __FILE__ unless cities.search('lat10:-33.85 lng10:150.2').ids == []
fail __FILE__ unless cities.search('lat100:-33.85 lng100:150.2').ids == [2]
fail __FILE__ unless cities.search('lat1000:-33.85 lng1000:150.2').ids == [2, 1]
2 changes: 2 additions & 0 deletions recipes/partial/customized.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

# Our special partializer only allows partials
# of even length.
#
Expand Down
2 changes: 2 additions & 0 deletions recipes/partial/infix.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first

Expand Down
2 changes: 2 additions & 0 deletions recipes/partial/postfix.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first

Expand Down
2 changes: 2 additions & 0 deletions recipes/partial/substring.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first

Expand Down
5 changes: 5 additions & 0 deletions recipes/range/README.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
h1. Range Categories

* one_dimensional.rb (Value range search)
* two_dimensional.rb (Area search)
* three_dimensional.rb (Volumetric search)
27 changes: 27 additions & 0 deletions recipes/range/one_dimensional.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require File.expand_path '../../../server/lib/picky', __FILE__

OneD = Struct.new :id, :value

data = Picky::Index.new :people do
# 1.0 is the range around which to search
# precision 1 is low precision (20% error margin), but fast
#
ranged_category :value, 1.0, precision: 1
end

data.replace OneD.new(1, 3.2)
data.replace OneD.new(2, 1.8)
data.replace OneD.new(3, 9.7)
data.replace OneD.new(4, 4.1)

one_d = Picky::Search.new data

results = one_d.search '3.1'

# p results.allocations

# Picky just returns results in range, not ordered
# by distance. Usually that is fine for showing results
# in a graph or on a map.
#
fail __FILE__ unless results.ids == [4, 1]
32 changes: 32 additions & 0 deletions recipes/range/three_dimensional.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require File.expand_path '../../../server/lib/picky', __FILE__

ThreeD = Struct.new :id, :x, :y, :z

data = Picky::Index.new :people do
# 1.0 is the range around which to search
# precision 1 is low precision (20% error margin), but fast
#
ranged_category :x, 1.0, precision: 1
ranged_category :y, 1.0, precision: 1
ranged_category :z, 1.0, precision: 1
end

data.replace ThreeD.new(1, 3.2, 2.8, -8.5)
data.replace ThreeD.new(2, 1.8, 0.4, 13.7)
data.replace ThreeD.new(3, 9.7, 11.2, -7.0)
data.replace ThreeD.new(4, 4.1, 2.2, -7.5)

three_d = Picky::Search.new data

# Since the x, y and z coordinates are not
# disjunct, we have to specify which is which.
#
results = three_d.search 'x:3.1 y:2.4 z:-8.0'

# p results.allocations

# Picky just returns results in range, not ordered
# by distance. Usually that is fine for showing results
# in a graph or on a map.
#
fail __FILE__ unless results.ids == [4, 1]
31 changes: 31 additions & 0 deletions recipes/range/two_dimensional.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require File.expand_path '../../../server/lib/picky', __FILE__

TwoD = Struct.new :id, :x, :y

data = Picky::Index.new :people do
# 1.0 is the range around which to search
# precision 1 is low precision (20% error margin), but fast
#
ranged_category :x, 1.0, precision: 1
ranged_category :y, 1.0, precision: 1
end

data.replace TwoD.new(1, 3.2, 2.8)
data.replace TwoD.new(2, 1.8, 0.4)
data.replace TwoD.new(3, 9.7, 11.2)
data.replace TwoD.new(4, 4.1, 2.2)

two_d = Picky::Search.new data

# Since the x and y coordinates are not
# disjunct, we have to specify which is which.
#
results = two_d.search 'x:3.1 y:2.4'

# p results.allocations

# Picky just returns results in range, not ordered
# by distance. Usually that is fine for showing results
# in a graph or on a map.
#
fail __FILE__ unless results.ids == [4, 1]
2 changes: 0 additions & 2 deletions recipes/run.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
Person = Struct.new :id, :first, :last

Dir['**/*.rb'].each { |file| require File.expand_path "../#{file}", __FILE__ }
2 changes: 2 additions & 0 deletions recipes/searches/ignore.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first
category :last
Expand Down
2 changes: 2 additions & 0 deletions recipes/searches/ignore_unassigned_tokens.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first
category :last
Expand Down
2 changes: 2 additions & 0 deletions recipes/searches/max_allocations.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first
category :last
Expand Down
2 changes: 2 additions & 0 deletions recipes/searches/terminate_early.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first
category :last
Expand Down
2 changes: 2 additions & 0 deletions recipes/similarity/customized.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

# Our similarizer just encodes text as if
# it had just vowels.
#
Expand Down
2 changes: 2 additions & 0 deletions recipes/similarity/double_metaphone.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first
category :last, similarity: Picky::Similarity::Metaphone.new
Expand Down
2 changes: 2 additions & 0 deletions recipes/similarity/metaphone.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first
category :last, similarity: Picky::Similarity::Metaphone.new
Expand Down
2 changes: 2 additions & 0 deletions recipes/similarity/soundex.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first
category :last, similarity: Picky::Similarity::Soundex.new
Expand Down
2 changes: 2 additions & 0 deletions recipes/weight/constant.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first
category :last, weight: Picky::Weights::Constant.new(3.0)
Expand Down
2 changes: 2 additions & 0 deletions recipes/weight/customized.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

# Our customized weighter for the last name.
# We weigh results higher that are only found once.
#
Expand Down
2 changes: 2 additions & 0 deletions recipes/weight/dynamic.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first

Expand Down
2 changes: 2 additions & 0 deletions recipes/weight/logarithmic.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.expand_path '../../../server/lib/picky', __FILE__

Person = Struct.new :id, :first, :last

data = Picky::Index.new :people do
category :first

Expand Down
2 changes: 1 addition & 1 deletion server/lib/picky/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def category category_name, options = {}
#
# === Options
# * precision: Default is 1 (20% error margin, very fast), up to 5 (5% error margin, slower) makes sense.
# * anchor: Where to anchor the geo grid.
# * anchor: Where to anchor the grid.
# * ... all options of #category.
#
def ranged_category category_name, range, options = {}
Expand Down
2 changes: 1 addition & 1 deletion server/lib/picky/query/allocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Allocation # :nodoc:all
#
#
def initialize index, combinations
@combinations = combinations
@combinations = combinations

# Could this be rewritten?
#
Expand Down
2 changes: 1 addition & 1 deletion server/spec/functional/regression_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'spec_helper'

describe "Regression" do

it 'does not get confused' do
index = Picky::Index.new :dynamic_weights do
category :text1
Expand Down

0 comments on commit ce3d034

Please sign in to comment.