Skip to content

Ensure fetch fields aren't dropped when rewriting search. #61390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ setup:
fields:
- field: keyword
format: "yyyy/MM/dd"

---
"Test disable source":
- do:
Expand Down Expand Up @@ -214,3 +215,43 @@ setup:

- match: { hits.hits.0.fields.keyword.0: "a" }
- match: { hits.hits.0.fields.integer.0: 42 }

---
"Test search rewrite":
- do:
indices.create:
index: test
body:
settings:
index.number_of_shards: 1
mappings:
properties:
date:
type: date

- do:
index:
index: test
id: 1
body:
date: "1990-12-29T22:30:00.000Z"

- do:
indices.refresh:
index: [ test ]

- do:
search:
index: test
body:
query:
range:
date:
from: "1990-12-29T22:30:00.000Z"
fields:
- field: date
format: "yyyy/MM/dd"

- is_true: hits.hits.0._id
- is_true: hits.hits.0._source
- match: { hits.hits.0.fields.date.0: "1990/12/29" }
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,7 @@ private SearchSourceBuilder shallowCopy(QueryBuilder queryBuilder, QueryBuilder
rewrittenBuilder.explain = explain;
rewrittenBuilder.extBuilders = extBuilders;
rewrittenBuilder.fetchSourceContext = fetchSourceContext;
rewrittenBuilder.fetchFields = fetchFields;
rewrittenBuilder.docValueFields = docValueFields;
rewrittenBuilder.storedFieldsContext = storedFieldsContext;
rewrittenBuilder.from = from;
Expand Down Expand Up @@ -1603,10 +1604,10 @@ public boolean equals(Object obj) {

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

@Override
Expand All @@ -1621,6 +1622,7 @@ public boolean equals(Object obj) {
return Objects.equals(aggregations, other.aggregations)
&& Objects.equals(explain, other.explain)
&& Objects.equals(fetchSourceContext, other.fetchSourceContext)
&& Objects.equals(fetchFields, other.fetchFields)
&& Objects.equals(docValueFields, other.docValueFields)
&& Objects.equals(storedFieldsContext, other.storedFieldsContext)
&& Objects.equals(from, other.from)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ public void testSerialization() throws IOException {
}
}

public void testShallowCopy() {
for (int i = 0; i < 10; i++) {
SearchSourceBuilder original = createSearchSourceBuilder();
SearchSourceBuilder copy = original.shallowCopy();
assertEquals(original, copy);
}
}

public void testEqualsAndHashcode() throws IOException {
// TODO add test checking that changing any member of this class produces an object that is not equal to the original
EqualsHashCodeTestUtils.checkEqualsAndHashCode(createSearchSourceBuilder(), this::copyBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ public static SearchSourceBuilder randomSearchSourceBuilder(
throw new IllegalStateException();
}

if (randomBoolean()) {
int numFields = randomInt(5);
for (int i = 0; i < numFields; i++) {
builder.fetchField(randomAlphaOfLengthBetween(5, 10));
}
}

if (randomBoolean()) {
int scriptFieldsSize = randomInt(25);
for (int i = 0; i < scriptFieldsSize; i++) {
Expand Down