Class: Elasticsearch::DSL::Search::Queries::FunctionScore

Inherits:
Object
  • Object
show all
Includes:
BaseComponent
Defined in:
lib/elasticsearch/dsl/search/queries/function_score.rb

Overview

A query which allows to modify the score of documents matching the query, either via built-in functions or a custom script

search do
  query do
    function_score do
      filter do
        terms amenities: ['wifi', 'pets']
      end
      functions << { gauss: { price:    { origin: 100, scale: 200 } } }
      functions << { gauss: { location: { origin: '50.090223,14.399590', scale: '5km' } } }
    end
  end
end

Examples:

Find documents with specific amenities, boosting documents within a certain

price range and geogprahical location

See Also:

Instance Method Summary collapse

Methods included from BaseComponent

included

Constructor Details

#initialize(*args, &block) ⇒ FunctionScore

Returns a new instance of FunctionScore.



40
41
42
43
# File 'lib/elasticsearch/dsl/search/queries/function_score.rb', line 40

def initialize(*args, &block)
  super
  @functions = []
end

Instance Method Details

#filter(*args, &block) ⇒ self

DSL method for building the ‘filter` part of the query definition

Returns:

  • (self)


58
59
60
61
# File 'lib/elasticsearch/dsl/search/queries/function_score.rb', line 58

def filter(*args, &block)
  @filter = block ? Filter.new(*args, &block) : args.first
  self
end

#functions(value = nil) ⇒ Array

DSL method for building the ‘functions` part of the query definition

Returns:

  • (Array)


67
68
69
70
71
72
73
# File 'lib/elasticsearch/dsl/search/queries/function_score.rb', line 67

def functions(value=nil)
  if value
    @functions = value
  else
    @functions
  end
end

#functions=(value) ⇒ Array

Set the ‘functions` part of the query definition

Returns:

  • (Array)


79
80
81
# File 'lib/elasticsearch/dsl/search/queries/function_score.rb', line 79

def functions=(value)
  @functions = value
end

#query(*args, &block) ⇒ self

DSL method for building the ‘query` part of the query definition

Returns:

  • (self)


49
50
51
52
# File 'lib/elasticsearch/dsl/search/queries/function_score.rb', line 49

def query(*args, &block)
  @query = block ? @query = Query.new(*args, &block) : args.first
  self
end

#to_hashHash

Converts the query definition to a Hash

Returns:

  • (Hash)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/elasticsearch/dsl/search/queries/function_score.rb', line 87

def to_hash
  hash = super
  if @query
    _query = @query.respond_to?(:to_hash) ? @query.to_hash : @query
    hash[self.name].update(query: _query)
  end
  if @filter
    _filter = @filter.respond_to?(:to_hash) ? @filter.to_hash : @filter
    hash[self.name].update(filter: _filter)
  end
  unless @functions.empty?
    hash[self.name].update(functions: @functions)
  end
  hash
end