Skip to content

Add source to inner_hits #18

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions lib/elasticsearch/dsl/search/queries/inner_hits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,30 @@ def sort(*args, &block)
end
end

# Specify the _source on the inner_hits definition. By default inner_hits contain complete source.
#
# @example
# inner_hits 'last_tweet' do
# size 10
# from 5
# source ['likes']
# sort do
# by :date, order: 'desc'
# by :likes, order: 'asc'
# end
# end
#
# @param [ Array, Hash ]
#
# @return self.
#
# @since 0.1.9
def _source(args)
@source = args
self
end
alias_method :source, :_source

# Convert the definition to a hash, to be used in a search request.
#
# @example
Expand All @@ -124,6 +148,7 @@ def to_hash
call
@hash = @value
@hash[:sort] = @sort.to_hash if @sort
@hash[:_source] = @source if defined?(@source)
@hash
end
end
Expand Down
4 changes: 3 additions & 1 deletion spec/elasticsearch/dsl/search/collapse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
inner_hits 'last_tweet' do
size 10
from 5
_source ['date']
sort do
by :date, order: 'desc'
by :likes, order: 'asc'
Expand All @@ -73,6 +74,7 @@
{ name: 'last_tweet',
size: 10,
from: 5,
_source: ['date'],
sort: [ { date: { order: 'desc' } },
{ likes: { order: 'asc' } } ]
}
Expand All @@ -90,4 +92,4 @@
expect(coll.to_hash[:inner_hits]).to eq(inner_hits_hash)
end
end
end
end
32 changes: 32 additions & 0 deletions spec/elasticsearch/dsl/search/queries/inner_hits_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,38 @@
{ likes: { order: 'asc' } }])
end
end

describe '#_source' do
context 'when excludes' do
before do
search._source({excludes: 'date'})
end

it 'applies the option' do
expect(search.to_hash[:_source][:excludes]).to eq('date')
end
end

context 'when includes' do
before do
search._source({includes: 'date'})
end

it 'applies the option' do
expect(search.to_hash[:_source][:includes]).to eq('date')
end
end

context 'when listing fields' do
before do
search._source(['last_tweet', 'date'])
end

it 'applies the option' do
expect(search.to_hash[:_source]).to eq(['last_tweet', 'date'])
end
end
end
end

describe '#initialize' do
Expand Down
56 changes: 2 additions & 54 deletions spec/elasticsearch/dsl/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ def bool_query(obj)
inner_hits 'last_tweet' do
size 10
from 5
_source ['date']
sort do
by :date, order: 'desc'
by :likes, order: 'asc'
Expand All @@ -300,6 +301,7 @@ def bool_query(obj)
{ name: 'last_tweet',
size: 10,
from: 5,
_source: ['date'],
sort: [ { date: { order: 'desc' } },
{ likes: { order: 'asc' } }]
}
Expand Down Expand Up @@ -328,58 +330,4 @@ def bool_query(obj)
expect(s.to_hash).to eq(expected_hash)
end
end

describe '#collapse' do

let(:s) do
search do
query do
match title: 'test'
end
collapse :user do
max_concurrent_group_searches 4
inner_hits 'last_tweet' do
size 10
from 5
sort do
by :date, order: 'desc'
by :likes, order: 'asc'
end
end
end
end
end

let(:inner_hits_hash) do
{ name: 'last_tweet',
size: 10,
from: 5,
sort: [ { date: { order: 'desc' } },
{ likes: { order: 'asc' } }]
}
end

let(:expected_hash) do
{ query: { match: { title: 'test' } },
collapse: { field: :user,
max_concurrent_group_searches: 4,
inner_hits: inner_hits_hash } }
end

it 'sets the field name' do
expect(s.to_hash[:collapse][:field]).to eq(:user)
end

it 'sets the max_concurrent_group_searches option' do
expect(s.to_hash[:collapse][:max_concurrent_group_searches]).to eq(4)
end

it 'sets the inner_hits' do
expect(s.to_hash[:collapse][:inner_hits]).to eq(inner_hits_hash)
end

it 'constructs the correct hash' do
expect(s.to_hash).to eq(expected_hash)
end
end
end