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.



53
54
55
56
# File 'lib/elasticsearch/dsl/search/queries/function_score.rb', line 53

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)


71
72
73
74
# File 'lib/elasticsearch/dsl/search/queries/function_score.rb', line 71

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)


80
81
82
83
84
85
86
# File 'lib/elasticsearch/dsl/search/queries/function_score.rb', line 80

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)


92
93
94
# File 'lib/elasticsearch/dsl/search/queries/function_score.rb', line 92

def functions=(value)
  @functions = value
end

#query(*args, &block) ⇒ self

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

Returns:

  • (self)


62
63
64
65
# File 'lib/elasticsearch/dsl/search/queries/function_score.rb', line 62

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)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/elasticsearch/dsl/search/queries/function_score.rb', line 100

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