elasticsearch-dsl 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e9bb408fa97be4d12b615340b7fa44090287e08
4
- data.tar.gz: d4c70bf0fcc2864c11226855c471dc921d23687c
3
+ metadata.gz: caa549b0dcf60175f255e9e4274863dba56808dc
4
+ data.tar.gz: 7ec42e3ede3c15f0904870338bc108ef5e51d742
5
5
  SHA512:
6
- metadata.gz: b0cdfe1bfb6786cffd3610c4fd9687fb86baf6b51c49d0282d867ebb4cf91fbd0aaa1c0594a572e01ad0ca34a2ce5fc56feff1761a319943fead579a1f4d1628
7
- data.tar.gz: d721ec3d8824fff4598984d0292c07df508134d431264fe51a54404dec837e45de2d7ccb2fa4a6c07faf5000ee4842693ed80e344fb59118566c4b4895cae115
6
+ metadata.gz: 551986fb989ba8424a9baab7bf9ba435c4cad33d8746652ad1ab924fdb8ccd4bb33a08c1cb517c2edbfbe6a38a64f39829b45166e9ef73da1b8ecb237392d25d
7
+ data.tar.gz: 9c9d61b3135d4b8f2ed9757d8f383f3bd8ea106ff08d50d00833dbb07ad1e545113d5bad8d1d7466070537240583a6a0ae3b50fe1c1c031c65f0ffe7728e53c3
data/Gemfile CHANGED
@@ -3,18 +3,18 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in elasticsearch-dsl.gemspec
4
4
  gemspec
5
5
 
6
- if File.exists? File.expand_path("../../elasticsearch/elasticsearch.gemspec", __FILE__)
6
+ if File.exist? File.expand_path("../../elasticsearch/elasticsearch.gemspec", __FILE__)
7
7
  gem 'elasticsearch', :path => File.expand_path("../../elasticsearch", __FILE__), :require => false
8
8
  end
9
9
 
10
- if File.exists? File.expand_path("../../elasticsearch-transport", __FILE__)
10
+ if File.exist? File.expand_path("../../elasticsearch-transport", __FILE__)
11
11
  gem 'elasticsearch-transport', :path => File.expand_path("../../elasticsearch-transport", __FILE__), :require => true
12
12
  end
13
13
 
14
- if File.exists? File.expand_path("../../elasticsearch-api", __FILE__)
14
+ if File.exist? File.expand_path("../../elasticsearch-api", __FILE__)
15
15
  gem 'elasticsearch-api', :path => File.expand_path("../../elasticsearch-api", __FILE__), :require => false
16
16
  end
17
17
 
18
- if File.exists? File.expand_path("../../elasticsearch-extensions", __FILE__)
18
+ if File.exist? File.expand_path("../../elasticsearch-extensions", __FILE__)
19
19
  gem 'elasticsearch-extensions', :path => File.expand_path("../../elasticsearch-extensions", __FILE__), :require => false
20
20
  end
data/Rakefile CHANGED
@@ -11,13 +11,15 @@ namespace :test do
11
11
  Rake::TestTask.new(:unit) do |test|
12
12
  test.libs << 'lib' << 'test'
13
13
  test.test_files = FileList["test/unit/**/*_test.rb"]
14
- # test.verbose = true
15
- # test.warning = true
14
+ test.verbose = false
15
+ test.warning = false
16
16
  end
17
17
 
18
18
  Rake::TestTask.new(:integration) do |test|
19
19
  test.libs << 'lib' << 'test'
20
20
  test.test_files = FileList["test/integration/**/*_test.rb"]
21
+ test.verbose = false
22
+ test.warning = false
21
23
  end
22
24
 
23
25
  desc "Run unit and integration tests"
@@ -116,7 +116,7 @@ module Elasticsearch
116
116
  # @return [self]
117
117
  #
118
118
  def aggregation(*args, &block)
119
- @aggregations ||= {}
119
+ @aggregations ||= AggregationsCollection.new
120
120
 
121
121
  if block
