|
7 | 7 | */
|
8 | 8 | package org.opensearch.searchrelevance.judgments;
|
9 | 9 |
|
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.HashMap; |
10 | 12 | import java.util.List;
|
11 | 13 | import java.util.Map;
|
12 | 14 |
|
@@ -52,7 +54,87 @@ public void generateJudgmentRating(Map<String, Object> metadata, ActionListener<
|
52 | 54 | coecClickModel.calculateJudgments(new ActionListener<>() {
|
53 | 55 | @Override
|
54 | 56 | public void onResponse(List<Map<String, Object>> judgments) {
|
55 |
| - listener.onResponse(judgments); |
| 57 | + // Create the result map in the expected format |
| 58 | + List<Map<String, Object>> formattedRatings = new ArrayList<>(); |
| 59 | + for (Map<String, Object> queryJudgment : judgments) { |
| 60 | + String queryText = (String) queryJudgment.get("query"); |
| 61 | + Object ratingData = queryJudgment.get("ratings"); |
| 62 | + |
| 63 | + if (!(ratingData instanceof Map)) { |
| 64 | + listener.onFailure( |
| 65 | + new SearchRelevanceException( |
| 66 | + "queryText " + queryText + " must have rating data as a Map (HashMap).", |
| 67 | + RestStatus.BAD_REQUEST |
| 68 | + ) |
| 69 | + ); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + @SuppressWarnings("unchecked") |
| 74 | + Map<String, Object> ratingsMap = (Map<String, Object>) ratingData; // Cast to Map, not List |
| 75 | + |
| 76 | + // Prepare a list to hold the docId and score maps for the current query |
| 77 | + List<Map<String, String>> docIdScoreList = new ArrayList<>(); |
| 78 | + |
| 79 | + // Iterate over the entrySet of the HashMap *** |
| 80 | + for (Map.Entry<String, Object> entry : ratingsMap.entrySet()) { |
| 81 | + String docId = entry.getKey(); // The key is the docId |
| 82 | + Object ratingObject = entry.getValue(); // The value is the rating |
| 83 | + |
| 84 | + if (docId == null || docId.isEmpty()) { |
| 85 | + // This case is unlikely if the keys of the map are docIds, but good for defensive coding |
| 86 | + listener.onFailure( |
| 87 | + new SearchRelevanceException( |
| 88 | + "docId (map key) for queryText " + queryText + " must not be null or empty", |
| 89 | + RestStatus.BAD_REQUEST |
| 90 | + ) |
| 91 | + ); |
| 92 | + return; |
| 93 | + } |
| 94 | + if (ratingObject == null) { |
| 95 | + listener.onFailure( |
| 96 | + new SearchRelevanceException( |
| 97 | + "rating for docId '" + docId + "' in queryText " + queryText + " must not be null", |
| 98 | + RestStatus.BAD_REQUEST |
| 99 | + ) |
| 100 | + ); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + String rating = String.valueOf(ratingObject); // Convert rating to String |
| 105 | + |
| 106 | + try { |
| 107 | + Float.parseFloat(rating); |
| 108 | + } catch (NumberFormatException e) { |
| 109 | + listener.onFailure( |
| 110 | + new SearchRelevanceException( |
| 111 | + "rating '" |
| 112 | + + rating |
| 113 | + + "' for docId '" |
| 114 | + + docId |
| 115 | + + "' in queryText " |
| 116 | + + queryText |
| 117 | + + " must be a valid float", |
| 118 | + RestStatus.BAD_REQUEST |
| 119 | + ) |
| 120 | + ); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + // Add the docId and score to the list for the current query |
| 125 | + Map<String, String> docScoreMap = new HashMap<>(); |
| 126 | + docScoreMap.put("docId", docId); |
| 127 | + docScoreMap.put("score", rating); |
| 128 | + docIdScoreList.add(docScoreMap); |
| 129 | + } |
| 130 | + |
| 131 | + // Add the formatted ratings for this query |
| 132 | + Map<String, Object> queryRatings = new HashMap<>(); |
| 133 | + queryRatings.put("query", queryText); |
| 134 | + queryRatings.put("ratings", docIdScoreList); |
| 135 | + formattedRatings.add(queryRatings); |
| 136 | + } |
| 137 | + listener.onResponse(formattedRatings); |
56 | 138 | }
|
57 | 139 |
|
58 | 140 | @Override
|
|
0 commit comments