Skip to content
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

Reset the ShardTargetType after serializing inner hits. #12261

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -38,12 +38,12 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.SearchShardTarget;
import org.elasticsearch.search.highlight.HighlightField;
import org.elasticsearch.search.internal.InternalSearchHits.StreamContext.ShardTargetType;
import org.elasticsearch.search.lookup.SourceLookup;

import java.io.IOException;
Expand Down Expand Up @@ -556,7 +556,7 @@ public static InternalSearchHit readSearchHit(StreamInput in, InternalSearchHits

@Override
public void readFrom(StreamInput in) throws IOException {
readFrom(in, InternalSearchHits.streamContext().streamShardTarget(InternalSearchHits.StreamContext.ShardTargetType.STREAM));
readFrom(in, InternalSearchHits.streamContext().streamShardTarget(ShardTargetType.STREAM));
}

public void readFrom(StreamInput in, InternalSearchHits.StreamContext context) throws IOException {
Expand Down Expand Up @@ -678,11 +678,11 @@ public void readFrom(StreamInput in, InternalSearchHits.StreamContext context) t
}
}

if (context.streamShardTarget() == InternalSearchHits.StreamContext.ShardTargetType.STREAM) {
if (context.streamShardTarget() == ShardTargetType.STREAM) {
if (in.readBoolean()) {
shard = readSearchShardTarget(in);
}
} else if (context.streamShardTarget() == InternalSearchHits.StreamContext.ShardTargetType.LOOKUP) {
} else if (context.streamShardTarget() == ShardTargetType.LOOKUP) {
int lookupId = in.readVInt();
if (lookupId > 0) {
shard = context.handleShardLookup().get(lookupId);
Expand All @@ -694,15 +694,17 @@ public void readFrom(StreamInput in, InternalSearchHits.StreamContext context) t
innerHits = new HashMap<>(size);
for (int i = 0; i < size; i++) {
String key = in.readString();
InternalSearchHits value = InternalSearchHits.readSearchHits(in, InternalSearchHits.streamContext().streamShardTarget(InternalSearchHits.StreamContext.ShardTargetType.NO_STREAM));
ShardTargetType shardTarget = InternalSearchHits.streamContext().streamShardTarget();
InternalSearchHits value = InternalSearchHits.readSearchHits(in, InternalSearchHits.streamContext().streamShardTarget(ShardTargetType.NO_STREAM));
InternalSearchHits.streamContext().streamShardTarget(shardTarget);
innerHits.put(key, value);
}
}
}

@Override
public void writeTo(StreamOutput out) throws IOException {
writeTo(out, InternalSearchHits.streamContext().streamShardTarget(InternalSearchHits.StreamContext.ShardTargetType.STREAM));
writeTo(out, InternalSearchHits.streamContext().streamShardTarget(ShardTargetType.STREAM));
}

public void writeTo(StreamOutput out, InternalSearchHits.StreamContext context) throws IOException {
Expand Down Expand Up @@ -787,14 +789,14 @@ public void writeTo(StreamOutput out, InternalSearchHits.StreamContext context)
}
}

if (context.streamShardTarget() == InternalSearchHits.StreamContext.ShardTargetType.STREAM) {
if (context.streamShardTarget() == ShardTargetType.STREAM) {
if (shard == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
shard.writeTo(out);
}
} else if (context.streamShardTarget() == InternalSearchHits.StreamContext.ShardTargetType.LOOKUP) {
} else if (context.streamShardTarget() == ShardTargetType.LOOKUP) {
if (shard == null) {
out.writeVInt(0);
} else {
Expand All @@ -808,7 +810,9 @@ public void writeTo(StreamOutput out, InternalSearchHits.StreamContext context)
out.writeVInt(innerHits.size());
for (Map.Entry<String, InternalSearchHits> entry : innerHits.entrySet()) {
out.writeString(entry.getKey());
entry.getValue().writeTo(out, InternalSearchHits.streamContext().streamShardTarget(InternalSearchHits.StreamContext.ShardTargetType.NO_STREAM));
ShardTargetType shardTarget = InternalSearchHits.streamContext().streamShardTarget();
entry.getValue().writeTo(out, InternalSearchHits.streamContext().streamShardTarget(ShardTargetType.NO_STREAM));
InternalSearchHits.streamContext().streamShardTarget(shardTarget);
}
}
}
Expand Down
Expand Up @@ -122,6 +122,7 @@ public void testSimpleNested() throws Exception {
assertNoFailures(response);
assertHitCount(response, 1);
assertSearchHit(response, 1, hasId("2"));
assertThat(response.getHits().getAt(0).getShard(), notNullValue());
assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
assertThat(innerHits.totalHits(), equalTo(3l));
Expand Down Expand Up @@ -223,6 +224,7 @@ public void testRandomNested() throws Exception {
assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));
for (int i = 0; i < numDocs; i++) {
SearchHit searchHit = searchResponse.getHits().getAt(i);
assertThat(searchHit.getShard(), notNullValue());
SearchHits inner = searchHit.getInnerHits().get("a");
assertThat(inner.totalHits(), equalTo((long) field1InnerObjects[i]));
for (int j = 0; j < field1InnerObjects[i] && j < size; j++) {
Expand Down Expand Up @@ -275,6 +277,7 @@ public void testSimpleParentChild() throws Exception {
assertNoFailures(response);
assertHitCount(response, 1);
assertSearchHit(response, 1, hasId("1"));
assertThat(response.getHits().getAt(0).getShard(), notNullValue());

assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
Expand Down Expand Up @@ -411,6 +414,7 @@ public void testRandomParentChild() throws Exception {
SearchHit searchHit = searchResponse.getHits().getAt(parent);
assertThat(searchHit.getType(), equalTo("parent"));
assertThat(searchHit.getId(), equalTo(String.format(Locale.ENGLISH, "%03d", parent)));
assertThat(searchHit.getShard(), notNullValue());

SearchHits inner = searchHit.getInnerHits().get("a");
assertThat(inner.totalHits(), equalTo((long) child1InnerObjects[parent]));
Expand Down