From df88f33869337883be4dc479af774a3b5d6058f7 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sat, 3 Aug 2013 22:38:04 -0700 Subject: [PATCH] Personalized results - closes #4 --- README.md | 23 +++++++++++++++++++++++ lib/searchkick/search.rb | 7 +++++++ test/boost_test.rb | 10 ++++++++++ test/test_helper.rb | 8 ++++++-- 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9a51c6d..0153b91 100644 --- a/README.md +++ b/README.md @@ -201,6 +201,29 @@ Reindex and set up a cron job to add new conversions daily. rake searchkick:reindex CLASS=Product ``` +### Personalized Results [master branch] + +**Subject to change before the next gem release** + +Order results differently for each user. For example, show a user’s previously ordered products before other results. + +```ruby +class Product < ActiveRecord::Base + def search_data + { + name: name, + user_ids: [4, 8, 15, 16, 23, 42] # boost this product for these users + } + end +end +``` + +Reindex and search with: + +```ruby +Product.search "milk", user_id: 8 +``` + ### Facets ```ruby diff --git a/lib/searchkick/search.rb b/lib/searchkick/search.rb index e86a733..eeb5397 100644 --- a/lib/searchkick/search.rb +++ b/lib/searchkick/search.rb @@ -56,6 +56,13 @@ module Searchkick script "log(doc['#{options[:boost]}'].value + 2.718281828)" end end + if options[:user_id] + filter do + filter :term, user_ids: options[:user_id] + boost 100 + end + end + score_mode "total" end end from options[:offset] if options[:offset] diff --git a/test/boost_test.rb b/test/boost_test.rb index dd81d7e..d461efe 100644 --- a/test/boost_test.rb +++ b/test/boost_test.rb @@ -46,4 +46,14 @@ class TestBoost < Minitest::Unit::TestCase assert_order "product", ["Product Conversions", "Product Boost"], boost: "orders_count" end + def test_user_id + store [ + {name: "Tomato A"}, + {name: "Tomato B", user_ids: [1, 2, 3]}, + {name: "Tomato C"}, + {name: "Tomato D"} + ] + assert_first "tomato", "Tomato B", user_id: 2 + end + end diff --git a/test/test_helper.rb b/test/test_helper.rb index 0c4887d..e324966 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -49,10 +49,10 @@ class Product < ActiveRecord::Base ["bandaid", "bandag"] ] - attr_accessor :conversions + attr_accessor :conversions, :user_ids def search_data - as_json.merge conversions: conversions + as_json.merge conversions: conversions, user_ids: user_ids end end @@ -89,4 +89,8 @@ class MiniTest::Unit::TestCase assert_equal expected, Product.search(term, options).map(&:name) end + def assert_first(term, expected, options = {}) + assert_equal expected, Product.search(term, options).map(&:name).first + end + end -- libgit2 0.21.0