-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script_score to query DSL (#254)
* Add script_score to query DSL Signed-off-by: Kim Quang Cap <kimquangcap@gmail.com> * Fix example for script_score Signed-off-by: Kim Quang Cap <kimquangcap@gmail.com> * Fixed example description and added changelog Signed-off-by: Kim Quang Cap <kimquangcap@gmail.com> * Remove ES header Signed-off-by: Kim Quang Cap <kimquangcap@gmail.com> * Add license header for OS. Signed-off-by: Kim Quang Cap <kimquangcap@gmail.com> --------- Signed-off-by: Kim Quang Cap <kimquangcap@gmail.com> Co-authored-by: Kim Quang Cap <kimquangcap@gmail.com>
- Loading branch information
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |