Skip to content

Dry up o.e.c.lucene.Lucene transport logic #126646

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 44 additions & 76 deletions server/src/main/java/org/elasticsearch/common/lucene/Lucene.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,15 @@ public static TopDocsAndMaxScore readTopDocs(StreamInput in) throws IOException
} else if (type == 1) {
TotalHits totalHits = readTotalHits(in);
float maxScore = in.readFloat();
SortField[] fields = in.readArray(Lucene::readSortField, SortField[]::new);
FieldDoc[] fieldDocs = new FieldDoc[in.readVInt()];
for (int i = 0; i < fieldDocs.length; i++) {
fieldDocs[i] = readFieldDoc(in);
}
SortField[] fields = readSortFieldArray(in);
FieldDoc[] fieldDocs = in.readArray(Lucene::readFieldDoc, FieldDoc[]::new);
return new TopDocsAndMaxScore(new TopFieldDocs(totalHits, fieldDocs, fields), maxScore);
} else if (type == 2) {
TotalHits totalHits = readTotalHits(in);
float maxScore = in.readFloat();

String field = in.readString();
SortField[] fields = in.readArray(Lucene::readSortField, SortField[]::new);
SortField[] fields = readSortFieldArray(in);
int size = in.readVInt();
Object[] collapseValues = new Object[size];
FieldDoc[] fieldDocs = new FieldDoc[size];
Expand All @@ -339,65 +336,30 @@ public static TopDocsAndMaxScore readTopDocs(StreamInput in) throws IOException
}

public static FieldDoc readFieldDoc(StreamInput in) throws IOException {
Comparable<?>[] cFields = new Comparable<?>[in.readVInt()];
for (int j = 0; j < cFields.length; j++) {
byte type = in.readByte();
if (type == 0) {
cFields[j] = null;
} else if (type == 1) {
cFields[j] = in.readString();
} else if (type == 2) {
cFields[j] = in.readInt();
} else if (type == 3) {
cFields[j] = in.readLong();
} else if (type == 4) {
cFields[j] = in.readFloat();
} else if (type == 5) {
cFields[j] = in.readDouble();
} else if (type == 6) {
cFields[j] = in.readByte();
} else if (type == 7) {
cFields[j] = in.readShort();
} else if (type == 8) {
cFields[j] = in.readBoolean();
} else if (type == 9) {
cFields[j] = in.readBytesRef();
} else if (type == 10) {
cFields[j] = new BigInteger(in.readString());
} else {
throw new IOException("Can't match type [" + type + "]");
}
}
return new FieldDoc(in.readVInt(), in.readFloat(), cFields);
var sortValues = readSortValues(in);
return new FieldDoc(in.readVInt(), in.readFloat(), sortValues);
}

public static Object[] readSortValues(StreamInput in) throws IOException {
return in.readArray(Lucene::readSortValue, Object[]::new);
}

public static Comparable<?> readSortValue(StreamInput in) throws IOException {
byte type = in.readByte();
if (type == 0) {
return null;
} else if (type == 1) {
return in.readString();
} else if (type == 2) {
return in.readInt();
} else if (type == 3) {
return in.readLong();
} else if (type == 4) {
return in.readFloat();
} else if (type == 5) {
return in.readDouble();
} else if (type == 6) {
return in.readByte();
} else if (type == 7) {
return in.readShort();
} else if (type == 8) {
return in.readBoolean();
} else if (type == 9) {
return in.readBytesRef();
} else if (type == 10) {
return new BigInteger(in.readString());
} else {
throw new IOException("Can't match type [" + type + "]");
}
return switch (type) {
case 0 -> null;
case 1 -> in.readString();
case 2 -> in.readInt();
case 3 -> in.readLong();
case 4 -> in.readFloat();
case 5 -> in.readDouble();
case 6 -> in.readByte();
case 7 -> in.readShort();
case 8 -> in.readBoolean();
case 9 -> in.readBytesRef();
case 10 -> new BigInteger(in.readString());
default -> throw new IOException("Can't match type [" + type + "]");
};
}

