Commit 0e10671561be314d6a6f7c5ae3c7c4788cd46ebb
1 parent
43484d84
Exists in
master
and in
21 other branches
Use search method for similar items
Showing
2 changed files
with
17 additions
and
23 deletions
Show diff stats
lib/searchkick/search.rb
@@ -35,7 +35,16 @@ module Searchkick | @@ -35,7 +35,16 @@ module Searchkick | ||
35 | 35 | ||
36 | all = term == "*" | 36 | all = term == "*" |
37 | 37 | ||
38 | - if all | 38 | + if options[:similar] |
39 | + payload = { | ||
40 | + more_like_this: { | ||
41 | + fields: fields, | ||
42 | + like_text: term, | ||
43 | + min_doc_freq: 1, | ||
44 | + min_term_freq: 1 | ||
45 | + } | ||
46 | + } | ||
47 | + elsif all | ||
39 | payload = { | 48 | payload = { |
40 | match_all: {} | 49 | match_all: {} |
41 | } | 50 | } |
lib/searchkick/similar.rb
@@ -6,28 +6,13 @@ module Searchkick | @@ -6,28 +6,13 @@ module Searchkick | ||
6 | .keep_if{|k,v| k[0] != "_" and (!options[:fields] or options[:fields].map(&:to_sym).include?(k)) } | 6 | .keep_if{|k,v| k[0] != "_" and (!options[:fields] or options[:fields].map(&:to_sym).include?(k)) } |
7 | .values.compact.join(" ") | 7 | .values.compact.join(" ") |
8 | 8 | ||
9 | - fields = options[:fields] ? options[:fields].map{|f| "#{f}.analyzed" } : ["_all"] | ||
10 | - | ||
11 | - payload = { | ||
12 | - query: { | ||
13 | - more_like_this: { | ||
14 | - fields: fields, | ||
15 | - like_text: like_text, | ||
16 | - min_doc_freq: 1, | ||
17 | - min_term_freq: 1 | ||
18 | - } | ||
19 | - }, | ||
20 | - filter: { | ||
21 | - not: { | ||
22 | - term: { | ||
23 | - _id: id | ||
24 | - } | ||
25 | - } | ||
26 | - } | ||
27 | - } | ||
28 | - | ||
29 | - search = Tire::Search::Search.new(index_name, payload: payload) | ||
30 | - Searchkick::Results.new(search.json, search.options) | 9 | + # TODO deep merge method |
10 | + options[:where] ||= {} | ||
11 | + options[:where][:_id] ||= {} | ||
12 | + options[:where][:_id][:not] = id | ||
13 | + options[:limit] ||= 10 | ||
14 | + options[:similar] = true | ||
15 | + self.class.search(like_text, options) | ||
31 | end | 16 | end |
32 | 17 | ||
33 | end | 18 | end |