122
122
  @aggregations.update args.first => Aggregation.new(*args, &block)
@@ -6,6 +6,12 @@ module Elasticsearch
6
6
  #
7
7
  module Aggregations;end
8
8
 
9
+ class AggregationsCollection < Hash
10
+ def to_hash
11
+ @hash ||= Hash[map { |k,v| [k, v.to_hash] }]
12
+ end
13
+ end
14
+
9
15
  # Wraps the `aggregations` part of a search definition
10
16
  #
11
17
  # @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations.html
@@ -33,7 +33,7 @@ module Elasticsearch
33
33
  # @return [self]
34
34
  #
35
35
  def aggregation(*args, &block)
36
- @aggregations ||= {}
36
+ @aggregations ||= AggregationsCollection.new
37
37
  @aggregations.update args.first => Aggregation.new(*args, &block)
38
38
  self
39
39
  end
@@ -50,8 +50,7 @@ module Elasticsearch
50
50
  @hash = { name => @args } unless @hash && @hash[name] && ! @hash[name].empty?
51
51
 
52
52
  if @aggregations
53
- @hash[:aggregations] = {}
54
- @aggregations.map { |name, value| @hash[:aggregations][name] = value.to_hash }
53
+ @hash[:aggregations] = @aggregations.to_hash
55
54
  end
56
55
  @hash
57
56
  end
@@ -43,6 +43,7 @@ module Elasticsearch
43
43
  # # => {:mycustomquery=>{:foo=>{:custom=>"TEST"}}}
44
44
  #
45
45
  def option_method(name, block=nil)
46
+ option_methods << name
46
47
  if block
47
48
  self.__send__ :define_method, name, &block
48
49
  else
@@ -102,6 +103,10 @@ module Elasticsearch
102
103
  def name=(value)
103
104
  @name = value.to_sym
104
105
  end
106
+
107
+ def option_methods
108
+ @option_methods ||= []
109
+ end
105
110
  end
106
111
 
107
112
  module InstanceMethods
@@ -36,6 +36,9 @@ module Elasticsearch
36
36
  class Bool
37
37
  include BaseComponent
38
38
 
39
+ option_method :minimum_should_match
40
+ option_method :boost
41
+
39
42
  def must(*args, &block)
40
43
  @hash[name][:must] ||= []
41
44
  value = Query.new(*args, &block).to_hash
@@ -58,7 +61,9 @@ module Elasticsearch
58
61
  end
59
62
 
60
63
  def filter(*args, &block)
61
- @filter = block ? Filter.new(*args, &block) : args.first
64
+ @hash[name][:filter] ||= []
65
+ value = Filter.new(*args, &block).to_hash
66
+ @hash[name][:filter].push(value).flatten! unless @hash[name][:filter].include?(value)
62
67
  self
63
68
  end
64
69
 
@@ -71,11 +76,6 @@ module Elasticsearch
71
76
  @hash[name] = @args unless @args.nil? || @args.empty?
72
77
  end
73
78
 
74
- if @filter
75
- _filter = @filter.respond_to?(:to_hash) ? @filter.to_hash : @filter
76
- @hash[name].update(filter: _filter)
77
- end
78
-
79
79
  @hash
80
80
  end
81
81
  end
@@ -0,0 +1,44 @@
1
+ module Elasticsearch
2
+ module DSL
3
+ module Search
4
+ module Queries
5
+
6
+ # Returns documents that have at least one non-null value in the field.
7
+ #
8
+ # @example Find documents with non-empty "name" property
9
+ #
10
+ # search do
11
+ # query do
12
+ # exists do
13
+ # field 'name'
14
+ # end
15
+ # end
16
+ # end
17
+ #
18
+ # @note The "Exists" query can be used as a "Missing" query in a "Bool" query "Must Not" context.
19
+ #
20
+ # @example Find documents with an empty "name" property
21
+ #
22
+ # search do
23
+ # query do
24
+ # bool do
25
+ # must_not do
26
+ # exists do
27
+ # field 'name'
28
+ # end
29
+ # end
30
+ # end
31
+ # end
32
+ # end
33
+ #
34
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-exists-query.html
35
+ #
36
+ class Exists
37
+ include BaseComponent
38
+
39
+ option_method :field
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -24,9 +24,17 @@ module Elasticsearch
24
24
 
