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 3 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
82 changes: 82 additions & 0 deletions lib/opensearch/dsl/search/queries/script_score.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# 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.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
#
# Licensed to Elasticsearch B.V. under one or more contributor
quangcap marked this conversation as resolved.
Show resolved Hide resolved
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the 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
110 changes: 110 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,110 @@
# 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.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
#
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the 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