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.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/Guardfile +46 -0
  3. data/elastic.gemspec +13 -4
  4. data/lib/elastic/commands/build_agg_from_params.rb +63 -0
  5. data/lib/elastic/commands/build_query_from_params.rb +132 -0
  6. data/lib/elastic/commands/import_index_documents.rb +63 -0
  7. data/lib/elastic/configuration.rb +61 -0
  8. data/lib/elastic/core/adaptor.rb +102 -0
  9. data/lib/elastic/core/base_middleware.rb +43 -0
  10. data/lib/elastic/core/default_middleware.rb +64 -0
  11. data/lib/elastic/core/definition.rb +118 -0
  12. data/lib/elastic/core/mapping_manager.rb +120 -0
  13. data/lib/elastic/core/middleware.rb +26 -0
  14. data/lib/elastic/core/query_assembler.rb +84 -0
  15. data/lib/elastic/core/query_config.rb +25 -0
  16. data/lib/elastic/core/serializer.rb +45 -0
  17. data/lib/elastic/core/source_formatter.rb +40 -0
  18. data/lib/elastic/dsl/bool_query_builder.rb +52 -0
  19. data/lib/elastic/dsl/bool_query_context.rb +42 -0
  20. data/lib/elastic/dsl/metric_builder.rb +34 -0
  21. data/lib/elastic/fields/nested.rb +42 -0
  22. data/lib/elastic/fields/value.rb +60 -0
  23. data/lib/elastic/nested_type.rb +5 -0
  24. data/lib/elastic/nodes/agg/average.rb +7 -0
  25. data/lib/elastic/nodes/agg/base_metric.rb +42 -0
  26. data/lib/elastic/nodes/agg/date_histogram.rb +48 -0
  27. data/lib/elastic/nodes/agg/maximum.rb +7 -0
  28. data/lib/elastic/nodes/agg/minimum.rb +7 -0
  29. data/lib/elastic/nodes/agg/stats.rb +7 -0
  30. data/lib/elastic/nodes/agg/sum.rb +7 -0
  31. data/lib/elastic/nodes/agg/terms.rb +38 -0
  32. data/lib/elastic/nodes/agg/top_hits.rb +17 -0
  33. data/lib/elastic/nodes/and.rb +45 -0
  34. data/lib/elastic/nodes/base.rb +34 -0
  35. data/lib/elastic/nodes/base_agg.rb +32 -0
  36. data/lib/elastic/nodes/boolean.rb +87 -0
  37. data/lib/elastic/nodes/concerns/aggregable.rb +52 -0
  38. data/lib/elastic/nodes/concerns/boostable.rb +25 -0
  39. data/lib/elastic/nodes/concerns/bucketed.rb +17 -0
  40. data/lib/elastic/nodes/concerns/hit_provider.rb +39 -0
  41. data/lib/elastic/nodes/function_score.rb +116 -0
  42. data/lib/elastic/nodes/match.rb +45 -0
  43. data/lib/elastic/nodes/nested.rb +42 -0
  44. data/lib/elastic/nodes/or.rb +9 -0
  45. data/lib/elastic/nodes/range.rb +36 -0
  46. data/lib/elastic/nodes/search.rb +48 -0
  47. data/lib/elastic/nodes/term.rb +58 -0
  48. data/lib/elastic/query.rb +84 -22
  49. data/lib/elastic/railtie.rb +41 -0
  50. data/lib/elastic/railties/ar_helpers.rb +51 -0
  51. data/lib/elastic/railties/ar_middleware.rb +45 -0
  52. data/lib/elastic/{indexable_record.rb → railties/indexable_record.rb} +21 -18
  53. data/lib/elastic/railties/query_extensions.rb +9 -0
  54. data/lib/elastic/railties/rspec.rb +6 -0
  55. data/lib/elastic/railties/tasks/es.rake +19 -0
  56. data/lib/elastic/railties/type_extensions.rb +14 -0
  57. data/lib/elastic/railties/utils.rb +62 -0
  58. data/lib/elastic/results/aggregations.rb +29 -0
  59. data/lib/elastic/results/base.rb +13 -0
  60. data/lib/elastic/results/bucket.rb +15 -0
  61. data/lib/elastic/results/bucket_collection.rb +17 -0
  62. data/lib/elastic/results/grouped_result.rb +37 -0
  63. data/lib/elastic/results/hit.rb +25 -0
  64. data/lib/elastic/results/hit_collection.rb +38 -0
  65. data/lib/elastic/results/metric.rb +13 -0
  66. data/lib/elastic/results/result_group.rb +19 -0
  67. data/lib/elastic/results/root.rb +15 -0
  68. data/lib/elastic/shims/base.rb +23 -0
  69. data/lib/elastic/shims/grouping.rb +23 -0
  70. data/lib/elastic/shims/populating.rb +69 -0
  71. data/lib/elastic/shims/reducing.rb +20 -0
  72. data/lib/elastic/support/command.rb +34 -0
  73. data/lib/elastic/support/transform.rb +37 -0
  74. data/lib/elastic/support/traversable.rb +22 -0
  75. data/lib/elastic/type.rb +56 -84
  76. data/lib/elastic/types/base_type.rb +38 -0
  77. data/lib/elastic/types/faceted_type.rb +16 -0
  78. data/lib/elastic/types/nestable_type.rb +14 -0
  79. data/lib/elastic/version.rb +1 -1
  80. data/lib/elastic.rb +72 -30
  81. data/lib/generators/elastic/index_generator.rb +10 -0
  82. data/lib/generators/elastic/templates/index.rb +17 -0
  83. metadata +222 -16
  84. data/lib/elastic/capabilities/aggregation_builder.rb +0 -64
  85. data/lib/elastic/capabilities/bool_query_builder.rb +0 -74
  86. data/lib/elastic/capabilities/context_handler.rb +0 -31
  87. data/lib/elastic/histogram.rb +0 -49
  88. data/lib/elastic/index.rb +0 -113
  89. data/lib/elastic/indexable.rb +0 -25
  90. 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
@@ -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