diff --git a/elasticsearch-dsl/lib/elasticsearch/dsl/search/queries/inner_hits.rb b/elasticsearch-dsl/lib/elasticsearch/dsl/search/queries/inner_hits.rb index 5673851d7..66df543e7 100644 --- a/elasticsearch-dsl/lib/elasticsearch/dsl/search/queries/inner_hits.rb +++ b/elasticsearch-dsl/lib/elasticsearch/dsl/search/queries/inner_hits.rb @@ -91,6 +91,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 @@ -111,6 +135,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/elasticsearch-dsl/spec/elasticsearch/dsl/search/collapse_spec.rb b/elasticsearch-dsl/spec/elasticsearch/dsl/search/collapse_spec.rb index 7bc721438..4ecffe3d9 100644 --- a/elasticsearch-dsl/spec/elasticsearch/dsl/search/collapse_spec.rb +++ b/elasticsearch-dsl/spec/elasticsearch/dsl/search/collapse_spec.rb @@ -48,6 +48,7 @@ inner_hits 'last_tweet' do size 10 from 5 + _source ['date'] sort do by :date, order: 'desc' by :likes, order: 'asc' @@ -60,6 +61,7 @@ { name: 'last_tweet', size: 10, from: 5, + _source: ['date'], sort: [ { date: { order: 'desc' } }, { likes: { order: 'asc' } } ] } @@ -77,4 +79,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/elasticsearch-dsl/spec/elasticsearch/dsl/search/queries/inner_hits_spec.rb b/elasticsearch-dsl/spec/elasticsearch/dsl/search/queries/inner_hits_spec.rb index 4a93b429e..ca0f08cdc 100644 --- a/elasticsearch-dsl/spec/elasticsearch/dsl/search/queries/inner_hits_spec.rb +++ b/elasticsearch-dsl/spec/elasticsearch/dsl/search/queries/inner_hits_spec.rb @@ -70,6 +70,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/elasticsearch-dsl/spec/elasticsearch/dsl/search_spec.rb b/elasticsearch-dsl/spec/elasticsearch/dsl/search_spec.rb index ba273f50d..7fef49e41 100644 --- a/elasticsearch-dsl/spec/elasticsearch/dsl/search_spec.rb +++ b/elasticsearch-dsl/spec/elasticsearch/dsl/search_spec.rb @@ -274,6 +274,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' @@ -287,6 +288,7 @@ def bool_query(obj) { name: 'last_tweet', size: 10, from: 5, + _source: ['date'], sort: [ { date: { order: 'desc' } }, { likes: { order: 'asc' } }] } @@ -315,58 +317,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