Skip to content

Commit 1594d02

Browse files
committed
fix implicit judgment for the new nested data structure introduced in opensearch-project#77
Signed-off-by: wrigleyDan <[email protected]>
1 parent 27e31a3 commit 1594d02

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

src/main/java/org/opensearch/searchrelevance/judgments/UbiJudgmentsProcessor.java

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
package org.opensearch.searchrelevance.judgments;
99

10+
import java.util.ArrayList;
11+
import java.util.HashMap;
1012
import java.util.List;
1113
import java.util.Map;
1214

@@ -52,7 +54,87 @@ public void generateJudgmentRating(Map<String, Object> metadata, ActionListener<
5254
coecClickModel.calculateJudgments(new ActionListener<>() {
5355
@Override
5456
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);
56138
}
57139

58140
@Override

0 commit comments

Comments
 (0)