25
25
  option_method :query
26
26
  option_method :operator
27
+ option_method :minimum_should_match
27
28
  option_method :type
28
29
  option_method :boost
29
30
  option_method :fuzziness
31
+ option_method :prefix_length
32
+ option_method :max_expansions
33
+ option_method :fuzzy_rewrite
34
+ option_method :analyzer
35
+ option_method :lenient
36
+ option_method :zero_terms_query
37
+ option_method :cutoff_frequency
30
38
  end
31
39
 
32
40
  end
@@ -10,7 +10,7 @@ module Elasticsearch
10
10
  # search do
11
11
  # query do
12
12
  # more_like_this do
13
- # like_text 'Eyjafjallajökull'
13
+ # like ['Eyjafjallajökull']
14
14
  # fields [:title, :abstract, :content]
15
15
  # end
16
16
  # end
@@ -22,7 +22,7 @@ module Elasticsearch
22
22
  # search do
23
23
  # query do
24
24
  # more_like_this do
25
- # ids [1, 2, 3]
25
+ # like [{_id: 1}, {_id: 2}, {_id: 3}]
26
26
  # fields [:title, :abstract]
27
27
  # end
28
28
  # end
@@ -33,12 +33,18 @@ module Elasticsearch
33
33
  class MoreLikeThis
34
34
  include BaseComponent
35
35
 
36
- option_method :fields
36
+ # like/unlike is since 2.0.0
37
+ option_method :like
38
+ option_method :unlike
39
+
40
+ # before 2.0.0 the following 3 options were available
37
41
  option_method :like_text
38
- option_method :min_term_freq
39
- option_method :max_query_terms
40
42
  option_method :docs
41
43
  option_method :ids
44
+
45
+ option_method :fields
46
+ option_method :min_term_freq
47
+ option_method :max_query_terms
42
48
  option_method :include
43
49
  option_method :exclude
44
50
  option_method :percent_terms_to_match
@@ -25,6 +25,7 @@ module Elasticsearch
25
25
 
26
26
  option_method :path
27
27
  option_method :score_mode
28
+ option_method :inner_hits
28
29
 
29
30
  # DSL method for building the `query` part of the query definition
30
31
  #
@@ -42,6 +42,7 @@ module Elasticsearch
42
42
  option_method :locale
43
43
  option_method :use_dis_max
44
44
  option_method :tie_breaker
45
+ option_method :time_zone
45
46
  end
46
47
 
47
48
  end
@@ -1,5 +1,5 @@
1
1
  module Elasticsearch
2
2
  module DSL
3
- VERSION = "0.1.4"
3
+ VERSION = "0.1.5"
4
4
  end
5
5
  end
@@ -47,6 +47,22 @@ module Elasticsearch
47
47
  assert_equal 2, response['hits']['total']
48
48
  assert_equal 'Test', response['hits']['hits'][0]['_source']['title']
49
49
  end
50
+
51
+ should "find the document with a filter" do
52
+ skip "Not supported on this Elasticsearch version" unless @version > '2'
53
+
54
+ response = @client.search index: 'test', body: search {
55
+ query do
56
+ bool do
57
+ filter { terms tags: ['one'] }
58
+ filter { terms tags: ['two'] }
59
+ end
60
+ end
61
+ }.to_hash
62
+
63
+ assert_equal 1, response['hits']['total']
64
+ assert_equal 'Rest', response['hits']['hits'][0]['_source']['title']
65
+ end
50
66
  end
51
67
 
52
68
  end
@@ -52,6 +52,7 @@ module Elasticsearch
52
52
  end
53
53
 
54
54
  @client = Elasticsearch::Client.new host: "localhost:#{@port}", logger: @logger