public static ScoreDoc readScoreDoc(StreamInput in) throws IOException {
Expand Down Expand Up @@ -426,7 +388,7 @@ public static void writeTopDocsIncludingShardIndex(StreamOutput out, TopDocs top
out.writeByte((byte) 2);
writeTotalHits(out, topDocs.totalHits);
out.writeString(topFieldGroups.field);
out.writeArray(Lucene::writeSortField, topFieldGroups.fields);
writeSortFieldArray(out, topFieldGroups.fields);
out.writeVInt(topDocs.scoreDocs.length);
for (int i = 0; i < topDocs.scoreDocs.length; i++) {
ScoreDoc doc = topFieldGroups.scoreDocs[i];
Expand All @@ -437,7 +399,7 @@ public static void writeTopDocsIncludingShardIndex(StreamOutput out, TopDocs top
} else if (topDocs instanceof TopFieldDocs topFieldDocs) {
out.writeByte((byte) 1);
writeTotalHits(out, topDocs.totalHits);
out.writeArray(Lucene::writeSortField, topFieldDocs.fields);
writeSortFieldArray(out, topFieldDocs.fields);
out.writeArray((o, doc) -> {
writeFieldDoc(o, (FieldDoc) doc);
o.writeVInt(doc.shardIndex);
Expand All @@ -452,6 +414,10 @@ public static void writeTopDocsIncludingShardIndex(StreamOutput out, TopDocs top
}
}

public static void writeSortFieldArray(StreamOutput out, SortField[] sortFields) throws IOException {
out.writeArray(Lucene::writeSortField, sortFields);
}

/**
* Read side counterpart to {@link #writeTopDocsIncludingShardIndex} and the same as {@link #readTopDocs(StreamInput)} but for the
* added shard index values that are read.
Expand All @@ -474,7 +440,7 @@ public static TopDocs readTopDocsIncludingShardIndex(StreamInput in) throws IOEx
return new TopDocs(totalHits, scoreDocs);
} else if (type == 1) {
TotalHits totalHits = readTotalHits(in);
SortField[] fields = in.readArray(Lucene::readSortField, SortField[]::new);
SortField[] fields = readSortFieldArray(in);
FieldDoc[] fieldDocs = new FieldDoc[in.readVInt()];
for (int i = 0; i < fieldDocs.length; i++) {
var fieldDoc = readFieldDoc(in);
Expand All @@ -485,7 +451,7 @@ public static TopDocs readTopDocsIncludingShardIndex(StreamInput in) throws IOEx
} else if (type == 2) {
TotalHits totalHits = readTotalHits(in);
String field = in.readString();
SortField[] fields = in.readArray(Lucene::readSortField, SortField[]::new);
SortField[] fields = readSortFieldArray(in);
int size = in.readVInt();
Object[] collapseValues = new Object[size];
FieldDoc[] fieldDocs = new FieldDoc[size];
Expand All @@ -501,6 +467,10 @@ public static TopDocs readTopDocsIncludingShardIndex(StreamInput in) throws IOEx
}
}

public static SortField[] readSortFieldArray(StreamInput in) throws IOException {
return in.readArray(Lucene::readSortField, SortField[]::new);
}

public static void writeTopDocs(StreamOutput out, TopDocsAndMaxScore topDocs) throws IOException {
if (topDocs.topDocs instanceof TopFieldGroups topFieldGroups) {
out.writeByte((byte) 2);
Expand All @@ -509,7 +479,7 @@ public static void writeTopDocs(StreamOutput out, TopDocsAndMaxScore topDocs) th
out.writeFloat(topDocs.maxScore);

out.writeString(topFieldGroups.field);
out.writeArray(Lucene::writeSortField, topFieldGroups.fields);
writeSortFieldArray(out, topFieldGroups.fields);

out.writeVInt(topFieldGroups.scoreDocs.length);
for (int i = 0; i < topFieldGroups.scoreDocs.length; i++) {
Expand All @@ -523,7 +493,7 @@ public static void writeTopDocs(StreamOutput out, TopDocsAndMaxScore topDocs) th
writeTotalHits(out, topFieldDocs.totalHits);
out.writeFloat(topDocs.maxScore);

out.writeArray(Lucene::writeSortField, topFieldDocs.fields);
writeSortFieldArray(out, topFieldDocs.fields);
out.writeArray((o, doc) -> writeFieldDoc(o, (FieldDoc) doc), topFieldDocs.scoreDocs);
} else {
out.writeByte((byte) 0);
Expand Down Expand Up @@ -598,14 +568,17 @@ public static void writeSortValue(StreamOutput out, Object field) throws IOExcep

public static void writeFieldDoc(StreamOutput out, FieldDoc fieldDoc) throws IOException {
out.writeArray(Lucene::writeSortValue, fieldDoc.fields);
out.writeVInt(fieldDoc.doc);
out.writeFloat(fieldDoc.score);
doWriteScoreDoc(out, fieldDoc);
}

public static void writeScoreDoc(StreamOutput out, ScoreDoc scoreDoc) throws IOException {
if (scoreDoc.getClass().equals(ScoreDoc.class) == false) {
throw new IllegalArgumentException("This method can only be used to serialize a ScoreDoc, not a " + scoreDoc.getClass());
}
doWriteScoreDoc(out, scoreDoc);
}

private static void doWriteScoreDoc(StreamOutput out, ScoreDoc scoreDoc) throws IOException {
out.writeVInt(scoreDoc.doc);
out.writeFloat(scoreDoc.score);
}
Expand Down Expand Up @@ -662,17 +635,12 @@ private static SortField rewriteMergeSortField(SortField sortField) {
}
}

public static void writeSortField(StreamOutput out, SortField sortField) throws IOException {
static void writeSortField(StreamOutput out, SortField sortField) throws IOException {
sortField = rewriteMergeSortField(sortField);
if (sortField.getClass() != SortField.class) {
throw new IllegalArgumentException("Cannot serialize SortField impl [" + sortField + "]");
}
if (sortField.getField() == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeString(sortField.getField());
}
out.writeOptionalString(sortField.getField());
if (sortField.getComparatorSource() != null) {
IndexFieldData.XFieldComparatorSource comparatorSource = (IndexFieldData.XFieldComparatorSource) sortField
.getComparatorSource();
Expand Down
6 changes: 3 additions & 3 deletions server/src/main/java/org/elasticsearch/search/SearchHits.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ public static SearchHits readFrom(StreamInput in, boolean pooled) throws IOExcep
isPooled = isPooled || hit.isPooled();
}
}
var sortFields = in.readOptionalArray(Lucene::readSortField, SortField[]::new);
var sortFields = in.readOptional(Lucene::readSortFieldArray);
var collapseField = in.readOptionalString();
var collapseValues = in.readOptionalArray(Lucene::readSortValue, Object[]::new);
var collapseValues = in.readOptional(Lucene::readSortValues);
if (isPooled) {
return new SearchHits(hits, totalHits, maxScore, sortFields, collapseField, collapseValues);
} else {
Expand All @@ -164,7 +164,7 @@ public void writeTo(StreamOutput out) throws IOException {
}
out.writeFloat(maxScore);
out.writeArray(hits);
out.writeOptionalArray(Lucene::writeSortField, sortFields);
out.writeOptional(Lucene::writeSortFieldArray, sortFields);
out.writeOptionalString(collapseField);
out.writeOptionalArray(Lucene::writeSortValue, collapseValues);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public SearchSortValues(Object[] rawSortValues, DocValueFormat[] sortValueFormat
}

public static SearchSortValues readFrom(StreamInput in) throws IOException {
Object[] formattedSortValues = in.readArray(Lucene::readSortValue, Object[]::new);
Object[] rawSortValues = in.readArray(Lucene::readSortValue, Object[]::new);
Object[] formattedSortValues = Lucene.readSortValues(in);
Object[] rawSortValues = Lucene.readSortValues(in);
if (formattedSortValues.length == 0 && rawSortValues.length == 0) {
return EMPTY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public SearchSortValuesAndFormats(Object[] rawSortValues, DocValueFormat[] sortV
}

public SearchSortValuesAndFormats(StreamInput in) throws IOException {
this.rawSortValues = in.readArray(Lucene::readSortValue, Object[]::new);
this.formattedSortValues = in.readArray(Lucene::readSortValue, Object[]::new);
this.rawSortValues = Lucene.readSortValues(in);
this.formattedSortValues = Lucene.readSortValues(in);
this.sortValueFormats = new DocValueFormat[formattedSortValues.length];
for (int i = 0; i < sortValueFormats.length; ++i) {
sortValueFormats[i] = in.readNamedWriteable(DocValueFormat.class);
Expand Down