Skip to content
This repository
Browse code

Merge branch 'aliases'

  • Loading branch information...
commit 0b52db179d76dcf4c482ef4a95b5a5949d0ed5fa 2 parents 8f8ab30 + 66a6a7e
Eric Waller authored May 25, 2011
2  README.markdown
Source Rendered
@@ -48,6 +48,8 @@ And here's the load command (Soulmate assumes redis is running locally on the de
48 48
 
49 49
     $ soulmate load venue --redis=redis://localhost:6379/0 < venues.json
50 50
 
  51
+You can also provide an array of strings under the `aliases` key that will also be added to the index for this item.
  52
+
51 53
 ### Querying for Data
52 54
 
53 55
 Once it's loaded, we can query this data by starting `soulmate-web`:
3  lib/soulmate/loader.rb
@@ -29,7 +29,8 @@ def load(items)
29 29
           # store the raw data in a separate key to reduce memory usage
30 30
           Soulmate.redis.hset(database, id, JSON.dump(item))
31 31
 
32  
-          prefixes_for_phrase(term).each do |p|
  32
+          phrase = ([term] + (item["aliases"] || [])).join(' ')
  33
+          prefixes_for_phrase(phrase).uniq.each do |p|
33 34
             Soulmate.redis.sadd(base, p) # remember this prefix in a master set
34 35
             Soulmate.redis.zadd("#{base}:#{p}", score, id) # store the id of this term in the index
35 36
           end
8  test/samples/venues.json
... ...
@@ -1,5 +1,5 @@
1  
-{"id":1,"term":"Dodger Stadium","score":85,"data":{"url":"\/dodger-stadium-tickets\/","subtitle":"Los Angeles, CA"}}
2  
-{"id":28,"term":"Angel Stadium","score":85,"data":{"url":"\/angel-stadium-tickets\/","subtitle":"Anaheim, CA"}}
3  
-{"id":30,"term":"Chase Field ","score":85,"data":{"url":"\/chase-field-tickets\/","subtitle":"Phoenix, AZ"}}
4  
-{"id":29,"term":"Sun Life Stadium","score":84,"data":{"url":"\/sun-life-stadium-tickets\/","subtitle":"Miami, FL"}}
  1
+{"id":1,"term":"Dodger Stadium","score":85,"data":{"url":"\/dodger-stadium-tickets\/","subtitle":"Los Angeles, CA"},"aliases":["Chavez Ravine"]}
  2
+{"id":28,"term":"Angel Stadium","score":85,"data":{"url":"\/angel-stadium-tickets\/","subtitle":"Anaheim, CA"},"aliases":["Edison International Field of Anaheim"]}
  3
+{"id":30,"term":"Chase Field ","score":85,"data":{"url":"\/chase-field-tickets\/","subtitle":"Phoenix, AZ"},"aliases":["Bank One Ballpark", "Bank One Stadium"]}
  4
+{"id":29,"term":"Sun Life Stadium","score":84,"data":{"url":"\/sun-life-stadium-tickets\/","subtitle":"Miami, FL"},"aliases":["Dolphins Stadium","Land Shark Stadium"]}
5 5
 {"id":2,"term":"Turner Field","score":83,"data":{"url":"\/turner-field-tickets\/","subtitle":"Atlanta, GA"}}
24  test/test_soulmate.rb
@@ -18,4 +18,28 @@ def test_integration_can_load_values_and_query
18 18
     assert_equal 3, results.size
19 19
     assert_equal 'Angel Stadium', results[0]['term']
20 20
   end
  21
+  
  22
+  def test_integration_can_load_values_and_query_via_aliases
  23
+    items = []
  24
+    venues = File.open(File.expand_path(File.dirname(__FILE__)) + '/samples/venues.json', "r")
  25
+    venues.each_line do |venue|
  26
+      items << JSON.parse(venue)
  27
+    end
  28
+    
  29
+    items_loaded = Soulmate::Loader.new('venues').load(items)
  30
+    
  31
+    assert_equal 5, items_loaded
  32
+    
  33
+    matcher = Soulmate::Matcher.new('venues')
  34
+    results = matcher.matches_for_term('land shark stadium', :limit => 5)
  35
+    
  36
+    assert_equal 1, results.size
  37
+    assert_equal 'Sun Life Stadium', results[0]['term']
  38
+    
  39
+    # Make sure we don't get dupes between aliases and the original term
  40
+    # this shouldn't happen due to Redis doing an intersect, but just in case!
  41
+    
  42
+    results = matcher.matches_for_term('stadium', :limit => 5)    
  43
+    assert_equal 4, results.size
  44
+  end
21 45
 end

0 notes on commit 0b52db1

Please sign in to comment.
Something went wrong with that request. Please try again.