elasticated 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +35 -0
  3. data/Gemfile +4 -0
  4. data/README.md +3 -0
  5. data/Rakefile +6 -0
  6. data/elasticated.gemspec +29 -0
  7. data/lib/elasticated.rb +102 -0
  8. data/lib/elasticated/aggregation.rb +36 -0
  9. data/lib/elasticated/aggregations/cardinality_aggregation.rb +15 -0
  10. data/lib/elasticated/aggregations/count_aggregation.rb +15 -0
  11. data/lib/elasticated/aggregations/count_distinct_aggregation.rb +15 -0
  12. data/lib/elasticated/aggregations/count_filtered_aggregation.rb +29 -0
  13. data/lib/elasticated/aggregations/custom_aggregation.rb +25 -0
  14. data/lib/elasticated/aggregations/date_histogram_aggregation.rb +35 -0
  15. data/lib/elasticated/aggregations/filter_aggregation.rb +33 -0
  16. data/lib/elasticated/aggregations/filter_aggregation_evaluator.rb +22 -0
  17. data/lib/elasticated/aggregations/group_aggregation.rb +29 -0
  18. data/lib/elasticated/aggregations/histogram_aggregation.rb +34 -0
  19. data/lib/elasticated/aggregations/nested_aggregation.rb +30 -0
  20. data/lib/elasticated/aggregations/range_aggregation.rb +35 -0
  21. data/lib/elasticated/aggregations/range_aggregation_evaluator.rb +22 -0
  22. data/lib/elasticated/aggregations/ranges_builder.rb +35 -0
  23. data/lib/elasticated/aggregations/single_value_aggregation.rb +47 -0
  24. data/lib/elasticated/aggregations/subaggregated.rb +27 -0
  25. data/lib/elasticated/aggregations/sum_distinct_aggregation.rb +20 -0
  26. data/lib/elasticated/aggregations/terms_aggregation.rb +63 -0
  27. data/lib/elasticated/aggregations/top_hits_aggregation.rb +25 -0
  28. data/lib/elasticated/block_evaluation.rb +15 -0
  29. data/lib/elasticated/boolean_clause.rb +43 -0
  30. data/lib/elasticated/client.rb +84 -0
  31. data/lib/elasticated/clonable.rb +58 -0
  32. data/lib/elasticated/conditions/custom_condition.rb +19 -0
  33. data/lib/elasticated/conditions/exists_condition.rb +11 -0
  34. data/lib/elasticated/conditions/missing_condition.rb +11 -0
  35. data/lib/elasticated/conditions/nested_condition.rb +19 -0
  36. data/lib/elasticated/conditions/range_condition.rb +27 -0
  37. data/lib/elasticated/conditions/script_condition.rb +22 -0
  38. data/lib/elasticated/conditions/standard_condition.rb +26 -0
  39. data/lib/elasticated/conditions/terms_condition.rb +22 -0
  40. data/lib/elasticated/conditions/wildcard_condition.rb +18 -0
  41. data/lib/elasticated/conditions_builder.rb +75 -0
  42. data/lib/elasticated/configurable.rb +9 -0
  43. data/lib/elasticated/configuration.rb +9 -0
  44. data/lib/elasticated/default_logger.rb +27 -0
  45. data/lib/elasticated/delimiters/date_field_delimiter.rb +33 -0
  46. data/lib/elasticated/delimiters/standard_field_delimiter.rb +33 -0
  47. data/lib/elasticated/delimiters/term_field_delimiter.rb +24 -0
  48. data/lib/elasticated/document.rb +46 -0
  49. data/lib/elasticated/helpers.rb +28 -0
  50. data/lib/elasticated/index_selector.rb +44 -0
  51. data/lib/elasticated/inspectionable.rb +9 -0
  52. data/lib/elasticated/mapping.rb +19 -0
  53. data/lib/elasticated/mapping/builder.rb +36 -0
  54. data/lib/elasticated/mapping/fields_builder.rb +148 -0
  55. data/lib/elasticated/mapping/nested_builder.rb +15 -0
  56. data/lib/elasticated/mapping/object_builder.rb +15 -0
  57. data/lib/elasticated/mapping/partial.rb +11 -0
  58. data/lib/elasticated/mapping/type_builder.rb +14 -0
  59. data/lib/elasticated/partitioned_repository.rb +27 -0
  60. data/lib/elasticated/query.rb +159 -0
  61. data/lib/elasticated/query_aggregations.rb +71 -0
  62. data/lib/elasticated/query_conditions.rb +89 -0
  63. data/lib/elasticated/repositories/monthly_partitioned_repository.rb +96 -0
  64. data/lib/elasticated/repository.rb +139 -0
  65. data/lib/elasticated/results.rb +43 -0
  66. data/lib/version.rb +92 -0
  67. data/spec/aggregation_spec.rb +587 -0
  68. data/spec/date_field_delimiter_spec.rb +67 -0
  69. data/spec/document_spec.rb +44 -0
  70. data/spec/elasticsearch_hit_1.json +14 -0
  71. data/spec/elasticsearch_response_1.json +29 -0
  72. data/spec/elasticsearch_response_2.json +44 -0
  73. data/spec/elasticsearch_top_hits_response.json +20 -0
  74. data/spec/integration_spec.rb +184 -0
  75. data/spec/mapping_spec.rb +219 -0
  76. data/spec/monthly_partitioned_repository_spec.rb +99 -0
  77. data/spec/query_aggregations_spec.rb +44 -0
  78. data/spec/query_conditions_spec.rb +314 -0
  79. data/spec/query_spec.rb +265 -0
  80. data/spec/results_spec.rb +69 -0
  81. data/spec/spec_helper.rb +2 -0
  82. data/spec/term_field_delimiter_spec.rb +39 -0
  83. metadata +225 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8dc9235eb397f995d6bd706184dde4d43a3a85dc
