Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script_score to query DSL #254

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading