Class: Queries::BoolQueryBuilder

Inherits:
QueryBuilder show all
Defined in:
lib/queries/bool_query_builder.rb

Overview

A query that matches documents matching boolean combinations of other queries. The bool query maps to Lucene BooleanQuery. It is built using one or more boolean clauses, each clause with a typed occurrence

QueryComponents
   should: queries that may appear in the matching documents and
            will contribute to scoring
   must: queries that must appear in the matching documents and
         will contribute to scoring
   must_not: queries that must not appear in the matching documents and
             will contribute to scoring
   filter: queries that must appear in the matching documents but
           don't contribute to scoring
   minimum_should_match: minimum should match as interger or percentage

Constant Summary collapse

NAME =
'bool'

Instance Method Summary collapse

Methods inherited from QueryBuilder

#boost

Methods included from AttributesReader

#attributes

Methods included from AbstractQueryBuilder

#do_equals?, #name

Constructor Details

#initializeBoolQueryBuilder

Returns a new instance of BoolQueryBuilder.



22
23
24
25
26
27
28
# File 'lib/queries/bool_query_builder.rb', line 22

def initialize
  @should_queries = []
  @must_queries = []
  @mustnot_queries = []
  @filter_queries = []
  @minimum_should_match = nil
end

Instance Method Details

#clauses?Boolean

Returns true iff this query builder has at least one should, must, must not or filter clause

Returns:

  • (Boolean)

    returns true iff this query builder has at least one should, must, must not or filter clause



140
141
142
143
144
145
146
# File 'lib/queries/bool_query_builder.rb', line 140

def clauses?
  shuld_clause = @should_queries.present?
  must_clause = @must_queries.present?
  mustnot_clause = @mustnot_queries.present?
  filter_clause = @filter_queries.present?
  shuld_clause && must_clause && mustnot_clause && filter_clause
end

#filter(query_builder) ⇒ BoolQueryBuilder

adds a filter query

Parameters:

Returns:



54
55
56
57
58
59
60
61
# File 'lib/queries/bool_query_builder.rb', line 54

def filter(query_builder)
  if query_builder.is_a?(Array)
    @filter_queries += query_builder
  else
    @filter_queries.append(query_builder)
  end
  self
end

#minimum_should_match(value) ⇒ BoolQueryBuilder

sets the minimum should match value for the given query object

Parameters:

  • value (Integer)

    minimum number of should conditions to fulfill

Returns:



131
132
133
134
135
136
# File 'lib/queries/bool_query_builder.rb', line 131

def minimum_should_match(value)
  raise 'Minimum Should match cannot be nil' if value.nil?

  @minimum_should_match = value
  self
end

#must(query_builder) ⇒ BoolQueryBuilder

must query to be added adds a must query

Parameters:

Returns:



92
93
94
95
96
97
98
99
# File 'lib/queries/bool_query_builder.rb', line 92

def must(query_builder)
  if query_builder.is_a?(Array)
    @must_queries += query_builder
  else
    @must_queries.append(query_builder)
  end
  self
end

#must_not(query_builder) ⇒ BoolQueryBuilder

adds a must_not query

Parameters:

  • query_builder (QueryBuilder)

    must_not query to be added

Returns:



111
112
113
114
115
116
117
118
# File 'lib/queries/bool_query_builder.rb', line 111

def must_not(query_builder)
  if query_builder.is_a?(Array)
    @mustnot_queries += query_builder
  else
    @mustnot_queries.append(query_builder)
  end
  self
end

#queryHash

Returns serialized query hash for this object

Returns:

  • (Hash)

    returns serialized query hash for this object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/queries/bool_query_builder.rb', line 31

def query
  query = {}
  bool_query = common_query
  bool_query[:should] = @should_queries.map(&:query)
  bool_query[:must] = @must_queries.map(&:query)
  bool_query[:must_not] = @mustnot_queries.map(&:query)
  bool_query[:filter] = @filter_queries.map(&:query)
  bool_query[:minimum_should_match] = @minimum_should_match
  bool_query[:boost] = @boost
  query[name.intern] = bool_query.compact
  query
end

#should(query_builder) ⇒ BoolQueryBuilder

adds a should query

Parameters:

Returns:



73
74
75
76
77
78
79
80
# File 'lib/queries/bool_query_builder.rb', line 73

def should(query_builder)
  if query_builder.is_a?(Array)
    @should_queries += query_builder
  else
    @should_queries.append(query_builder)
  end
  self
end