4
+ data.tar.gz: 10cd46beddee5efa0aaa3bf301d534880413f968
5
+ SHA512:
6
+ metadata.gz: 27039469aef119a66796daaa93ab1713845a91cb61345bd3592a55e41ccd84827cc72ec9e4ebeaa450d24979f98a5f09feef6eedf1ed003be86a69ba8c0d242d
7
+ data.tar.gz: 1998fbde3e419a39fcf6090fc827ffce2a498237056130efb5fd7b2b152d87983026ac203fcbed5e5f9c7e739a9eb92ebb0b6f74c1b57867dd38b93832bbcda4
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ Gemfile.lock
31
+ .ruby-version
32
+ .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in elasticated.gemspec
4
+ gemspec
@@ -0,0 +1,3 @@
1
+ # elasticated
2
+
3
+ Elasticsearch Wrapper, with Query & Mapping Builders
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'elasticated'
8
+ spec.version = Elasticated::VERSION
9
+ spec.authors = ['Pablo Fernandez']
10
+ spec.email = ['psfutn@gmail.com']
11
+ spec.summary = 'Elasticsearch Wrapper, with Query & Mapping Builders'
12
+ spec.description = ''
13
+ spec.homepage = 'http://github.com/pablo31/elasticated'
14
+ spec.license = ''
15
+
16
+ spec.files = `git ls-files`.split($\)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.5'
22
+ spec.add_development_dependency 'rspec', '3.0.0'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'pry-byebug'
25
+
26
+ spec.add_runtime_dependency 'elasticsearch'
27
+ spec.add_runtime_dependency 'hash_ext', '~> 0.1.1'
28
+
29
+ end
@@ -0,0 +1,102 @@
1
+ require 'json'
2
+ require 'hash_ext'
3
+ require 'elasticsearch'
4
+
5
+ require_relative 'elasticated/helpers'
6
+ require_relative 'elasticated/block_evaluation'
7
+ require_relative 'elasticated/clonable'
8
+ require_relative 'elasticated/inspectionable'
9
+ require_relative 'elasticated/configuration'
10
+ require_relative 'elasticated/configurable'
11
+ require_relative 'elasticated/default_logger'
12
+
13
+ # query conditions
14
+
15
+ require_relative 'elasticated/conditions/standard_condition'
16
+ require_relative 'elasticated/conditions/terms_condition'
17
+ require_relative 'elasticated/conditions/wildcard_condition'
18
+ require_relative 'elasticated/conditions/exists_condition'
19
+ require_relative 'elasticated/conditions/missing_condition'
20
+ require_relative 'elasticated/conditions/range_condition'
21
+ require_relative 'elasticated/conditions/nested_condition'
22
+ require_relative 'elasticated/conditions/script_condition'
23
+ require_relative 'elasticated/conditions/custom_condition'
24
+
25
+ # delimiters
26
+
27
+ require_relative 'elasticated/delimiters/standard_field_delimiter'
28
+ require_relative 'elasticated/delimiters/term_field_delimiter'
29
+ require_relative 'elasticated/delimiters/date_field_delimiter'
30
+
31
+ # query components
32
+
33
+ require_relative 'elasticated/conditions_builder'
34
+ require_relative 'elasticated/boolean_clause'
35
+ require_relative 'elasticated/query_conditions'
36
+ require_relative 'elasticated/query_aggregations'
37
+
38
+ # query
39
+
40
+ require_relative 'elasticated/query'
41
+
42
+ # aggregations
43
+
44
+ require_relative 'elasticated/aggregation'
45
+ require_relative 'elasticated/aggregations/subaggregated'
46
+
47
+ require_relative 'elasticated/aggregations/terms_aggregation'
48
+
49
+ require_relative 'elasticated/aggregations/histogram_aggregation'
50
+ require_relative 'elasticated/aggregations/date_histogram_aggregation'
51
+
52
+ require_relative 'elasticated/aggregations/single_value_aggregation'
53
+ require_relative 'elasticated/aggregations/cardinality_aggregation'
54
+ require_relative 'elasticated/aggregations/count_distinct_aggregation'
55
+
56
+ require_relative 'elasticated/aggregations/group_aggregation'
57
+ require_relative 'elasticated/aggregations/count_aggregation'
58
+ require_relative 'elasticated/aggregations/sum_distinct_aggregation'
59
+
60
+ require_relative 'elasticated/aggregations/count_filtered_aggregation'
61
+ require_relative 'elasticated/aggregations/filter_aggregation_evaluator'
62
+ require_relative 'elasticated/aggregations/filter_aggregation'
63
+
64
+ require_relative 'elasticated/aggregations/nested_aggregation'
65
+
66
+ require_relative 'elasticated/aggregations/ranges_builder'
67
+ require_relative 'elasticated/aggregations/range_aggregation_evaluator'
68
+ require_relative 'elasticated/aggregations/range_aggregation'
69
+
70
+ require_relative 'elasticated/aggregations/top_hits_aggregation'
71
+
72
+ require_relative 'elasticated/aggregations/custom_aggregation'
73
+
74
+ # repository
75
+
76
+ require_relative 'elasticated/document'
77
+ require_relative 'elasticated/results'
78
+
79
+ require_relative 'elasticated/client'
80
+ require_relative 'elasticated/repository'
81
+
82
+ require_relative 'elasticated/mapping'
83
+ require_relative 'elasticated/mapping/partial'
84
+ require_relative 'elasticated/mapping/fields_builder'
85
+ require_relative 'elasticated/mapping/object_builder'
86
+ require_relative 'elasticated/mapping/nested_builder'
87
+ require_relative 'elasticated/mapping/type_builder'
88
+ require_relative 'elasticated/mapping/builder'
89
+
90
+ require_relative 'elasticated/index_selector'
91
+ require_relative 'elasticated/partitioned_repository'
92
+
93
+ require_relative 'elasticated/repositories/monthly_partitioned_repository'
94
+
95
+
96
+ module Elasticated
97
+
98
+ def self.configure(&block)
99
+ block.call Configuration
100
+ end
101
+
102
+ end
@@ -0,0 +1,36 @@
1
+ module Elasticated
2
+ class Aggregation
3
+
4
+ # abstract class
5
+ # child must implement 'default_name'
6
+ # child must implement 'build'
7
+ # child must implement 'parse(response)'
8
+
9
+ include BlockEvaluation
10
+ include Clonable
11
+
12
+ attr_accessor :field, :alias_name, :extra_params
13
+
14
+ def name
15
+ return Helpers.string_to_agg_name(alias_name).to_sym if alias_name
16
+ Helpers.string_to_agg_name(default_name).to_sym
17
+ end
18
+
19
+ def original_name
20
+ return alias_name.to_sym if alias_name
21
+ default_name.to_s.to_sym
22
+ end
23
+
24
+ def initialize(field, *args, &block)
25
+ self.field = field
26
+ opts = args.last
27
+ if opts.is_a? Hash
28
+ self.extra_params = Helpers.hash_deep_dup opts
29
+ self.alias_name = extra_params.delete :as
30
+ else
31
+ self.extra_params = Hash.new
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,15 @@
1
+ module Elasticated
2
+ class CardinalityAggregation < SingleValueAggregation
3
+
4
+ # override
5
+ def default_name
6
+ "#{field}_cardinality"
7
+ end
8
+
9
+ # implementation
10
+ def operation
11
+ :cardinality
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Elasticated
2
+ class CountAggregation < TermsAggregation
3
+
4
+ def default_name
5
+ "count_by_#{field}"
6
+ end
7
+
8
+ def parse(response)
9
+ response['buckets'].inject({}) do |hash, bucket|
10
+ hash.merge bucket['key'] => bucket['doc_count']
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Elasticated
2
+ class CountDistinctAggregation < SingleValueAggregation
3
+
4
+ # override
5
+ def default_name
6
+ "#{field}_value_count"
7
+ end
8
+
9
+ # implementation
10
+ def operation
11
+ :value_count
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ module Elasticated
2
+ class CountFilteredAggregation < Aggregation
3
+
4
+ attr_accessor :_conditions, :_filter_name
5
+
6
+ def initialize(filter_name, *args, &block)
7
+ self._conditions = QueryConditions.new
8
+ self._filter_name = filter_name
9
+ _conditions.evaluate block
10
+ end
11
+
12
+ def default_name
13
+ _filter_name
14
+ end
15
+
16
+ def conditions(&block)
17
+ _conditions.instance_exec &block
18
+ end
19
+
20
+ def build
21
+ { filter: _conditions.build }
22
+ end
23
+
24
+ def parse(response)
25
+ response['doc_count']
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ module Elasticated
2
+ class CustomAggregation < Aggregation
3
+
4
+ attr_accessor :_name, :_hash
5
+
6
+ def initialize(name, &block)
7
+ self._name = name
8
+ super
9
+ self._hash = block.call
10
+ end
11
+
12
+ def default_name
13
+ _name
14
+ end
15
+
16
+ def build
17
+ _hash
18
+ end
19
+
20
+ def parse(response)
21
+ response
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ module Elasticated
2
+ class DateHistogramAggregation < HistogramAggregation
3
+ include Subaggregated
4
+
5
+ attr_accessor :format, :offset
6
+
7
+ def initialize(field, opts={}, &block)
8
+ self.format = opts.delete(:format) || 'yyyy-MM-dd'
9
+ interval = opts.delete(:interval) || 'day'
10
+ self.offset = opts.delete(:offset)
11
+ super field, interval, opts, &block
12
+ end
13
+
14
+ def build
15
+ terms = { field: field, interval: interval, format: format }
16
+ if offset # '1.4 style'
17
+ terms.merge! pre_offset: offset
18
+ terms.merge! post_offset: offset
19
+ end
20
+ terms.merge! extra_params
21
+ aggregation_struct = { date_histogram: terms }
22
+ aggregation_struct.merge! build_subaggregations
23
+ aggregation_struct
24
+ end
25
+
26
+ def parse(response)
27
+ response['buckets'].inject({}) do |hash, bucket|
28
+ bucket_hash = { 'count' => bucket['doc_count'] }
29
+ bucket_hash.merge! parse_subaggregations(bucket)
30
+ hash.merge bucket['key_as_string'] => bucket_hash
31
+ end
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,33 @@
1
+ module Elasticated
2
+ class FilterAggregation < Aggregation
3
+ include Subaggregated
4
+
5
+ attr_accessor :_evaluator, :_filter_name
6
+
7
+ def initialize(filter_name, *args, &block)
8
+ self._filter_name = filter_name
9
+ super
10
+ initialize_subaggregations FilterAggregationEvaluator.new, &block
11
+ end
12
+
13
+ def default_name
14
+ _filter_name
15
+ end
16
+
17
+ def build
18
+ # _subaggregations is a FilterAggregationEvaluator, so...
19
+ conditions = _subaggregations.build_conditions
20
+ body = { filter: conditions }
21
+ body.merge! build_subaggregations
22
+ body
23
+ end
24
+
25
+ def parse(response)
26
+ response_body = response
27
+ ret = { 'count' => response_body['doc_count'] }
28
+ ret.merge! parse_subaggregations(response_body)
29
+ ret
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ module Elasticated
2
+ class FilterAggregationEvaluator < QueryAggregations
3
+
4
+ include BlockEvaluation
5
+
6
+ attr_accessor :_conditions
7
+
8
+ def initialize
9
+ super
10
+ self._conditions = QueryConditions.new
11
+ end
12
+
13
+ def conditions(&block)
14
+ _conditions.evaluate block
15
+ end
16
+
17
+ def build_conditions
18
+ _conditions.build
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ module Elasticated
2
+ class GroupAggregation < TermsAggregation
3
+ include Subaggregated
4
+
5
+ def initialize(field, *args, &block)
6
+ super
7
+ initialize_subaggregations &block
8
+ end
9
+
10
+ def default_name
11
+ "group_by_#{field}"
12
+ end
13
+
14
+ def build
15
+ aggregation_struct = super
16
+ aggregation_struct.merge! build_subaggregations
17
+ aggregation_struct
18
+ end
19
+
20
+ def parse(response)
21
+ response['buckets'].inject({}) do |hash, bucket|
22
+ bucket_hash = { 'count' => bucket['doc_count'] }
23
+ bucket_hash.merge! parse_subaggregations(bucket)
24
+ hash.merge bucket['key'] => bucket_hash
25
+ end
26
+ end
27
+
28
+ end
29
+ end