diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/330_fetch_fields.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/330_fetch_fields.yml index a039747e332..377c5e0e08b 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search/330_fetch_fields.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/330_fetch_fields.yml @@ -92,6 +92,7 @@ setup: fields: - field: keyword format: "yyyy/MM/dd" + --- "Test disable source": - do: @@ -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" } diff --git a/server/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java b/server/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java index ce66f1b4ce7..84ee3cb0174 100644 --- a/server/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java @@ -1045,6 +1045,7 @@ public final class SearchSourceBuilder implements Writeable, ToXContentObject, R rewrittenBuilder.explain = explain; rewrittenBuilder.extBuilders = extBuilders; rewrittenBuilder.fetchSourceContext = fetchSourceContext; + rewrittenBuilder.fetchFields = fetchFields; rewrittenBuilder.docValueFields = docValueFields; rewrittenBuilder.storedFieldsContext = storedFieldsContext; rewrittenBuilder.from = from; @@ -1603,10 +1604,10 @@ public final class SearchSourceBuilder implements Writeable, ToXContentObject, R @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 @@ -1621,6 +1622,7 @@ public final class SearchSourceBuilder implements Writeable, ToXContentObject, R 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) diff --git a/server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java b/server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java index 12c3e487ff1..8d35d4f1252 100644 --- a/server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java @@ -105,6 +105,14 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase { } } + 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); diff --git a/test/framework/src/main/java/org/elasticsearch/search/RandomSearchRequestGenerator.java b/test/framework/src/main/java/org/elasticsearch/search/RandomSearchRequestGenerator.java index 70b0059b556..11264670595 100644 --- a/test/framework/src/main/java/org/elasticsearch/search/RandomSearchRequestGenerator.java +++ b/test/framework/src/main/java/org/elasticsearch/search/RandomSearchRequestGenerator.java @@ -182,6 +182,13 @@ public class RandomSearchRequestGenerator { 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++) {