Skip to content

Commit 06a95fa

Browse files
committed
Core: Fix IndicesSegmentResponse.toXcontent() serialization (#33414)
When index sorting is enabled, toXContent tried to serialize an SortField object, resulting in an exception, when using the _segments endpoint. Relates #29120
1 parent 90426ef commit 06a95fa

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponse.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ private static void toXContent(XContentBuilder builder, Sort sort) throws IOExce
186186
builder.field("mode", ((SortedSetSortField) field).getSelector()
187187
.toString().toLowerCase(Locale.ROOT));
188188
}
189-
builder.field("missing", field.getMissingValue());
189+
if (field.getMissingValue() != null) {
190+
builder.field("missing", field.getMissingValue().toString());
191+
}
190192
builder.field("reverse", field.getReverse());
191193
builder.endObject();
192194
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.action.admin.indices.segments;
21+
22+
import org.apache.lucene.search.Sort;
23+
import org.apache.lucene.search.SortField;
24+
import org.elasticsearch.cluster.routing.ShardRouting;
25+
import org.elasticsearch.cluster.routing.ShardRoutingState;
26+
import org.elasticsearch.cluster.routing.TestShardRouting;
27+
import org.elasticsearch.common.xcontent.ToXContent;
28+
import org.elasticsearch.common.xcontent.XContentBuilder;
29+
import org.elasticsearch.index.engine.Segment;
30+
import org.elasticsearch.test.ESTestCase;
31+
32+
import java.util.Collections;
33+
34+
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
35+
36+
public class IndicesSegmentResponseTests extends ESTestCase {
37+
38+
public void testToXContentSerialiationWithSortedFields() throws Exception {
39+
ShardRouting shardRouting = TestShardRouting.newShardRouting("foo", 0, "node_id", true, ShardRoutingState.STARTED);
40+
Segment segment = new Segment("my");
41+
42+
SortField sortField = new SortField("foo", SortField.Type.STRING);
43+
sortField.setMissingValue(SortField.STRING_LAST);
44+
segment.segmentSort = new Sort(sortField);
45+
46+
ShardSegments shardSegments = new ShardSegments(shardRouting, Collections.singletonList(segment));
47+
IndicesSegmentResponse response =
48+
new IndicesSegmentResponse(new ShardSegments[] { shardSegments }, 1, 1, 0, Collections.emptyList());
49+
try (XContentBuilder builder = jsonBuilder()) {
50+
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)