Skip to content

Commit

Permalink
Add script_score to query DSL (#254)
Browse files Browse the repository at this point in the history
* Add script_score to query DSL

Signed-off-by: Kim Quang Cap <[email protected]>

* Fix example for script_score

Signed-off-by: Kim Quang Cap <[email protected]>

* Fixed example description and added changelog

Signed-off-by: Kim Quang Cap <[email protected]>

* Remove ES header

Signed-off-by: Kim Quang Cap <[email protected]>

* Add license header for OS.

Signed-off-by: Kim Quang Cap <[email protected]>

---------

Signed-off-by: Kim Quang Cap <[email protected]>
Co-authored-by: Kim Quang Cap <[email protected]>
  • Loading branch information
quangcap and Kim Quang Cap authored Jul 9, 2024
1 parent fd116e8 commit 280774c
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]
### Added
- Added `script_score` to Query DSL ([#254](https://github.com/opensearch-project/opensearch-ruby/pull/254))
### Changed
### Deprecated
### Removed
Expand Down
62 changes: 62 additions & 0 deletions lib/opensearch/dsl/search/queries/script_score.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.

module OpenSearch
module DSL
module Search
module Queries
# A query which wraps another query and returns a customized score for matching documents
#
# @example
#
# search do
# query do
# script_score do
# query do
# match content: 'Twitter'
# end
#
# script source: "_score * params['multiplier']",
# params: { multiplier: 2.0 }
# end
# end
# end
#
# @see https://opensearch.org/docs/latest/query-dsl/specialized/script-score/
#
class ScriptScore
include BaseComponent

option_method :script
option_method :min_score
option_method :boost

# DSL method for building the `query` part of the query definition
#
# @return [self]
#
def query(*args, &block)
@query = block ? @query = Query.new(*args, &block) : args.first
self
end

# Converts the query definition to a Hash
#
# @return [Hash]
#
def to_hash
hash = super
if @query
_query = @query.respond_to?(:to_hash) ? @query.to_hash : @query
hash[name].update(query: _query)
end
hash
end
end
end
end
end
end
90 changes: 90 additions & 0 deletions spec/opensearch/dsl/search/queries/script_score_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.

require_relative '../../../../spec_helper'

describe OpenSearch::DSL::Search::Queries::ScriptScore do
describe '#to_hash' do
let(:search) do
described_class.new
end

it 'can be converted to a hash' do
expect(search.to_hash).to eq(script_score: {})
end
end

context 'when options methods are called' do
let(:search) do
described_class.new
end

describe '#query' do
before do
search.query('bar')
end

it 'applies the option' do
expect(search.to_hash[:script_score][:query]).to eq('bar')
end
end

describe '#boost' do
before do
search.boost('bar')
end

it 'applies the option' do
expect(search.to_hash[:script_score][:boost]).to eq('bar')
end
end

describe '#script' do
before do
search.script('bar')
end

it 'applies the option' do
expect(search.to_hash[:script_score][:script]).to eq('bar')
end
end

describe '#min_score' do
before do
search.min_score('bar')
end

it 'applies the option' do
expect(search.to_hash[:script_score][:min_score]).to eq('bar')
end
end
end

describe '#initialize' do
context 'when a block is provided' do
let(:search) do
described_class.new do
query do
term foo: 'bar'
end

script source: 'foo',
params: { foo: 'bar' }
boost 'bar'
min_score 'bar'
end
end

it 'executes the block' do
expect(search.to_hash[:script_score][:query][:term][:foo]).to eq('bar')
expect(search.to_hash[:script_score][:script][:source]).to eq('foo')
expect(search.to_hash[:script_score][:script][:params]).to eq(foo: 'bar')
expect(search.to_hash[:script_score][:boost]).to eq('bar')
expect(search.to_hash[:script_score][:min_score]).to eq('bar')
end
end
end
end

0 comments on commit 280774c

Please sign in to comment.