Skip to content

Commit 090f898

Browse files
eliskahpicandocodigo
authored andcommitted
[DSL] Allow specify _source for inner_hits
1 parent 31b3b04 commit 090f898

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

lib/elasticsearch/dsl/search/queries/inner_hits.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,30 @@ def sort(*args, &block)
104104
end
105105
end
106106

107+
# Specify the _source on the inner_hits definition. By default inner_hits contain complete source.
108+
#
109+
# @example
110+
# inner_hits 'last_tweet' do
111+
# size 10
112+
# from 5
113+
# source ['likes']
114+
# sort do
115+
# by :date, order: 'desc'
116+
# by :likes, order: 'asc'
117+
# end
118+
# end
119+
#
120+
# @param [ Array, Hash ]
121+
#
122+
# @return self.
123+
#
124+
# @since 0.1.9
125+
def _source(args)
126+
@source = args
127+
self
128+
end
129+
alias_method :source, :_source
130+
107131
# Convert the definition to a hash, to be used in a search request.
108132
#
109133
# @example
@@ -124,6 +148,7 @@ def to_hash
124148
call
125149
@hash = @value
126150
@hash[:sort] = @sort.to_hash if @sort
151+
@hash[:_source] = @source if defined?(@source)
127152
@hash
128153
end
129154
end

spec/elasticsearch/dsl/search/collapse_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
inner_hits 'last_tweet' do
6262
size 10
6363
from 5
64+
_source ['date']
6465
sort do
6566
by :date, order: 'desc'
6667
by :likes, order: 'asc'
@@ -73,6 +74,7 @@
7374
{ name: 'last_tweet',
7475
size: 10,
7576
from: 5,
77+
_source: ['date'],
7678
sort: [ { date: { order: 'desc' } },
7779
{ likes: { order: 'asc' } } ]
7880
}
@@ -90,4 +92,4 @@
9092
expect(coll.to_hash[:inner_hits]).to eq(inner_hits_hash)
9193
end
9294
end
93-
end
95+
end

spec/elasticsearch/dsl/search/queries/inner_hits_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,38 @@
8383
{ likes: { order: 'asc' } }])
8484
end
8585
end
86+
87+
describe '#_source' do
88+
context 'when excludes' do
89+
before do
90+
search._source({excludes: 'date'})
91+
end
92+
93+
it 'applies the option' do
94+
expect(search.to_hash[:_source][:excludes]).to eq('date')
95+
end
96+
end
97+
98+
context 'when includes' do
99+
before do
100+
search._source({includes: 'date'})
101+
end
102+
103+
it 'applies the option' do
104+
expect(search.to_hash[:_source][:includes]).to eq('date')
105+
end
106+
end
107+
108+
context 'when listing fields' do
109+
before do
110+
search._source(['last_tweet', 'date'])
111+
end
112+
113+
it 'applies the option' do
114+
expect(search.to_hash[:_source]).to eq(['last_tweet', 'date'])
115+
end
116+
end
117+
end
86118
end
87119

88120
describe '#initialize' do

spec/elasticsearch/dsl/search_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ def bool_query(obj)
287287
inner_hits 'last_tweet' do
288288
size 10
289289
from 5
290+
_source ['date']
290291
sort do
291292
by :date, order: 'desc'
292293
by :likes, order: 'asc'
@@ -300,6 +301,7 @@ def bool_query(obj)
300301
{ name: 'last_tweet',
301302
size: 10,
302303
from: 5,
304+
_source: ['date'],
303305
sort: [ { date: { order: 'desc' } },
304306
{ likes: { order: 'asc' } }]
305307
}

0 commit comments

Comments
 (0)