55
+ @version = @client.info['version']['number']
55
56
  end
56
57
 
57
58
  def teardown
@@ -27,17 +27,38 @@ module Elasticsearch
27
27
  assert_equal( { bool: {must: [ {match: { foo: 'bar' }} ] } }, subject.to_hash )
28
28
  end
29
29
 
30
+ should "have option methods" do
31
+ subject = Bool.new do
32
+ should { term tag: 'wow' }
33
+ should { term tag: 'elasticsearch' }
34
+
35
+ minimum_should_match 1
36
+ boost 1.0
37
+ end
38
+
39
+ assert_equal( { bool:
40
+ {
41
+ minimum_should_match: 1,
42
+ boost: 1.0,
43
+ should: [ {term: { tag: 'wow' }}, {term: { tag: 'elasticsearch' }} ]
44
+ }
45
+ },
46
+ subject.to_hash )
47
+ end
48
+
30
49
  should "take a block with multiple methods" do
31
50
  subject = Bool.new do
32
51
  must { match foo: 'bar' }
33
52
  must_not { match moo: 'bam' }
34
53
  should { match xoo: 'bax' }
54
+ filter { term zoo: 'baz'}
35
55
  end
36
56
 
37
57
  assert_equal( { bool:
38
58
  { must: [ {match: { foo: 'bar' }} ],
39
59
  must_not: [ {match: { moo: 'bam' }} ],
40
- should: [ {match: { xoo: 'bax' }} ]
60
+ should: [ {match: { xoo: 'bax' }} ],
61
+ filter: [ {term: { zoo: 'baz' }}]
41
62
  }
42
63
  },
43
64
  subject.to_hash )
@@ -92,13 +113,23 @@ module Elasticsearch
92
113
  subject.to_hash )
93
114
  end
94
115
 
95
- should "allow adding a filter" do
116
+ should "combine chained filters" do
96
117
  subject = Bool.new
97
- subject.filter do
98
- term foo: 'bar'
99
- end
118
+ subject.
119
+ filter {
120
+ term foo: "bar"
121
+ }
122
+ subject.filter {
123
+ term zoo: "baz"
124
+ }
100
125
 
101
- assert_equal( { bool: { filter: { term: { foo: "bar" } } } }, subject.to_hash)
126
+ assert_equal( { bool:
127
+ { filter: [
128
+ { term: { foo: "bar"}},
129
+ { term: { zoo: "baz"}}
130
+ ] }
131
+ },
132
+ subject.to_hash)
102
133
  end
103
134
 
104
135
  should "be chainable" do
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ module Elasticsearch
4
+ module Test
5
+ module Queries
6
+ class ExistsTest < ::Test::Unit::TestCase
7
+ include Elasticsearch::DSL::Search::Queries
8
+
9
+ context "Exists query" do
10
+ subject { Exists.new }
11
+
12
+ should "be converted to a Hash" do
13
+ assert_equal({ exists: {} }, subject.to_hash)
14
+ end
15
+
16
+ should "have option methods" do
17
+ subject = Exists.new
18
+
19
+ subject.field 'bar'
20
+
21
+ assert_equal %w[ field ],
22
+ subject.to_hash[:exists].keys.map(&:to_s).sort
23
+ assert_equal 'bar', subject.to_hash[:exists][:field]
24
+ end
25
+
26
+ should "take a block" do
27
+ subject = Exists.new do
28
+ field 'bar'
29
+ end
30
+ assert_equal({ exists: { field: 'bar' } }, subject.to_hash)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -19,10 +19,12 @@ module Elasticsearch
19
19
  subject.path 'bar'
20
20
  subject.score_mode 'bar'
21
21
  subject.query 'bar'
22
+ subject.inner_hits({ size: 1 })
22
23
 
23
- assert_equal %w[ path query score_mode ],
24
+ assert_equal %w[ inner_hits path query score_mode ],
24
25
  subject.to_hash[:nested].keys.map(&:to_s).sort
