From a29c0db70242bf5367e7cfe21bed12e7465da1f4 Mon Sep 17 00:00:00 2001 From: Eliska Hutnikova Date: Wed, 11 Nov 2020 13:19:26 +0100 Subject: [PATCH 1/2] [DSL] Remove a duplicate test There is a completely identical test just above --- .../spec/elasticsearch/dsl/search_spec.rb | 54 ------------------- 1 file changed, 54 deletions(-) diff --git a/elasticsearch-dsl/spec/elasticsearch/dsl/search_spec.rb b/elasticsearch-dsl/spec/elasticsearch/dsl/search_spec.rb index ba273f50de..883701b803 100644 --- a/elasticsearch-dsl/spec/elasticsearch/dsl/search_spec.rb +++ b/elasticsearch-dsl/spec/elasticsearch/dsl/search_spec.rb @@ -315,58 +315,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 From 3b6d3f2163573967e7d2f38c83a7ad997662f7f6 Mon Sep 17 00:00:00 2001 From: Eliska Hutnikova Date: Wed, 11 Nov 2020 16:12:00 +0100 Subject: [PATCH 2/2] [DSL] Allow specify _source for inner_hits --- .../dsl/search/queries/inner_hits.rb | 25 +++++++++++++++ .../elasticsearch/dsl/search/collapse_spec.rb | 4 ++- .../dsl/search/queries/inner_hits_spec.rb | 32 +++++++++++++++++++ .../spec/elasticsearch/dsl/search_spec.rb | 2 ++ 4 files changed, 62 insertions(+), 1 deletion(-) 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 5673851d7c..66df543e78 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 7bc721438a..4ecffe3d9e 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 4a93b429ec..ca0f08cdc8 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 883701b803..7fef49e414 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' } }] }