Skip to content

Commit 44bd4a6

Browse files
javannadnhatn
authored andcommitted
Fix point in time toXContent impl (#62080)
PointInTimeBuilder is a ToXContentObject yet it does not print out a whole object (it is rather a fragment). Also, when it is printed out as part of SearchSourceBuilder, an error is thrown because pit should be wrapped into its own object. This commit fixes this and adds tests for it.
1 parent 3d69b5c commit 44bd4a6

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t
14221422
builder.field(COLLAPSE.getPreferredName(), collapse);
14231423
}
14241424
if (pointInTimeBuilder != null) {
1425-
builder.field(POINT_IN_TIME.getPreferredName(), pointInTimeBuilder);
1425+
pointInTimeBuilder.toXContent(builder, params);
14261426
}
14271427
return builder;
14281428
}
@@ -1735,8 +1735,10 @@ public void writeTo(StreamOutput out) throws IOException {
17351735

17361736
@Override
17371737
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
1738+
builder.startObject(POINT_IN_TIME.getPreferredName());
17381739
builder.field(ID_FIELD.getPreferredName(), id);
17391740
builder.field(KEEP_ALIVE_FIELD.getPreferredName(), keepAlive);
1741+
builder.endObject();
17401742
return builder;
17411743
}
17421744

server/src/test/java/org/elasticsearch/action/search/TransportSearchActionTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,9 @@ public void testShouldMinimizeRoundtrips() throws Exception {
828828
searchRequestTests.setUp();
829829
SearchRequest searchRequest = searchRequestTests.createSearchRequest();
830830
searchRequest.scroll((Scroll)null);
831+
if (searchRequest.source() != null) {
832+
searchRequest.source().pointInTimeBuilder(null);
833+
}
831834
searchRequest.searchType(SearchType.QUERY_THEN_FETCH);
832835
SearchSourceBuilder source = searchRequest.source();
833836
if (source != null) {

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ public void testTimeoutWithoutUnits() throws IOException {
360360
}
361361
}
362362

363-
public void testToXContent() throws IOException {
363+
public void testToXContent() throws IOException {
364364
//verify that only what is set gets printed out through toXContent
365365
XContentType xContentType = randomFrom(XContentType.values());
366366
{
@@ -383,6 +383,22 @@ public void testToXContent() throws IOException {
383383
}
384384
}
385385

386+
public void testToXContentWithPointInTime() throws IOException {
387+
XContentType xContentType = randomFrom(XContentType.values());
388+
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
389+
searchSourceBuilder.pointInTimeBuilder(new SearchSourceBuilder.PointInTimeBuilder("id", TimeValue.timeValueHours(1)));
390+
XContentBuilder builder = XContentFactory.contentBuilder(xContentType);
391+
searchSourceBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
392+
BytesReference bytes = BytesReference.bytes(builder);
393+
Map<String, Object> sourceAsMap = XContentHelper.convertToMap(bytes, false, xContentType).v2();
394+
assertEquals(1, sourceAsMap.size());
395+
@SuppressWarnings("unchecked")
396+
Map<String, Object> pit = (Map<String, Object>) sourceAsMap.get("pit");
397+
assertEquals(2, pit.size());
398+
assertEquals("id", pit.get("id"));
399+
assertEquals("1h", pit.get("keep_alive"));
400+
}
401+
386402
public void testParseIndicesBoost() throws IOException {
387403
{
388404
String restContent = " { \"indices_boost\": {\"foo\": 1.0, \"bar\": 2.0}}";

server/src/test/java/org/elasticsearch/search/internal/ShardSearchRequestTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
import static org.hamcrest.Matchers.nullValue;
5353

5454
public class ShardSearchRequestTests extends AbstractSearchTestCase {
55-
private IndexMetadata baseMetadata = IndexMetadata.builder("test").settings(Settings.builder()
55+
private static final IndexMetadata BASE_METADATA = IndexMetadata.builder("test").settings(Settings.builder()
5656
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build())
5757
.numberOfShards(1).numberOfReplicas(1).build();
5858

@@ -105,7 +105,7 @@ private ShardSearchRequest createShardSearchRequest() throws IOException {
105105
}
106106

107107
public void testFilteringAliases() throws Exception {
108-
IndexMetadata indexMetadata = baseMetadata;
108+
IndexMetadata indexMetadata = BASE_METADATA;
109109
indexMetadata = add(indexMetadata, "cats", filter(termQuery("animal", "cat")));
110110
indexMetadata = add(indexMetadata, "dogs", filter(termQuery("animal", "dog")));
111111
indexMetadata = add(indexMetadata, "all", null);
@@ -131,7 +131,7 @@ public void testFilteringAliases() throws Exception {
131131
}
132132

133133
public void testRemovedAliasFilter() throws Exception {
134-
IndexMetadata indexMetadata = baseMetadata;
134+
IndexMetadata indexMetadata = BASE_METADATA;
135135
indexMetadata = add(indexMetadata, "cats", filter(termQuery("animal", "cat")));
136136
indexMetadata = remove(indexMetadata, "cats");
137137
try {
@@ -143,7 +143,7 @@ public void testRemovedAliasFilter() throws Exception {
143143
}
144144

145145
public void testUnknownAliasFilter() throws Exception {
146-
IndexMetadata indexMetadata = baseMetadata;
146+
IndexMetadata indexMetadata = BASE_METADATA;
147147
indexMetadata = add(indexMetadata, "cats", filter(termQuery("animal", "cat")));
148148
indexMetadata = add(indexMetadata, "dogs", filter(termQuery("animal", "dog")));
149149
IndexMetadata finalIndexMetadata = indexMetadata;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ public static SearchSourceBuilder randomSearchSourceBuilder(
373373
if (randomBoolean()) {
374374
builder.collapse(randomCollapseBuilder.get());
375375
}
376+
if (randomBoolean()) {
377+
builder.pointInTimeBuilder(new SearchSourceBuilder.PointInTimeBuilder(randomAlphaOfLengthBetween(3, 10),
378+
TimeValue.timeValueMinutes(randomIntBetween(1, 60))));
379+
}
376380
return builder;
377381
}
378382
}

0 commit comments

Comments
 (0)