Skip to content

Commit 741d6d8

Browse files
committed
Fix ES error in protip.
1 parent d863a72 commit 741d6d8

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

app/models/protip.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def search(query_string, tags =[], options={})
149149
filters = []
150150
filters << {term: {upvoters: bookmarked_by}} unless bookmarked_by.nil?
151151
filters << {term: {'user.user_id' => author}} unless author.nil?
152-
Rails.logger.debug "SEARCH: query=#{query}, tags=#{tags}, team=#{team}, author=#{author}, bookmarked_by=#{bookmarked_by}, execution=#{execution}, sorts=#{sorts} from query-string=#{query_string}, #{options.inspect}"
152+
Rails.logger.debug "SEARCH: query=#{query}, tags=#{tags}, team=#{team}, author=#{author}, bookmarked_by=#{bookmarked_by}, execution=#{execution}, sorts=#{sorts} from query-string=#{query_string}, #{options.inspect}" if ENV['DEBUG']
153153
begin
154154
tire.search(options) do
155155
query { string query, default_operator: 'AND', use_dis_max: true } unless query.blank?

lib/search.rb

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ def rebuild_index(name = nil)
1111
end
1212
end
1313

14-
def self.included(base) ; base.extend(ClassMethods) ; end
14+
def self.included(base)
15+
base.extend(ClassMethods)
16+
end
1517

1618
class Search
1719
def initialize(context, query=nil, scope=nil, sort=nil, facet=nil, options={})
@@ -37,13 +39,17 @@ def execute
3739
filter *fltr
3840
end unless filter_criteria.nil?
3941

40-
sort { by sort_criteria.to_tire } unless sort_criteria.nil?
42+
sort do
43+
sort_criteria.to_tire.each do |k|
44+
by k
45+
end
46+
end unless sort_criteria.nil?
4147

4248
ap facets
4349
ap facets.to_tire unless facets.nil?
4450
eval(facets.to_tire) unless facets.nil?
4551

46-
Rails.logger.debug "[search](#{context.to_s}):" + JSON.pretty_generate(to_hash)
52+
Rails.logger.debug "[search](#{context.to_s}):" + JSON.pretty_generate(to_hash) if ENV['DEBUG']
4753
end
4854
rescue Tire::Search::SearchRequestFailed, Errno::ECONNREFUSED
4955
if @options[:failover].nil?
@@ -53,9 +59,13 @@ def execute
5359
end
5460
end
5561

56-
def sort_criteria ; @sort ; end
62+
def sort_criteria
63+
@sort
64+
end
5765

58-
def failover_strategy ; { failover: @context.order('created_at DESC') } ; end
66+
def failover_strategy
67+
{ failover: @context.order('created_at DESC') }
68+
end
5969

6070
class Scope
6171
def initialize(domain, object)
@@ -64,8 +74,13 @@ def initialize(domain, object)
6474
@filter = to_hash
6575
end
6676

67-
def to_tire ; @filter ; end
68-
def to_hash ; {} ; end
77+
def to_tire
78+
@filter
79+
end
80+
81+
def to_hash
82+
{}
83+
end
6984

7085
def <<(other)
7186
@filter.deep_merge(other.to_tire)
@@ -79,19 +94,27 @@ def initialize(fields, direction = 'desc')
7994
@direction = direction
8095
end
8196

82-
def to_tire ; @fields.map { |field| {field => @direction} } ; end
97+
def to_tire
98+
@fields.map do |field|
99+
{field => {order: @direction}}
100+
end
101+
end
102+
103+
alias_method :to_s, :to_tire
83104
end
84105

85106
class Query
86-
def default_query ; '' ; end
107+
def default_query;
108+
'';
109+
end
87110

88111
def initialize(query_string, default_operator = 'AND', default_query_string = default_query)
89112
@query_string = default_query_string + ' ' + query_string
90113
@default_operator = default_operator
91114
end
92115

93116
def to_tire
94-
[:string, "#{@query_string}", { default_operator: "#{@default_operator}" }] unless @query_string.blank?
117+
[:string, "#{@query_string}", {default_operator: "#{@default_operator}"}] unless @query_string.blank?
95118
end
96119
end
97120

@@ -111,7 +134,9 @@ def to_eval_form
111134
"end"
112135
end
113136

114-
def to_tire ; @facet ; end
137+
def to_tire
138+
@facet
139+
end
115140

116141
def <<(other_facet)
117142
@facet << "\n" << other_facet.to_eval_form

0 commit comments

Comments
 (0)