Class: OpenSearch::DSL::Search::Queries::FunctionScore

Inherits:
Object
  • Object
show all
Includes:
BaseComponent
Defined in:
lib/opensearch/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.



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

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)


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

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)


88
89
90
91
92
93
94
# File 'lib/opensearch/dsl/search/queries/function_score.rb', line 88

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)


100
101
102
# File 'lib/opensearch/dsl/search/queries/function_score.rb', line 100

def functions=(value)
  @functions = value
end

#query(*args, &block) ⇒ self

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

Returns:

  • (self)


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

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)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/opensearch/dsl/search/queries/function_score.rb', line 108

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