Module: Elasticsearch::DSL::Search::BaseComponent

Included in:
Aggregations::Avg, Aggregations::Cardinality, Aggregations::ExtendedStats, Aggregations::GeoBounds, Aggregations::Max, Aggregations::Min, Aggregations::PercentileRanks, Aggregations::Percentiles, Aggregations::ScriptedMetric, Aggregations::Stats, Aggregations::Sum, Aggregations::TopHits, Aggregations::ValueCount, Collapse, Filters::And, Filters::Bool, Filters::Exists, Filters::GeoBoundingBox, Filters::GeoDistance, Filters::GeoDistanceRange, Filters::GeoPolygon, Filters::GeoShape, Filters::GeohashCell, Filters::HasChild, Filters::HasParent, Filters::Ids, Filters::Indices, Filters::Limit, Filters::MatchAll, Filters::Missing, Filters::Nested, Filters::Not, Filters::Or, Filters::Prefix, Filters::Query, Filters::Range, Filters::Regexp, Filters::Script, Filters::Term, Filters::Terms, Filters::Type, Highlight, Queries::Bool, Queries::Boosting, Queries::Common, Queries::ConstantScore, Queries::DisMax, Queries::Exists, Queries::Filtered, Queries::FunctionScore, Queries::Fuzzy, Queries::FuzzyLikeThis, Queries::FuzzyLikeThisField, Queries::GeoShape, Queries::HasChild, Queries::HasParent, Queries::Ids, Queries::Indices, Queries::InnerHits, Queries::Match, Queries::MatchAll, Queries::MatchPhrase, Queries::MatchPhrasePrefix, Queries::MoreLikeThis, Queries::MultiMatch, Queries::Nested, Queries::Prefix, Queries::QueryString, Queries::Range, Queries::Regexp, Queries::SimpleQueryString, Queries::SpanFirst, Queries::SpanMulti, Queries::SpanNear, Queries::SpanNot, Queries::SpanOr, Queries::SpanTerm, Queries::Template, Queries::Term, Queries::Terms, Queries::TopChildren, Queries::Wildcard, Sort, Suggest
Defined in:
lib/elasticsearch/dsl/search/base_component.rb

Overview

Module containing common functionality for DSL classes

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/elasticsearch/dsl/search/base_component.rb', line 25

def self.included(base)
  base.__send__ :extend,  ClassMethods
  base.__send__ :include, InstanceMethods

  base.instance_eval do
    #   Defines an "inner" method for DSL classes
    #
    #     @example Define a method `bar` for the MyQuery class which updates the query definition
    #
    #         class MyQuery
    #           include BaseComponent
    #
    #           option_method :bar
    #         end
    #
    #         q = MyQuery.new :foo do
    #           bar 'TEST'
    #         end
    #
    #         q.to_hash
    #         # => {:myquery=>{:foo=>{:bar=>"TEST"}}}
    #
    #     @example Define a method `bar` with custom logic for updating the Hash with query definition
    #
    #         class MyCustomQuery
    #           include BaseComponent
    #
    #           option_method :bar, lambda { |*args| @hash[self.name.to_sym][@args].update custom: args.pop }
    #         end
    #
    #         q = MyCustomQuery.new :foo do
    #           bar 'TEST'
    #         end
    #
    #         q.to_hash
    #       # => {:mycustomquery=>{:foo=>{:custom=>"TEST"}}}
    #
    def option_method(name, block=nil)
      option_methods << name
      if block
        self.__send__ :define_method, name, &block
      else
        self.__send__ :define_method, name do |*args|
          # 1. Component has empty @args (ie. no user supplied name or @hash value)
          if @args && @args.respond_to?(:to_hash) && @args.empty?
            @hash[self.name.to_sym].update name.to_sym => args.first
          # 2. Component user-supplied name or @hash value passed in @args
          else
            @hash[self.name.to_sym] = { @args => {} } unless @hash[self.name.to_sym][@args]
            @hash[self.name.to_sym][@args].update name.to_sym => args.first
          end
        end
      end
    end
  end
end

Instance Method Details

#initialize(*args, &block) ⇒ Object



82
83
84
85
86
87
# File 'lib/elasticsearch/dsl/search/base_component.rb', line 82

def initialize(*args, &block)
  @hash    = { name => {} }
  @args    = args.first || {}
  @options = args.size > 1 ? args.last : {}
  @block   = block
end