Commit df88f33869337883be4dc479af774a3b5d6058f7

Authored by Andrew Kane
1 parent e71c15f5

Personalized results - closes #4

README.md
... ... @@ -201,6 +201,29 @@ Reindex and set up a cron job to add new conversions daily.
201 201 rake searchkick:reindex CLASS=Product
202 202 ```
203 203  
  204 +### Personalized Results [master branch]
  205 +
  206 +**Subject to change before the next gem release**
  207 +
  208 +Order results differently for each user. For example, show a user’s previously ordered products before other results.
  209 +
  210 +```ruby
  211 +class Product < ActiveRecord::Base
  212 + def search_data
  213 + {
  214 + name: name,
  215 + user_ids: [4, 8, 15, 16, 23, 42] # boost this product for these users
  216 + }
  217 + end
  218 +end
  219 +```
  220 +
  221 +Reindex and search with:
  222 +
  223 +```ruby
  224 +Product.search "milk", user_id: 8
  225 +```
  226 +
204 227 ### Facets
205 228  
206 229 ```ruby
... ...
lib/searchkick/search.rb
... ... @@ -56,6 +56,13 @@ module Searchkick
56 56 script "log(doc['#{options[:boost]}'].value + 2.718281828)"
57 57 end
58 58 end
  59 + if options[:user_id]
  60 + filter do
  61 + filter :term, user_ids: options[:user_id]
  62 + boost 100
  63 + end
  64 + end
  65 + score_mode "total"
59 66 end
60 67 end
61 68 from options[:offset] if options[:offset]
... ...
test/boost_test.rb
... ... @@ -46,4 +46,14 @@ class TestBoost &lt; Minitest::Unit::TestCase
46 46 assert_order "product", ["Product Conversions", "Product Boost"], boost: "orders_count"
47 47 end
48 48  
  49 + def test_user_id
  50 + store [
  51 + {name: "Tomato A"},
  52 + {name: "Tomato B", user_ids: [1, 2, 3]},
  53 + {name: "Tomato C"},
  54 + {name: "Tomato D"}
  55 + ]
  56 + assert_first "tomato", "Tomato B", user_id: 2
  57 + end
  58 +
49 59 end
... ...
test/test_helper.rb
... ... @@ -49,10 +49,10 @@ class Product &lt; ActiveRecord::Base
49 49 ["bandaid", "bandag"]
50 50 ]
51 51  
52   - attr_accessor :conversions
  52 + attr_accessor :conversions, :user_ids
53 53  
54 54 def search_data
55   - as_json.merge conversions: conversions
  55 + as_json.merge conversions: conversions, user_ids: user_ids
56 56 end
57 57 end
58 58  
... ... @@ -89,4 +89,8 @@ class MiniTest::Unit::TestCase
89 89 assert_equal expected, Product.search(term, options).map(&:name)
90 90 end
91 91  
  92 + def assert_first(term, expected, options = {})
  93 + assert_equal expected, Product.search(term, options).map(&:name).first
  94 + end
  95 +
92 96 end
... ...