mm_es_search 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
data/lib/mm_es_search.rb
CHANGED
@@ -54,6 +54,7 @@ module MmEsSearch
|
|
54
54
|
|
55
55
|
smart_load "mm_es_search/api/query/and_filter"
|
56
56
|
smart_load "mm_es_search/api/query/bool_filter"
|
57
|
+
smart_load "mm_es_search/api/query/geo_distance_range_filter"
|
57
58
|
smart_load "mm_es_search/api/query/has_child_filter"
|
58
59
|
smart_load "mm_es_search/api/query/has_parent_filter"
|
59
60
|
smart_load "mm_es_search/api/query/match_all_filter"
|
@@ -2,7 +2,36 @@ module MmEsSearch
|
|
2
2
|
module Api
|
3
3
|
module Facet
|
4
4
|
|
5
|
-
|
5
|
+
class GeoDistanceFacet < AbstractFacet
|
6
|
+
|
7
|
+
key :field, String
|
8
|
+
many :ranges, :class_name => 'MmEsSearch::Api::Facet::RangeItem'
|
9
|
+
key :key_field, String
|
10
|
+
key :value_field, String
|
11
|
+
key :key_script, String
|
12
|
+
key :value_script, String
|
13
|
+
key :params, Hash
|
14
|
+
|
15
|
+
key :center #any of the valid formats, see http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-filter.html
|
16
|
+
key :unit, String, :default => "km"
|
17
|
+
|
18
|
+
def to_es_query
|
19
|
+
|
20
|
+
range_params = {field => center, :unit => unit}
|
21
|
+
range_params.merge!(:ranges => ranges.map(&:attributes))
|
22
|
+
range_params.merge!(:key_field => size) if key_field?
|
23
|
+
range_params.merge!(:value_field => size) if value_field?
|
24
|
+
range_params.merge!(:key_script => size) if key_script?
|
25
|
+
range_params.merge!(:value_script => size) if value_script?
|
26
|
+
range_params.merge!(:params => params) unless params.empty?
|
27
|
+
|
28
|
+
facet_params = {:geo_distance => range_params}.merge(super)
|
29
|
+
|
30
|
+
return {label => facet_params}
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
6
35
|
|
7
36
|
end
|
8
37
|
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module MmEsSearch
|
2
|
+
module Api
|
3
|
+
module Query
|
4
|
+
|
5
|
+
class GeoDistanceRangeFilter < AbstractQuery
|
6
|
+
|
7
|
+
key :field, String
|
8
|
+
key :path, String
|
9
|
+
|
10
|
+
key :center #any of the valid formats, see http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-filter.html
|
11
|
+
key :unit, String, :default => "km"
|
12
|
+
|
13
|
+
key :from
|
14
|
+
key :to
|
15
|
+
key :include_lower, :default => true
|
16
|
+
key :include_upper, :default => true
|
17
|
+
key :boost, Float
|
18
|
+
|
19
|
+
# def to_mongo_query(options = {})
|
20
|
+
#
|
21
|
+
# range_params = {}
|
22
|
+
#
|
23
|
+
# if from
|
24
|
+
# cmd = include_lower ? '$gte' : '$gt'
|
25
|
+
# range_params.merge!({cmd => from})
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# if to
|
29
|
+
# cmd = include_upper ? '$lte' : '$lt'
|
30
|
+
# range_params.merge!({cmd => to})
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# if options[:negated]
|
34
|
+
# return {mongo_abs_field => {"$not" => range_params}}
|
35
|
+
# else
|
36
|
+
# return {mongo_abs_field => range_params}
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# end
|
40
|
+
|
41
|
+
def to_es_query
|
42
|
+
|
43
|
+
raise "must have either :from or :to" if from.nil? and to.nil?
|
44
|
+
|
45
|
+
range_params = {es_abs_field => center, :unit => unit}
|
46
|
+
range_params.merge!({:from => from}) unless from.nil?
|
47
|
+
range_params.merge!({:to => to}) unless to.nil?
|
48
|
+
range_params.merge!({:include_lower => include_lower}) unless (from.nil? or include_lower == true)
|
49
|
+
range_params.merge!({:include_upper => include_upper}) unless (to.nil? or include_upper == true)
|
50
|
+
range_params.merge!({:boost => boost}) unless boost.nil?
|
51
|
+
|
52
|
+
return {:geo_distance_range => range_params}
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -36,7 +36,7 @@ module MmEsSearch
|
|
36
36
|
transitions :to => :need_row_data, :from => [:need_field_stats, :need_row_data]
|
37
37
|
end
|
38
38
|
|
39
|
-
aasm_event :prepare_for_new_data, :after => :
|
39
|
+
aasm_event :prepare_for_new_data, :after => :reinitialize do
|
40
40
|
transitions :to => :need_field_stats, :from => [:ready_for_display, :need_field_stats, :need_row_data]
|
41
41
|
end
|
42
42
|
|
@@ -52,6 +52,11 @@ module MmEsSearch
|
|
52
52
|
|
53
53
|
end
|
54
54
|
|
55
|
+
def reinitialize
|
56
|
+
prune_unchecked_rows
|
57
|
+
field_stats_set if is_distance?
|
58
|
+
end
|
59
|
+
|
55
60
|
def result_name
|
56
61
|
'ranges'
|
57
62
|
end
|
@@ -60,6 +65,10 @@ module MmEsSearch
|
|
60
65
|
false
|
61
66
|
end
|
62
67
|
|
68
|
+
def is_distance?
|
69
|
+
false
|
70
|
+
end
|
71
|
+
|
63
72
|
def timezone_matters?
|
64
73
|
true
|
65
74
|
end
|
@@ -89,17 +98,42 @@ module MmEsSearch
|
|
89
98
|
# nil
|
90
99
|
# end
|
91
100
|
|
101
|
+
def es_facet_class
|
102
|
+
if is_distance?
|
103
|
+
GeoDistanceFacet
|
104
|
+
else
|
105
|
+
RangeFacet
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def es_filter_class
|
110
|
+
if is_distance?
|
111
|
+
GeoDistanceRangeFilter
|
112
|
+
else
|
113
|
+
RangeFilter
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def distance_center
|
118
|
+
"gcpvj32dx" #London, UK
|
119
|
+
end
|
120
|
+
|
92
121
|
def to_facet
|
93
122
|
case display_mode || select_display_mode
|
94
123
|
when "range"
|
95
124
|
initialize_rows
|
96
|
-
|
125
|
+
f = es_facet_class.new(
|
97
126
|
default_params.merge(
|
98
127
|
:label => prefix_label('display_result'),
|
99
128
|
:ranges => rows.map(&:to_range_item),
|
100
129
|
:facet_filter => facet_filter
|
101
130
|
)
|
102
131
|
)
|
132
|
+
if is_distance?
|
133
|
+
f.center = distance_center
|
134
|
+
f.unit = distance_unit
|
135
|
+
end
|
136
|
+
f
|
103
137
|
when "histogram"
|
104
138
|
raise NotImplementedError
|
105
139
|
else
|
@@ -122,6 +156,8 @@ module MmEsSearch
|
|
122
156
|
@transform_lookup = {}
|
123
157
|
if is_time?
|
124
158
|
initialize_time_rows
|
159
|
+
elsif is_distance?
|
160
|
+
initialize_distance_rows
|
125
161
|
else
|
126
162
|
initialize_numeric_rows
|
127
163
|
end
|
@@ -332,6 +368,15 @@ module MmEsSearch
|
|
332
368
|
build_rows(values)
|
333
369
|
end
|
334
370
|
|
371
|
+
def distance_unit
|
372
|
+
@distance_unit ||= "km"
|
373
|
+
end
|
374
|
+
|
375
|
+
def initialize_distance_rows
|
376
|
+
values = [0, 1, 5, 10, 50, 100]
|
377
|
+
build_rows(values)
|
378
|
+
end
|
379
|
+
|
335
380
|
def build_rows(values)
|
336
381
|
self.rows = checked_rows #NOTE: we preserve selected rows
|
337
382
|
if (num_values = values.length) == 2
|
data/lib/mm_es_search/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Jonathan Chambers
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2013-
|
17
|
+
date: 2013-03-22 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -58,6 +58,7 @@ files:
|
|
58
58
|
- lib/mm_es_search/api/query/custom_score_query.rb
|
59
59
|
- lib/mm_es_search/api/query/dismax_query.rb
|
60
60
|
- lib/mm_es_search/api/query/filtered_query.rb
|
61
|
+
- lib/mm_es_search/api/query/geo_distance_range_filter.rb
|
61
62
|
- lib/mm_es_search/api/query/has_child_filter.rb
|
62
63
|
- lib/mm_es_search/api/query/has_child_query.rb
|
63
64
|
- lib/mm_es_search/api/query/has_parent_filter.rb
|