diff --git a/server/src/main/java/org/elasticsearch/action/search/SearchShardIterator.java b/server/src/main/java/org/elasticsearch/action/search/SearchShardIterator.java index c0441b1b7a188..36a9b75199402 100644 --- a/server/src/main/java/org/elasticsearch/action/search/SearchShardIterator.java +++ b/server/src/main/java/org/elasticsearch/action/search/SearchShardIterator.java @@ -75,7 +75,7 @@ public SearchShardIterator(@Nullable String clusterAlias, ShardId shardId, this.clusterAlias = clusterAlias; this.searchContextId = searchContextId; this.searchContextKeepAlive = searchContextKeepAlive; - assert (searchContextId == null) == (searchContextKeepAlive == null); + assert searchContextKeepAlive == null || searchContextId != null; } /** 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 476521469bc0f..9da90c8873e8d 100644 --- a/server/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java @@ -1676,33 +1676,35 @@ private static final class XContentParams { public PointInTimeBuilder(String id, TimeValue keepAlive) { this.id = Objects.requireNonNull(id); - this.keepAlive = Objects.requireNonNull(keepAlive); + this.keepAlive = keepAlive; } public PointInTimeBuilder(StreamInput in) throws IOException { id = in.readString(); - keepAlive = in.readTimeValue(); + keepAlive = in.readOptionalTimeValue(); } @Override public void writeTo(StreamOutput out) throws IOException { out.writeString(id); - out.writeTimeValue(keepAlive); + out.writeOptionalTimeValue(keepAlive); } @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(POINT_IN_TIME.getPreferredName()); builder.field(ID_FIELD.getPreferredName(), id); - builder.field(KEEP_ALIVE_FIELD.getPreferredName(), keepAlive); + if (keepAlive != null) { + builder.field(KEEP_ALIVE_FIELD.getPreferredName(), keepAlive); + } builder.endObject(); return builder; } public static PointInTimeBuilder fromXContent(XContentParser parser) throws IOException { final XContentParams params = PARSER.parse(parser, null); - if (params.id == null || params.keepAlive == null) { - throw new IllegalArgumentException("id and keep_alive must be specified"); + if (params.id == null) { + throw new IllegalArgumentException("point int time id is not provided"); } return new PointInTimeBuilder(params.id, params.keepAlive); } diff --git a/server/src/main/java/org/elasticsearch/search/internal/ShardSearchRequest.java b/server/src/main/java/org/elasticsearch/search/internal/ShardSearchRequest.java index cc4660d6034bc..15e5effa38fa4 100644 --- a/server/src/main/java/org/elasticsearch/search/internal/ShardSearchRequest.java +++ b/server/src/main/java/org/elasticsearch/search/internal/ShardSearchRequest.java @@ -175,7 +175,7 @@ private ShardSearchRequest(OriginalIndices originalIndices, this.originalIndices = originalIndices; this.readerId = readerId; this.keepAlive = keepAlive; - assert (readerId != null) == (keepAlive != null); + assert keepAlive == null || readerId != null : "readerId: " + readerId + " keepAlive: " + keepAlive; } public ShardSearchRequest(StreamInput in) throws IOException { 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 1601cc9e0cfae..deb645de1fb89 100644 --- a/server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java @@ -386,7 +386,8 @@ public void testToXContent() throws IOException { public void testToXContentWithPointInTime() throws IOException { XContentType xContentType = randomFrom(XContentType.values()); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); - searchSourceBuilder.pointInTimeBuilder(new SearchSourceBuilder.PointInTimeBuilder("id", TimeValue.timeValueHours(1))); + TimeValue keepAlive = randomBoolean() ? TimeValue.timeValueHours(1) : null; + searchSourceBuilder.pointInTimeBuilder(new SearchSourceBuilder.PointInTimeBuilder("id", keepAlive)); XContentBuilder builder = XContentFactory.contentBuilder(xContentType); searchSourceBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); BytesReference bytes = BytesReference.bytes(builder); @@ -394,9 +395,14 @@ public void testToXContentWithPointInTime() throws IOException { assertEquals(1, sourceAsMap.size()); @SuppressWarnings("unchecked") Map pit = (Map) sourceAsMap.get("pit"); - assertEquals(2, pit.size()); assertEquals("id", pit.get("id")); - assertEquals("1h", pit.get("keep_alive")); + if (keepAlive != null) { + assertEquals("1h", pit.get("keep_alive")); + assertEquals(2, pit.size()); + } else { + assertNull(pit.get("keep_alive")); + assertEquals(1, pit.size()); + } } public void testParseIndicesBoost() throws IOException { 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 d6d3ec51c0ed0..5d5b2bbaca32a 100644 --- a/test/framework/src/main/java/org/elasticsearch/search/RandomSearchRequestGenerator.java +++ b/test/framework/src/main/java/org/elasticsearch/search/RandomSearchRequestGenerator.java @@ -371,8 +371,8 @@ public static SearchSourceBuilder randomSearchSourceBuilder( builder.collapse(randomCollapseBuilder.get()); } if (randomBoolean()) { - builder.pointInTimeBuilder(new SearchSourceBuilder.PointInTimeBuilder(randomAlphaOfLengthBetween(3, 10), - TimeValue.timeValueMinutes(randomIntBetween(1, 60)))); + TimeValue keepAlive = randomBoolean() ? TimeValue.timeValueMinutes(randomIntBetween(1, 60)) : null; + builder.pointInTimeBuilder(new SearchSourceBuilder.PointInTimeBuilder(randomAlphaOfLengthBetween(3, 10), keepAlive)); } return builder; } diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/search/point_in_time.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/search/point_in_time.yml index 52e5708179234..f6ff4d66f9a41 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/search/point_in_time.yml +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/search/point_in_time.yml @@ -86,7 +86,6 @@ setup: search_after: [24, 172] pit: id: "$point_in_time_id" - keep_alive: 1m - match: {hits.total: 3 } - length: {hits.hits: 1 } @@ -106,7 +105,6 @@ setup: search_after: [18, 42] pit: id: "$point_in_time_id" - keep_alive: 1m - match: {hits.total: 3} - length: {hits.hits: 1 }