elastic-rails 0.1.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Guardfile +46 -0
- data/elastic.gemspec +13 -4
- data/lib/elastic/commands/build_agg_from_params.rb +63 -0
- data/lib/elastic/commands/build_query_from_params.rb +132 -0
- data/lib/elastic/commands/import_index_documents.rb +63 -0
- data/lib/elastic/configuration.rb +61 -0
- data/lib/elastic/core/adaptor.rb +102 -0
- data/lib/elastic/core/base_middleware.rb +43 -0
- data/lib/elastic/core/default_middleware.rb +64 -0
- data/lib/elastic/core/definition.rb +118 -0
- data/lib/elastic/core/mapping_manager.rb +120 -0
- data/lib/elastic/core/middleware.rb +26 -0
- data/lib/elastic/core/query_assembler.rb +84 -0
- data/lib/elastic/core/query_config.rb +25 -0
- data/lib/elastic/core/serializer.rb +45 -0
- data/lib/elastic/core/source_formatter.rb +40 -0
- data/lib/elastic/dsl/bool_query_builder.rb +52 -0
- data/lib/elastic/dsl/bool_query_context.rb +42 -0
- data/lib/elastic/dsl/metric_builder.rb +34 -0
- data/lib/elastic/fields/nested.rb +42 -0
- data/lib/elastic/fields/value.rb +60 -0
- data/lib/elastic/nested_type.rb +5 -0
- data/lib/elastic/nodes/agg/average.rb +7 -0
- data/lib/elastic/nodes/agg/base_metric.rb +42 -0
- data/lib/elastic/nodes/agg/date_histogram.rb +48 -0
- data/lib/elastic/nodes/agg/maximum.rb +7 -0
- data/lib/elastic/nodes/agg/minimum.rb +7 -0
- data/lib/elastic/nodes/agg/stats.rb +7 -0
- data/lib/elastic/nodes/agg/sum.rb +7 -0
- data/lib/elastic/nodes/agg/terms.rb +38 -0
- data/lib/elastic/nodes/agg/top_hits.rb +17 -0
- data/lib/elastic/nodes/and.rb +45 -0
- data/lib/elastic/nodes/base.rb +34 -0
- data/lib/elastic/nodes/base_agg.rb +32 -0
- data/lib/elastic/nodes/boolean.rb +87 -0
- data/lib/elastic/nodes/concerns/aggregable.rb +52 -0
- data/lib/elastic/nodes/concerns/boostable.rb +25 -0
- data/lib/elastic/nodes/concerns/bucketed.rb +17 -0
- data/lib/elastic/nodes/concerns/hit_provider.rb +39 -0
- data/lib/elastic/nodes/function_score.rb +116 -0
- data/lib/elastic/nodes/match.rb +45 -0
- data/lib/elastic/nodes/nested.rb +42 -0
- data/lib/elastic/nodes/or.rb +9 -0
- data/lib/elastic/nodes/range.rb +36 -0
- data/lib/elastic/nodes/search.rb +48 -0
- data/lib/elastic/nodes/term.rb +58 -0
- data/lib/elastic/query.rb +84 -22
- data/lib/elastic/railtie.rb +41 -0
- data/lib/elastic/railties/ar_helpers.rb +51 -0
- data/lib/elastic/railties/ar_middleware.rb +45 -0
- data/lib/elastic/{indexable_record.rb → railties/indexable_record.rb} +21 -18
- data/lib/elastic/railties/query_extensions.rb +9 -0
- data/lib/elastic/railties/rspec.rb +6 -0
- data/lib/elastic/railties/tasks/es.rake +19 -0
- data/lib/elastic/railties/type_extensions.rb +14 -0
- data/lib/elastic/railties/utils.rb +62 -0
- data/lib/elastic/results/aggregations.rb +29 -0
- data/lib/elastic/results/base.rb +13 -0
- data/lib/elastic/results/bucket.rb +15 -0
- data/lib/elastic/results/bucket_collection.rb +17 -0
- data/lib/elastic/results/grouped_result.rb +37 -0
- data/lib/elastic/results/hit.rb +25 -0
- data/lib/elastic/results/hit_collection.rb +38 -0
- data/lib/elastic/results/metric.rb +13 -0
- data/lib/elastic/results/result_group.rb +19 -0
- data/lib/elastic/results/root.rb +15 -0
- data/lib/elastic/shims/base.rb +23 -0
- data/lib/elastic/shims/grouping.rb +23 -0
- data/lib/elastic/shims/populating.rb +69 -0
- data/lib/elastic/shims/reducing.rb +20 -0
- data/lib/elastic/support/command.rb +34 -0
- data/lib/elastic/support/transform.rb +37 -0
- data/lib/elastic/support/traversable.rb +22 -0
- data/lib/elastic/type.rb +56 -84
- data/lib/elastic/types/base_type.rb +38 -0
- data/lib/elastic/types/faceted_type.rb +16 -0
- data/lib/elastic/types/nestable_type.rb +14 -0
- data/lib/elastic/version.rb +1 -1
- data/lib/elastic.rb +72 -30
- data/lib/generators/elastic/index_generator.rb +10 -0
- data/lib/generators/elastic/templates/index.rb +17 -0
- metadata +222 -16
- data/lib/elastic/capabilities/aggregation_builder.rb +0 -64
- data/lib/elastic/capabilities/bool_query_builder.rb +0 -74
- data/lib/elastic/capabilities/context_handler.rb +0 -31
- data/lib/elastic/histogram.rb +0 -49
- data/lib/elastic/index.rb +0 -113
- data/lib/elastic/indexable.rb +0 -25
- data/lib/elastic/value_transform.rb +0 -15
data/lib/elastic/index.rb
DELETED
@@ -1,113 +0,0 @@
|
|
1
|
-
module Elastic
|
2
|
-
class Index
|
3
|
-
attr_reader :api_client, :index_name
|
4
|
-
|
5
|
-
def initialize(_api_client, _index_name)
|
6
|
-
@api_client = _api_client
|
7
|
-
@index_name = _index_name
|
8
|
-
# TODO: multiple index mode (one per type)
|
9
|
-
end
|
10
|
-
|
11
|
-
def drop
|
12
|
-
api_client.indices.delete index: index_name # TODO: add +'*' in multi index mode
|
13
|
-
clear_index_cache
|
14
|
-
end
|
15
|
-
|
16
|
-
def refresh
|
17
|
-
ensure_index index_name
|
18
|
-
api_client.indices.refresh index: index_name # TODO: add +'*' in multi index mode
|
19
|
-
end
|
20
|
-
|
21
|
-
def index(_type, _data, mapping: nil)
|
22
|
-
ensure_type(_type)
|
23
|
-
ensure_mapping(_type, mapping) unless mapping.nil?
|
24
|
-
|
25
|
-
options = {
|
26
|
-
index: index_name,
|
27
|
-
type: _type,
|
28
|
-
body: _data
|
29
|
-
}
|
30
|
-
|
31
|
-
options[:id] = _data[:id] if _data.key? :id
|
32
|
-
|
33
|
-
api_client.index(options)
|
34
|
-
end
|
35
|
-
|
36
|
-
def exists?(_type)
|
37
|
-
api_client.indices.exists_type build_options(_type)
|
38
|
-
end
|
39
|
-
|
40
|
-
def clear(_type, _query = nil)
|
41
|
-
if _query.nil?
|
42
|
-
return unless exists? _type
|
43
|
-
api_client.indices.delete_mapping build_options(_type)
|
44
|
-
clear_type_cache(_type)
|
45
|
-
else
|
46
|
-
ensure_type(_type)
|
47
|
-
api_client.delete_by_query build_options(_type, q: _query)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def query(_type, _query)
|
52
|
-
ensure_type(_type)
|
53
|
-
api_client.search build_options(_type, body: _query)
|
54
|
-
end
|
55
|
-
|
56
|
-
def count(_type, _query = nil)
|
57
|
-
ensure_type(_type)
|
58
|
-
r = api_client.count build_options(_type, body: _query)
|
59
|
-
r["count"]
|
60
|
-
end
|
61
|
-
|
62
|
-
private
|
63
|
-
|
64
|
-
def self.clear_cache
|
65
|
-
@@index_cache = nil
|
66
|
-
@@mapping_cache = nil
|
67
|
-
end
|
68
|
-
|
69
|
-
def index_cache
|
70
|
-
@@index_cache ||= {}
|
71
|
-
end
|
72
|
-
|
73
|
-
def mapping_cache
|
74
|
-
@@mapping_cache ||= {}
|
75
|
-
end
|
76
|
-
|
77
|
-
def build_options(_type, _options = {})
|
78
|
-
# TODO: in multiple index mode use { index: index_name + _type }
|
79
|
-
{ index: index_name, type: _type }.merge! _options
|
80
|
-
end
|
81
|
-
|
82
|
-
def clear_index_cache
|
83
|
-
clear_cache
|
84
|
-
end
|
85
|
-
|
86
|
-
def clear_type_cache(_type)
|
87
|
-
mapping_cache[type_key(_type)]
|
88
|
-
end
|
89
|
-
|
90
|
-
def ensure_index(_name)
|
91
|
-
return if index_cache[_name]
|
92
|
-
return if api_client.indices.exists? index: _name
|
93
|
-
api_client.indices.create index: _name
|
94
|
-
api_client.cluster.health wait_for_status: 'yellow'
|
95
|
-
end
|
96
|
-
|
97
|
-
def ensure_type(_type)
|
98
|
-
ensure_index(index_name)
|
99
|
-
end
|
100
|
-
|
101
|
-
def ensure_mapping(_type, _mapping)
|
102
|
-
return if mapping_cache[type_key(_type)] == _mapping
|
103
|
-
api_client.indices.put_mapping index: index_name, type: _type, body: _mapping
|
104
|
-
mapping_cache[type_key(_type)] = _mapping
|
105
|
-
end
|
106
|
-
|
107
|
-
def type_key(_type)
|
108
|
-
index_name + '/' + _type
|
109
|
-
end
|
110
|
-
|
111
|
-
clear_cache
|
112
|
-
end
|
113
|
-
end
|
data/lib/elastic/indexable.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
module Elastic
|
2
|
-
module Indexable
|
3
|
-
def self.included(_base)
|
4
|
-
_base.extend ClassMethods
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
def index_class
|
9
|
-
@index_class = (to_s + 'Index').constantize
|
10
|
-
end
|
11
|
-
|
12
|
-
def query
|
13
|
-
Elastic::Query.new index_class
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def index_now
|
18
|
-
self.class.index_class.store self
|
19
|
-
end
|
20
|
-
|
21
|
-
def index_later
|
22
|
-
index_now
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
module Elastic
|
2
|
-
class ValueTransform
|
3
|
-
attr_reader :context, :transform
|
4
|
-
|
5
|
-
def initialize(_context, _transform)
|
6
|
-
@context = _context
|
7
|
-
@transform = _transform
|
8
|
-
end
|
9
|
-
|
10
|
-
def apply(_value)
|
11
|
-
return _value.public_send @transform if @transform.is_a? String
|
12
|
-
_value.instance_exec(&@transform)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|