25
26
  assert_equal 'bar', subject.to_hash[:nested][:path]
27
+ assert_equal({ size: 1 }, subject.to_hash[:nested][:inner_hits])
26
28
  end
27
29
 
28
30
  should "take the query as a Hash" do
@@ -36,8 +36,9 @@ module Elasticsearch
36
36
  subject.locale 'bar'
37
37
  subject.use_dis_max 'bar'
38
38
  subject.tie_breaker 'bar'
39
+ subject.time_zone 'bar'
39
40
 
40
- assert_equal %w[ allow_leading_wildcard analyze_wildcard analyzer auto_generate_phrase_queries boost default_field default_operator enable_position_increments fields fuzziness fuzzy_max_expansions fuzzy_prefix_length lenient locale lowercase_expanded_terms minimum_should_match phrase_slop query tie_breaker use_dis_max ],
41
+ assert_equal %w[ allow_leading_wildcard analyze_wildcard analyzer auto_generate_phrase_queries boost default_field default_operator enable_position_increments fields fuzziness fuzzy_max_expansions fuzzy_prefix_length lenient locale lowercase_expanded_terms minimum_should_match phrase_slop query tie_breaker time_zone use_dis_max ],
41
42
  subject.to_hash[:query_string][:foo].keys.map(&:to_s).sort
42
43
  assert_equal 'bar', subject.to_hash[:query_string][:foo][:query]
43
44
  end
@@ -64,6 +64,16 @@ module Elasticsearch
64
64
  assert_equal({ dummy_component_with_option_method: { foo: { bar: 'BAM' } } }, subject.to_hash)
65
65
  end
66
66
 
67
+ should "keep track of option methods" do
68
+ class DummyComponentWithCustomOptionMethod
69
+ include Elasticsearch::DSL::Search::BaseComponent
70
+ option_method :foo
71
+ end
72
+
73
+ subject = DummyComponentWithCustomOptionMethod
74
+ assert_includes subject.option_methods, :foo
75
+ end
76
+
67
77
  should "have an option method without args" do
68
78
  class DummyComponentWithOptionMethod
69
79
  include Elasticsearch::DSL::Search::BaseComponent
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch-dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karel Minarik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-21 00:00:00.000000000 Z
11
+ date: 2017-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -305,6 +305,7 @@ files:
305
305
  - lib/elasticsearch/dsl/search/queries/common.rb
306
306
  - lib/elasticsearch/dsl/search/queries/constant_score.rb
307
307
  - lib/elasticsearch/dsl/search/queries/dis_max.rb
308
+ - lib/elasticsearch/dsl/search/queries/exists.rb
308
309
  - lib/elasticsearch/dsl/search/queries/filtered.rb
309
310
  - lib/elasticsearch/dsl/search/queries/function_score.rb
310
311
  - lib/elasticsearch/dsl/search/queries/fuzzy.rb
@@ -428,6 +429,7 @@ files:
428
429
  - test/unit/queries/common_test.rb
429
430
  - test/unit/queries/constant_score_test.rb
430
431
  - test/unit/queries/dis_max_test.rb
432
+ - test/unit/queries/exists_test.rb
431
433
  - test/unit/queries/filtered_test.rb
432
434
  - test/unit/queries/function_score_test.rb
433
435
  - test/unit/queries/fuzzy_like_this_field_test.rb
@@ -584,6 +586,7 @@ test_files:
584
586
  - test/unit/queries/common_test.rb
585
587
  - test/unit/queries/constant_score_test.rb
586
588
  - test/unit/queries/dis_max_test.rb
589
+ - test/unit/queries/exists_test.rb
587
590
  - test/unit/queries/filtered_test.rb
588
591
  - test/unit/queries/function_score_test.rb
589
592
  - test/unit/queries/fuzzy_like_this_field_test.rb
@@ -627,4 +630,3 @@ test_files:
627
630
  - test/unit/search_suggest_test.rb
628
631
  - test/unit/search_test.rb
629
632
  - test/unit/utils_test.rb
630
- has_rdoc: