@@ -11,25 +11,61 @@ def rebuild_index(name = nil)
11
11
end
12
12
end
13
13
14
- def self . included ( base )
15
- base . extend ( ClassMethods )
16
- end
14
+ def self . included ( base ) ; base . extend ( ClassMethods ) ; end
17
15
18
16
class Search
17
+ def initialize ( context , query = nil , scope = nil , sort = nil , facet = nil , options = { } )
18
+ @context = context
19
+ @query = query
20
+ @scope = scope
21
+ @sort = sort
22
+ @facet = facet
23
+ @options = failover_strategy . merge ( options || { } )
24
+ end
25
+
26
+ def execute
27
+ query_criteria , filter_criteria , sort_criteria , facets , context = [ @query , @scope , @sort , @facet , @context ]
28
+
29
+ @context . tire . search ( @options ) do
30
+ query do
31
+ signature = query_criteria . to_tire
32
+ method = signature . shift
33
+ self . send ( method , *signature )
34
+ end unless query_criteria . nil? || query_criteria . to_tire . blank?
35
+
36
+ filter_criteria . to_tire . each do |fltr |
37
+ filter *fltr
38
+ end unless filter_criteria . nil?
39
+
40
+ sort { by sort_criteria . to_tire } unless sort_criteria . nil?
41
+
42
+ ap facets
43
+ ap facets . to_tire unless facets . nil?
44
+ eval ( facets . to_tire ) unless facets . nil?
45
+
46
+ Rails . logger . debug "[search](#{ context . to_s } ):" + JSON . pretty_generate ( to_hash )
47
+ end
48
+ rescue Tire ::Search ::SearchRequestFailed , Errno ::ECONNREFUSED
49
+ if @options [ :failover ] . nil?
50
+ raise
51
+ else
52
+ @options [ :failover ] . limit ( @options [ :per_page ] || 18 ) . offset ( ( ( @options [ :page ] || 1 ) -1 ) * ( @options [ :per_page ] || 19 ) )
53
+ end
54
+ end
55
+
56
+ def sort_criteria ; @sort ; end
57
+
58
+ def failover_strategy ; { failover : @context . order ( 'created_at DESC' ) } ; end
59
+
19
60
class Scope
20
61
def initialize ( domain , object )
21
62
@domain = domain
22
63
@object = object
23
64
@filter = to_hash
24
65
end
25
66
26
- def to_tire
27
- @filter
28
- end
29
-
30
- def to_hash
31
- { }
32
- end
67
+ def to_tire ; @filter ; end
68
+ def to_hash ; { } ; end
33
69
34
70
def <<( other )
35
71
@filter . deep_merge ( other . to_tire )
@@ -43,30 +79,23 @@ def initialize(fields, direction = 'desc')
43
79
@direction = direction
44
80
end
45
81
46
- def to_tire
47
- @fields . map { |field | { field => @direction } }
48
- end
82
+ def to_tire ; @fields . map { |field | { field => @direction } } ; end
49
83
end
50
84
51
85
class Query
52
- def default_query
53
- ''
54
- end
86
+ def default_query ; '' ; end
55
87
56
88
def initialize ( query_string , default_operator = 'AND' , default_query_string = default_query )
57
89
@query_string = default_query_string + ' ' + query_string
58
90
@default_operator = default_operator
59
91
end
60
92
61
93
def to_tire
62
- unless @query_string . blank?
63
- "string '#{ @query_string } ', :default_operator => '#{ @default_operator } '"
64
- end
94
+ [ :string , "#{ @query_string } " , { default_operator : "#{ @default_operator } " } ] unless @query_string . blank?
65
95
end
66
96
end
67
97
68
98
class Facet
69
-
70
99
def initialize ( name , type , field , options )
71
100
@name = name
72
101
@type = type
@@ -82,9 +111,7 @@ def to_eval_form
82
111
"end"
83
112
end
84
113
85
- def to_tire
86
- @facet
87
- end
114
+ def to_tire ; @facet ; end
88
115
89
116
def <<( other_facet )
90
117
@facet << "\n " << other_facet . to_eval_form
@@ -96,44 +123,6 @@ def <<(other_facet)
96
123
def evaluatable_options
97
124
', ' + @options . join ( ', ' ) unless @options . blank?
98
125
end
99
-
100
- end
101
-
102
- def initialize ( context , query = nil , scope = nil , sort = nil , facet = nil , options = { } )
103
- @context = context
104
- @query = query
105
- @scope = scope
106
- @sort = sort
107
- @facet = facet
108
- @options = failover_strategy . merge ( options || { } )
109
- end
110
-
111
- def sort_criteria
112
- @sort
113
- end
114
-
115
- def failover_strategy
116
- { failover : @context . order ( 'created_at DESC' ) }
117
- end
118
-
119
- def execute
120
- query_criteria , filter_criteria , sort_criteria , facets , context = [ @query , @scope , @sort , @facet , @context ]
121
-
122
- @context . tire . search ( @options ) do
123
- query { eval query_criteria . to_tire } unless query_criteria . nil? || query_criteria . to_tire . blank?
124
- filter_criteria . to_tire . each do |fltr |
125
- filter *fltr
126
- end unless filter_criteria . nil?
127
- sort { by sort_criteria . to_tire } unless sort_criteria . nil?
128
- eval ( facets . to_tire ) unless facets . nil?
129
- Rails . logger . debug "[search](#{ context . to_s } ):" + JSON . pretty_generate ( to_hash )
130
- end
131
- rescue Tire ::Search ::SearchRequestFailed , Errno ::ECONNREFUSED
132
- if @options [ :failover ] . nil?
133
- raise
134
- else
135
- @options [ :failover ] . limit ( @options [ :per_page ] || 18 ) . offset ( ( ( @options [ :page ] || 1 ) -1 ) * ( @options [ :per_page ] || 19 ) )
136
- end
137
126
end
138
127
end
139
128
end
0 commit comments