diff --git a/lib/elasticsearch/dsl/search/queries/inner_hits.rb b/lib/elasticsearch/dsl/search/queries/inner_hits.rb index c0ac470..4783dc7 100644 --- a/lib/elasticsearch/dsl/search/queries/inner_hits.rb +++ b/lib/elasticsearch/dsl/search/queries/inner_hits.rb @@ -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 @@ -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 diff --git a/spec/elasticsearch/dsl/search/collapse_spec.rb b/spec/elasticsearch/dsl/search/collapse_spec.rb index aee2943..d9146a5 100644 --- a/spec/elasticsearch/dsl/search/collapse_spec.rb +++ b/spec/elasticsearch/dsl/search/collapse_spec.rb @@ -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' @@ -73,6 +74,7 @@ { name: 'last_tweet', size: 10, from: 5, + _source: ['date'], sort: [ { date: { order: 'desc' } }, { likes: { order: 'asc' } } ] } @@ -90,4 +92,4 @@ expect(coll.to_hash[:inner_hits]).to eq(inner_hits_hash) end end -end \ No newline at end of file +end diff --git a/spec/elasticsearch/dsl/search/queries/inner_hits_spec.rb b/spec/elasticsearch/dsl/search/queries/inner_hits_spec.rb index 0f5fb8f..0c5e125 100644 --- a/spec/elasticsearch/dsl/search/queries/inner_hits_spec.rb +++ b/spec/elasticsearch/dsl/search/queries/inner_hits_spec.rb @@ -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 diff --git a/spec/elasticsearch/dsl/search_spec.rb b/spec/elasticsearch/dsl/search_spec.rb index 1b93836..c588dab 100644 --- a/spec/elasticsearch/dsl/search_spec.rb +++ b/spec/elasticsearch/dsl/search_spec.rb @@ -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' @@ -300,6 +301,7 @@ def bool_query(obj) { name: 'last_tweet', size: 10, from: 5, + _source: ['date'], sort: [ { date: { order: 'desc' } }, { likes: { order: 'asc' } }] } @@ -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