esearch 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/.gitignore +5 -0
  2. data/.rspec +6 -0
  3. data/.travis.yml +19 -0
  4. data/Changelog.md +27 -0
  5. data/Gemfile +6 -0
  6. data/Gemfile.devtools +60 -0
  7. data/Guardfile +18 -0
  8. data/LICENSE +20 -0
  9. data/README.md +93 -0
  10. data/Rakefile +2 -0
  11. data/TODO +3 -0
  12. data/config/devtools.yml +2 -0
  13. data/config/flay.yml +3 -0
  14. data/config/flog.yml +3 -0
  15. data/config/mutant.yml +3 -0
  16. data/config/reek.yml +103 -0
  17. data/config/yardstick.yml +2 -0
  18. data/esearch.gemspec +26 -0
  19. data/lib/esearch.rb +43 -0
  20. data/lib/esearch/cluster.rb +85 -0
  21. data/lib/esearch/command.rb +158 -0
  22. data/lib/esearch/command/cluster.rb +28 -0
  23. data/lib/esearch/command/document.rb +111 -0
  24. data/lib/esearch/command/exist.rb +37 -0
  25. data/lib/esearch/command/index.rb +66 -0
  26. data/lib/esearch/command/search.rb +24 -0
  27. data/lib/esearch/command/status.rb +24 -0
  28. data/lib/esearch/connection.rb +36 -0
  29. data/lib/esearch/document.rb +53 -0
  30. data/lib/esearch/index.rb +61 -0
  31. data/lib/esearch/indices.rb +52 -0
  32. data/lib/esearch/mixin.rb +111 -0
  33. data/lib/esearch/presenter.rb +43 -0
  34. data/lib/esearch/presenter/aspect.rb +17 -0
  35. data/lib/esearch/presenter/aspect/range.rb +63 -0
  36. data/lib/esearch/presenter/aspect/term.rb +19 -0
  37. data/lib/esearch/presenter/cluster.rb +84 -0
  38. data/lib/esearch/presenter/document.rb +102 -0
  39. data/lib/esearch/presenter/facet.rb +72 -0
  40. data/lib/esearch/presenter/hit.rb +70 -0
  41. data/lib/esearch/presenter/hits.rb +60 -0
  42. data/lib/esearch/presenter/index.rb +23 -0
  43. data/lib/esearch/presenter/search.rb +32 -0
  44. data/lib/esearch/presenter/status.rb +18 -0
  45. data/lib/esearch/request.rb +90 -0
  46. data/lib/esearch/type.rb +40 -0
  47. data/spec/integration/esearch/spike_spec.rb +50 -0
  48. data/spec/spec_helper.rb +65 -0
  49. data/spec/support/example_group_methods.rb +7 -0
  50. data/spec/support/ice_nine_config.rb +6 -0
  51. data/spec/unit/esearch/cluster/class_methods/connect_spec.rb +16 -0
  52. data/spec/unit/esearch/cluster/health_spec.rb +10 -0
  53. data/spec/unit/esearch/cluster/index_spec.rb +11 -0
  54. data/spec/unit/esearch/cluster/indices_spec.rb +11 -0
  55. data/spec/unit/esearch/cluster/path_spec.rb +11 -0
  56. data/spec/unit/esearch/command/class_methods/run_spec.rb +16 -0
  57. data/spec/unit/esearch/command/cluster/health/run_spec.rb +14 -0
  58. data/spec/unit/esearch/command/document/delete/run_spec.rb +13 -0
  59. data/spec/unit/esearch/command/document/get/result_spec.rb +27 -0
  60. data/spec/unit/esearch/command/document/index/create/run_spec.rb +17 -0
  61. data/spec/unit/esearch/command/document/index/run_create_spec.rb +17 -0
  62. data/spec/unit/esearch/command/document/index/run_spec.rb +15 -0
  63. data/spec/unit/esearch/command/document/index/run_update_spec.rb +15 -0
  64. data/spec/unit/esearch/command/document/index/update/run_spec.rb +15 -0
  65. data/spec/unit/esearch/command/exist/result_spec.rb +39 -0
  66. data/spec/unit/esearch/command/index/create/run_spec.rb +14 -0
  67. data/spec/unit/esearch/command/index/delete/run_spec.rb +13 -0
  68. data/spec/unit/esearch/command/index/refresh/run_spec.rb +13 -0
  69. data/spec/unit/esearch/command/result_spec.rb +68 -0
  70. data/spec/unit/esearch/command/search/run_spec.rb +14 -0
  71. data/spec/unit/esearch/command/status/run_spec.rb +13 -0
  72. data/spec/unit/esearch/connection/class_methods/build_spec.rb +29 -0
  73. data/spec/unit/esearch/connection/run_spec.rb +36 -0
  74. data/spec/unit/esearch/document/connection_spec.rb +12 -0
  75. data/spec/unit/esearch/document/delete_spec.rb +12 -0
  76. data/spec/unit/esearch/document/get_spec.rb +12 -0
  77. data/spec/unit/esearch/index/create_spec.rb +12 -0
  78. data/spec/unit/esearch/index/delete_spec.rb +11 -0
  79. data/spec/unit/esearch/index/type_spec.rb +12 -0
  80. data/spec/unit/esearch/indices/all/path_spec.rb +12 -0
  81. data/spec/unit/esearch/mixin/document/index_create_spec.rb +31 -0
  82. data/spec/unit/esearch/mixin/document/index_spec.rb +31 -0
  83. data/spec/unit/esearch/mixin/document/index_update_spec.rb +31 -0
  84. data/spec/unit/esearch/mixin/exist/exist_predicate_spec.rb +16 -0
  85. data/spec/unit/esearch/mixin/index/refresh_spec.rb +16 -0
  86. data/spec/unit/esearch/mixin/index/status_spec.rb +16 -0
  87. data/spec/unit/esearch/mixin/search/search_spec.rb +18 -0
  88. data/spec/unit/esearch/presenter/aspect/range/from_spec.rb +24 -0
  89. data/spec/unit/esearch/presenter/aspect/range/to_spec.rb +24 -0
  90. data/spec/unit/esearch/presenter/class_methods/new_spec.rb +37 -0
  91. data/spec/unit/esearch/presenter/facet/build_spec.rb +26 -0
  92. data/spec/unit/esearch/presenter/facet/class_methods/build_spec.rb +26 -0
  93. data/spec/unit/esearch/presenter/hit/fields_spec.rb +24 -0
  94. data/spec/unit/esearch/presenter/hit/source_spec.rb +24 -0
  95. data/spec/unit/esearch/presenter/hits/each_spec.rb +15 -0
  96. data/spec/unit/esearch/presenter/hits/size_spec.rb +13 -0
  97. data/spec/unit/esearch/request/initialize_spec.rb +39 -0
  98. data/spec/unit/esearch/request/run_spec.rb +39 -0
  99. data/spec/unit/esearch/type/connection_spec.rb +15 -0
  100. data/spec/unit/esearch/type/document_spec.rb +12 -0
  101. metadata +330 -0
