Class: ElasticsearchDslBuilder::DSL::Search::Queries::Bool

Inherits:
Query
  • Object
show all
Defined in:
lib/elasticsearch_dsl_builder/dsl/search/queries/bool.rb

Overview

A compound query which matches documents based on combinations of queries

See the integration test for a working example.

Instance Attribute Summary

Attributes inherited from Query

#query, #type

Instance Method Summary collapse

Constructor Details

#initializeBool

class variables @minimum_should_match, @must, @must_not, @should, @filter



13
14
15
16
# File 'lib/elasticsearch_dsl_builder/dsl/search/queries/bool.rb', line 13

def initialize
  @type = :bool
  super()
end

Instance Method Details

#filter(query) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
# File 'lib/elasticsearch_dsl_builder/dsl/search/queries/bool.rb', line 51

def filter(query)
  raise ArgumentError, 'query must extend type Queries::Query' unless query.is_a?(Query)

  @filter ||= []
  hashed_query = query
  @filter << hashed_query unless @filter.include?(hashed_query)
  self
end

#minimum_should_match(value) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
# File 'lib/elasticsearch_dsl_builder/dsl/search/queries/bool.rb', line 18

def minimum_should_match(value)
  raise ArgumentError, 'minimum_should_match must be Numeric' unless value.is_a?(Numeric)
  @minimum_should_match = value
  self
end

#must(query) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
# File 'lib/elasticsearch_dsl_builder/dsl/search/queries/bool.rb', line 24

def must(query)
  raise ArgumentError, 'query must extend type Queries::Query' unless query.is_a?(Query)

  @must ||= []
  hashed_query = query
  @must << hashed_query unless @must.include?(hashed_query)
  self
end

#must_not(query) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
# File 'lib/elasticsearch_dsl_builder/dsl/search/queries/bool.rb', line 33

def must_not(query)
  raise ArgumentError, 'query must extend type Queries::Query' unless query.is_a?(Query)

  @must_not ||= []
  hashed_query = query
  @must_not << hashed_query unless @must_not.include?(hashed_query)
  self
end

#should(query) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
# File 'lib/elasticsearch_dsl_builder/dsl/search/queries/bool.rb', line 42

def should(query)
  raise ArgumentError, 'query must extend type Queries::Query' unless query.is_a?(Query)

  @should ||= []
  hashed_query = query
  @should << hashed_query unless @should.include?(hashed_query)
  self
end

#to_hashObject



60
61
62
63
64
65
66
67
68
# File 'lib/elasticsearch_dsl_builder/dsl/search/queries/bool.rb', line 60

def to_hash
  @query = {}
  @query.update(minimum_should_match: @minimum_should_match) if @minimum_should_match
  @query.update(must: @must.map(&:to_hash)) if @must
  @query.update(must_not: @must_not.map(&:to_hash)) if @must_not
  @query.update(should: @should.map(&:to_hash)) if @should
  @query.update(filter: @filter.map(&:to_hash)) if @filter
  super
end