Skip to content

Commit 85ad328

Browse files
authored
Ensure fetch fields aren't dropped when rewriting search. (#61390)
Previously we didn't retain the requested fields when performing a shallow copy of the search source. This meant that when a search was rewritten, we could drop the requested fields and fail to return them in the response.
1 parent 00b56bf commit 85ad328

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/search/330_fetch_fields.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ setup:
9292
fields:
9393
- field: keyword
9494
format: "yyyy/MM/dd"
95+
9596
---
9697
"Test disable source":
9798
- do:
@@ -214,3 +215,43 @@ setup:
214215

215216
- match: { hits.hits.0.fields.keyword.0: "a" }
216217
- match: { hits.hits.0.fields.integer.0: 42 }
218+
219+
---
220+
"Test search rewrite":
221+
- do:
222+
indices.create:
223+
index: test
224+
body:
225+
settings:
226+
index.number_of_shards: 1
227+
mappings:
228+
properties:
229+
date:
230+
type: date
231+
232+
- do:
233+
index:
234+
index: test
235+
id: 1
236+
body:
237+
date: "1990-12-29T22:30:00.000Z"
238+
239+
- do:
240+
indices.refresh:
241+
index: [ test ]
242+
243+
- do:
244+
search:
245+
index: test
246+
body:
247+
query:
248+
range:
249+
date:
250+
from: "1990-12-29T22:30:00.000Z"
251+
fields:
252+
- field: date
253+
format: "yyyy/MM/dd"
254+
255+
- is_true: hits.hits.0._id
256+
- is_true: hits.hits.0._source
257+
- match: { hits.hits.0.fields.date.0: "1990/12/29" }

server/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@ private SearchSourceBuilder shallowCopy(QueryBuilder queryBuilder, QueryBuilder
10451045
rewrittenBuilder.explain = explain;
10461046
rewrittenBuilder.extBuilders = extBuilders;
10471047
rewrittenBuilder.fetchSourceContext = fetchSourceContext;
1048+
rewrittenBuilder.fetchFields = fetchFields;
10481049
rewrittenBuilder.docValueFields = docValueFields;
10491050
rewrittenBuilder.storedFieldsContext = storedFieldsContext;
10501051
rewrittenBuilder.from = from;
@@ -1603,10 +1604,10 @@ public boolean equals(Object obj) {
16031604

16041605
@Override
16051606
public int hashCode() {
1606-
return Objects.hash(aggregations, explain, fetchSourceContext, docValueFields, storedFieldsContext, from, highlightBuilder,
1607-
indexBoosts, minScore, postQueryBuilder, queryBuilder, rescoreBuilders, scriptFields, size,
1608-
sorts, searchAfterBuilder, sliceBuilder, stats, suggestBuilder, terminateAfter, timeout, trackScores, version,
1609-
seqNoAndPrimaryTerm, profile, extBuilders, collapse, trackTotalHitsUpTo);
1607+
return Objects.hash(aggregations, explain, fetchSourceContext, fetchFields, docValueFields, storedFieldsContext, from,
1608+
highlightBuilder, indexBoosts, minScore, postQueryBuilder, queryBuilder, rescoreBuilders, scriptFields, size,
1609+
sorts, searchAfterBuilder, sliceBuilder, stats, suggestBuilder, terminateAfter, timeout, trackScores, version,
1610+
seqNoAndPrimaryTerm, profile, extBuilders, collapse, trackTotalHitsUpTo);
16101611
}
16111612

16121613
@Override
@@ -1621,6 +1622,7 @@ public boolean equals(Object obj) {
16211622
return Objects.equals(aggregations, other.aggregations)
16221623
&& Objects.equals(explain, other.explain)
16231624
&& Objects.equals(fetchSourceContext, other.fetchSourceContext)
1625+
&& Objects.equals(fetchFields, other.fetchFields)
16241626
&& Objects.equals(docValueFields, other.docValueFields)
16251627
&& Objects.equals(storedFieldsContext, other.storedFieldsContext)
16261628
&& Objects.equals(from, other.from)

server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ public void testSerialization() throws IOException {
105105
}
106106
}
107107

108+
public void testShallowCopy() {
109+
for (int i = 0; i < 10; i++) {
110+
SearchSourceBuilder original = createSearchSourceBuilder();
111+
SearchSourceBuilder copy = original.shallowCopy();
112+
assertEquals(original, copy);
113+
}
114+
}
115+
108116
public void testEqualsAndHashcode() throws IOException {
109117
// TODO add test checking that changing any member of this class produces an object that is not equal to the original
110118
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createSearchSourceBuilder(), this::copyBuilder);

test/framework/src/main/java/org/elasticsearch/search/RandomSearchRequestGenerator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ public static SearchSourceBuilder randomSearchSourceBuilder(
182182
throw new IllegalStateException();
183183
}
184184

185+
if (randomBoolean()) {
186+
int numFields = randomInt(5);
187+
for (int i = 0; i < numFields; i++) {
188+
builder.fetchField(randomAlphaOfLengthBetween(5, 10));
189+
}
190+
}
191+
185192
if (randomBoolean()) {
186193
int scriptFieldsSize = randomInt(25);
187194
for (int i = 0; i < scriptFieldsSize; i++) {

0 commit comments

Comments
 (0)