@@ -0,0 +1,19 @@
1
+ module Esearch
2
+ class Presenter
3
+ class Aspect
4
+
5
+ # Aspect of a term facet
6
+ class Term < self
7
+
8
+ # Return value of aspect
9
+ #
10
+ # @return [String]
11
+ #
12
+ # @api private
13
+ #
14
+ expose_primitive(:term, :value)
15
+
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,84 @@
1
+ module Esearch
2
+ class Presenter
3
+ # Base class for cluster presenters
4
+ class Cluster < self
5
+
6
+ # Presenter for cluster health
7
+ class Health < self
8
+
9
+ # Return cluster name
10
+ #
11
+ # @return [String]
12
+ #
13
+ # @api private
14
+ #
15
+ expose_primitive('name')
16
+
17
+ # Return status string
18
+ #
19
+ # @return [String]
20
+ #
21
+ # @api private
22
+ #
23
+ expose_primitive('status')
24
+
25
+ # Return number of nodes
26
+ #
27
+ # @return [String]
28
+ #
29
+ # @api private
30
+ #
31
+ expose_primitive('number_of_nodes')
32
+
33
+ # Return number of data nodes
34
+ #
35
+ # @return [Fixnum]
36
+ #
37
+ # @api private
38
+ #
39
+ expose_primitive('number_of_data_nodes')
40
+
41
+ # Return number of active primary shards
42
+ #
43
+ # @return [Fixnum]
44
+ #
45
+ # @api private
46
+ #
47
+ expose_primitive('active_primary_shards')
48
+
49
+ # Return number of active shards
50
+ #
51
+ # @return [Fixnum]
52
+ #
53
+ # @api private
54
+ #
55
+ expose_primitive('active_shards')
56
+
57
+ # Return number of relocating shards
58
+ #
59
+ # @return [Fixnum]
60
+ #
61
+ # @api private
62
+ #
63
+ expose_primitive('relocating_shards')
64
+
65
+ # Return number of initializing shards
66
+ #
67
+ # @return [Fixnum]
68
+ #
69
+ # @api private
70
+ #
71
+ expose_primitive('initializing_shards')
72
+
73
+ # Return number of unassigned shards
74
+ #
75
+ # @return [Fixnum]
76
+ #
77
+ # @api private
78
+ #
79
+ expose_primitive('unassigned_shards')
80
+
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,102 @@
1
+ module Esearch
2
+ class Presenter
3
+ # Base class result of document command presenters
4
+ class Document < self
5
+
6
+ # Return index name
7
+ #
8
+ # @return [String]
9
+ #
10
+ # @api private
11
+ #
12
+ expose_primitive(:_index, :index)
13
+
14
+ # Return type name
15
+ #
16
+ # @return [String]
17
+ #
18
+ # @api private
19
+ #
20
+ expose_primitive(:_type, :type)
21
+
22
+ # Return id
23
+ #
24
+ # @return [String]
25
+ #
26
+ # @api private
27
+ #
28
+ expose_primitive(:_id, :id)
29
+
30
+ # Return version
31
+ #
32
+ # @return [Fixnum]
33
+ #
34
+ # @api private
35
+ #
36
+ expose_primitive(:_version, :version)
37
+
38
+ # Base class for document operations
39
+ class Operation < self
40
+
41
+ # Test for successful operation
42
+ #
43
+ # @return [true]
44
+ # if successful
45
+ #
46
+ # @return [false]
47
+ # otherwise
48
+ #
49
+ # @api private
50
+ #
51
+ expose_primitive(:ok, :ok?)
52
+
53
+ # Index operation
54
+ class Index < self
55
+ end
56
+
57
+ # Update operation
58
+ class Update < self
59
+ end
60
+
61
+ # Delete operation
62
+ class Delete < self
63
+
64
+ # Test if document was found
65
+ #
66
+ # @return [true]
67
+ # if document was found
68
+ #
69
+ # @return [false]
70
+ # otherwise
71
+ #
72
+ # @api private
73
+ #
74
+ expose_primitive(:found, :found?)
75
+
76
+ end
77
+ end
78
+
79
+ # Presenter for document get result
80
+ class Get < self
81
+
82
+ # Return source document
83
+ #
84
+ # @return [Hash]
85
+ #
86
+ # @api private
87
+ #
88
+ expose_primitive(:_source, :source)
89
+
90
+ # Return requested fields
91
+ #
92
+ # @return [Hash]
93
+ #
94
+ # @api private
95
+ #
96
+ expose_primitive(:fields)
97
+
98
+ end
99
+
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,72 @@
1
+ module Esearch
2
+ class Presenter
3
+
4
+ # Base class for facets in results
5
+ class Facet < self
6
+
7
+ TYPE_KEY = '_type'.freeze
8
+
9
+ # Build facet from raw
10
+ #
11
+ # @param [Hash] raw
12
+ #
13
+ # @return [Facet]
14
+ #
15
+ # @api private
16
+ #
17
+ def self.build(raw)
18
+ type = raw.fetch(TYPE_KEY)
19
+ get(type).new(raw)
20
+ end
21
+
22
+ # Get class for type
23
+ #
24
+ # @param [String] type
25
+ #
26
+ # @return [Class:Facet]
27
+ #
28
+ # @api private
29
+ #
30
+ def self.get(type)
31
+ REGISTRY.fetch(type) do
32
+ raise "Facet with type #{type.inspect} is not known"
33
+ end
34
+ end
35
+ private_class_method :get
36
+
37
+ # Enumerate aspects
38
+ #
39
+ # @return [Enumerable<Aspect>]
40
+ #
41
+ # @api private
42
+ #
43
+ def aspects
44
+ util = self.class
45
+ raw.fetch(util::FACET_KEY).map do |item|
46
+ util::ASPECT_CLASS.new(item)
47
+ end
48
+ end
49
+ memoize :aspects
50
+
51
+ private
52
+
53
+ # Facet that returns range counts
54
+ class Range < self
55
+ ASPECT_CLASS = Aspect::Range
56
+ FACET_KEY = 'ranges'.freeze
57
+ end
58
+
59
+ # Facet that returns term counts
60
+ class Terms < self
61
+ ASPECT_CLASS = Aspect::Term
62
+ FACET_KEY = 'terms'.freeze
63
+ end
64
+
65
+ REGISTRY = {
66
+ 'range' => Range,
67
+ 'terms' => Terms
68
+ }.freeze
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,70 @@
1
+ module Esearch
2
+ class Presenter
3
+
4
+ # Presenter for an hit
5
+ class Hit < self
6
+
7
+ # Return source field if present
8
+ #
9
+ # @return [Hash]
10
+ # if present
11
+ #
12
+ # @return [nil]
13
+ # otherwise
14
+ #
15
+ # @api private
16
+ #
17
+ def source
18
+ raw['_source']
19
+ end
20
+
21
+ # Return fields if present
22
+ #
23
+ # @return [Hash]
24
+ # if present
25
+ #
26
+ # @return [nil]
27
+ # otherwise
28
+ #
29
+ # @api private
30
+ #
31
+ def fields
32
+ raw['fields']
33
+ end
34
+
35
+ # Return index name
36
+ #
37
+ # @return [String]
38
+ #
39
+ # @api private
40
+ #
41
+ expose_primitive(:_index, :index_name)
42
+
43
+ # Return type name
44
+ #
45
+ # @return [Type]
46
+ #
47
+ # @api private
48
+ #
49
+ expose_primitive(:_type, :type_name)
50
+
51
+ # Return id
52
+ #
53
+ # @return [String]
54
+ #
55
+ # @api private
56
+ #
57
+ expose_primitive(:_id, :id)
58
+
59
+ # Return score
60
+ #
61
+ # @return [Float]
62
+ #
63
+ # @api private
64
+ #
65
+ expose_primitive(:_score, :score)
66
+
67
+ end
68
+ end
69
+ end
70
+
@@ -0,0 +1,60 @@
1
+ module Esearch
2
+ class Presenter
3
+
4
+ # Presenter for elasticsearch result hits
5
+ class Hits < self
6
+ include Enumerable
7
+
8
+ # Enumerate contents
9
+ #
10
+ # @return [self]
11
+ # if block given
12
+ #
13
+ # @return [Enumerator<Hit>]
14
+ # otherwise
15
+ #
16
+ # @api private
17
+ #
18
+ def each(&block)
19
+ return to_enum unless block_given?
20
+
21
+ hits.each do |hit|
22
+ yield Hit.new(hit)
23
+ end
24
+
25
+ self
26
+ end
27
+
28
+ # Return amount of hits in this request
29
+ #
30
+ # @return [Fixnum]
31
+ #
32
+ # @api private
33
+ #
34
+ def size
35
+ hits.size
36
+ end
37
+
38
+ # Return total amount of hits in the query
39
+ #
40
+ # @return [Fixnum]
41
+ #
42
+ # @api private
43
+ #
44
+ expose_primitive('total')
45
+
46
+ private
47
+
48
+ # Return raw hits
49
+ #
50
+ # @return [Array]
51
+ #
52
+ # @api private
53
+ #
54
+ expose_primitive(:hits)
55
+ private :hits
56
+
57
+ end
58
+ end
59
+ end
60
+
@@ -0,0 +1,23 @@
1
+ module Esearch
2
+ class Presenter
3
+
4
+ # Base class for index operation result presenters
5
+ class Index < self
6
+
7
+ # Create operation presenter
8
+ class Create < self
9
+ expose_primitive :ok, :ok?
10
+ end
11
+
12
+ # Delete operation presenter
13
+ class Delete < self
14
+ expose_primitive :ok, :ok?
15
+ end
16
+
17
+ # Refresh operation presenter
18
+ class Refresh < self
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ module Esearch
2
+ class Presenter
3
+ # Presenter for search result
4
+ class Search < self
5
+
6
+ # Return facets
7
+ #
8
+ # @return [Hash]
9
+ #
10
+ # @api private
11
+ #
12
+ def facets
13
+ raw.fetch('facets', {}).each_with_object({}) do |(name, body), hash|
14
+ hash[name] = Facet.build(body)
15
+ end
16
+ end
17
+ memoize :facets
18
+
19
+ # Return hits
20
+ #
21
+ # @return [Enumerable<Hash>]
22
+ #
23
+ # @api private
24
+ #
25
+ def hits
26
+ Hits.new(raw.fetch('hits'))
27
+ end
28
+ memoize :hits
29
+
30
+ end
31
+ end
32
+ end