|
13 | 13 | import static org.opensearch.neuralsearch.search.util.HybridSearchResultFormatUtil.isHybridQueryStartStopElement;
|
14 | 14 |
|
15 | 15 | import java.util.ArrayList;
|
| 16 | +import java.util.Arrays; |
16 | 17 | import java.util.List;
|
17 | 18 | import java.util.Objects;
|
18 | 19 |
|
@@ -150,4 +151,78 @@ private ScoreDoc deepCopyScoreDoc(final ScoreDoc scoreDoc, final boolean isSortE
|
150 | 151 | FieldDoc fieldDoc = (FieldDoc) scoreDoc;
|
151 | 152 | return new FieldDoc(fieldDoc.doc, fieldDoc.score, fieldDoc.fields, fieldDoc.shardIndex);
|
152 | 153 | }
|
| 154 | + |
| 155 | + @Override |
| 156 | + public boolean equals(Object other) { |
| 157 | + if (this == other) return true; |
| 158 | + if (other == null || getClass() != other.getClass()) return false; |
| 159 | + CompoundTopDocs that = (CompoundTopDocs) other; |
| 160 | + |
| 161 | + if (this.topDocs.size() != that.topDocs.size()) { |
| 162 | + return false; |
| 163 | + } |
| 164 | + for (int i = 0; i < topDocs.size(); i++) { |
| 165 | + TopDocs thisTopDoc = this.topDocs.get(i); |
| 166 | + TopDocs thatTopDoc = that.topDocs.get(i); |
| 167 | + if ((thisTopDoc == null) != (thatTopDoc == null)) { |
| 168 | + return false; |
| 169 | + } |
| 170 | + if (thisTopDoc == null) { |
| 171 | + continue; |
| 172 | + } |
| 173 | + if (Objects.equals(thisTopDoc.totalHits, thatTopDoc.totalHits) == false) { |
| 174 | + return false; |
| 175 | + } |
| 176 | + if (compareScoreDocs(thisTopDoc.scoreDocs, thatTopDoc.scoreDocs) == false) { |
| 177 | + return false; |
| 178 | + } |
| 179 | + } |
| 180 | + return Objects.equals(totalHits, that.totalHits) && Objects.equals(searchShard, that.searchShard); |
| 181 | + } |
| 182 | + |
| 183 | + private boolean compareScoreDocs(ScoreDoc[] first, ScoreDoc[] second) { |
| 184 | + if (first.length != second.length) { |
| 185 | + return false; |
| 186 | + } |
| 187 | + |
| 188 | + for (int i = 0; i < first.length; i++) { |
| 189 | + ScoreDoc firstDoc = first[i]; |
| 190 | + ScoreDoc secondDoc = second[i]; |
| 191 | + if ((firstDoc == null) != (secondDoc == null)) { |
| 192 | + return false; |
| 193 | + } |
| 194 | + if (firstDoc == null) { |
| 195 | + continue; |
| 196 | + } |
| 197 | + if (firstDoc.doc != secondDoc.doc || Float.compare(firstDoc.score, secondDoc.score) != 0) { |
| 198 | + return false; |
| 199 | + } |
| 200 | + if (firstDoc instanceof FieldDoc != secondDoc instanceof FieldDoc) { |
| 201 | + return false; |
| 202 | + } |
| 203 | + if (firstDoc instanceof FieldDoc firstFieldDoc) { |
| 204 | + FieldDoc secondFieldDoc = (FieldDoc) secondDoc; |
| 205 | + if (Arrays.equals(firstFieldDoc.fields, secondFieldDoc.fields) == false) { |
| 206 | + return false; |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + return true; |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public int hashCode() { |
| 215 | + int result = Objects.hash(totalHits, searchShard); |
| 216 | + for (TopDocs topDoc : topDocs) { |
| 217 | + result = 31 * result + topDoc.totalHits.hashCode(); |
| 218 | + for (ScoreDoc scoreDoc : topDoc.scoreDocs) { |
| 219 | + result = 31 * result + Float.floatToIntBits(scoreDoc.score); |
| 220 | + result = 31 * result + scoreDoc.doc; |
| 221 | + if (scoreDoc instanceof FieldDoc fieldDoc && fieldDoc.fields != null) { |
| 222 | + result = 31 * result + Arrays.deepHashCode(fieldDoc.fields); |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + return result; |
| 227 | + } |
153 | 228 | }
|
0 commit comments