Class: Queries::TermsQueryBuilder

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

Constant Summary collapse

NAME =
"terms"

Instance Method Summary collapse

Methods inherited from QueryBuilder

#boost

Methods included from AttributesReader

#attributes

Methods included from AbstractQueryBuilder

#do_equals?, #name

Constructor Details

#initialize(field_name:, values: []) ⇒ TermsQueryBuilder

@params:

field_name: name of the field in the document which is being queried
values: values which are matched to the data in that field can be:
          Data Set: 
            Directly matched to the data in the given field.
          Terms Lookup:
            Terms lookup fetches the field values of an existing document. 
            Elasticsearch then uses those values as search terms. This can be helpful when searching for a large set of terms.
            Because terms lookup fetches values from a document, the _source mapping field must be enabled to use terms lookup. 
            The _source field is enabled by default.


23
24
25
26
27
28
29
30
# File 'lib/queries/terms_query_builder.rb', line 23

def initialize field_name:, values: []
  @field_name = field_name
  if values.is_a?(Misc::TermsLookup)
    @terms_lookup = values
  else
    @values = values.is_a?(Array) ? values : values.split(',')
  end
end

Instance Method Details

#field_name_exprObject

FIELD NAME ########## returns field_name



43
44
45
# File 'lib/queries/terms_query_builder.rb', line 43

def field_name_expr
  return @field_name
end

#queryObject



32
33
34
35
36
37
38
39
# File 'lib/queries/terms_query_builder.rb', line 32

def query
  query = {}
  terms_query = self.common_query
  terms_query[@field_name.intern] = @values
  terms_query[@field_name.intern] = @terms_lookup.settings if @terms_lookup.present?
  query[name.intern] = terms_query
  return query
end

#values_exprObject

FIELD VALUE ########## returns value



49
50
51
# File 'lib/queries/terms_query_builder.rb', line 49

def values_expr
  return @terms_lookup || @values
end