Skip to content

Commit

Permalink
Clean up, adding javadocs, removing unnecessary logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzonthemtn committed Dec 6, 2024
1 parent 8208f25 commit 4b2c2d6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public String getName() {
@Override
public double calculate() {

// TODO: Implement this.
// TODO: Implement precision calculation.
return 0.0;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.client.Client;
import org.opensearch.core.action.ActionListener;
import org.opensearch.eval.SearchQualityEvaluationPlugin;
import org.opensearch.index.query.BoolQueryBuilder;
import org.opensearch.index.query.QueryBuilders;
Expand Down Expand Up @@ -146,8 +145,6 @@ public Double getJudgmentValue(final String judgmentsId, final String query, fin

} else {

// LOGGER.info("No judgments found for query: {}; documentId = {}; judgmentsId = {}", query, documentId, judgmentsId);

// No judgment for this query/doc pair exists.
judgment = Double.NaN;

Expand All @@ -157,9 +154,16 @@ public Double getJudgmentValue(final String judgmentsId, final String query, fin

}

public List<Double> getRelevanceScores(final String judgmentsId, final String query, final List<String> orderedDocumentIds, final int k) throws Exception {

// LOGGER.info("Getting relevance scores for query: {}, k = {}, docIds size = {}", query, k, orderedDocumentIds.size());
/**
* Gets the judgments for a query / document pairs.
* @param judgmentsId The judgments collection for which the judgment to retrieve belongs.
* @param query The user query.
* @param orderedDocumentIds A list of document IDs returned for the user query.
* @param k The k used for metrics calculation, i.e. DCG@k.
* @return An ordered list of relevance scores for the query / document pairs.
* @throws Exception Thrown if a judgment cannot be retrieved.
*/
protected List<Double> getRelevanceScores(final String judgmentsId, final String query, final List<String> orderedDocumentIds, final int k) throws Exception {

// Ordered list of scores.
final List<Double> scores = new ArrayList<>();
Expand All @@ -172,26 +176,13 @@ public List<Double> getRelevanceScores(final String judgmentsId, final String qu
// Find the judgment value for this combination of query and documentId from the index.
final Double judgmentValue = getJudgmentValue(judgmentsId, query, documentId);

// LOGGER.info("Got judgment value: {}", judgmentValue);

// If a judgment for this query/doc pair is not found, Double.NaN will be returned.
if(!Double.isNaN(judgmentValue)) {
//LOGGER.info("Adding score {} for query {}", judgmentValue, query);
scores.add(judgmentValue);
}

// if (i == orderedDocumentIds.size()) {
// // k is greater than the actual length of documents.
// break;
// }

}

// LOGGER.info("----- scores size: " + scores.size());

//final String listOfScores = scores.stream().map(Object::toString).collect(Collectors.joining(", "));
//LOGGER.info("Got relevance scores: size = {}: scores = {}", listOfScores.length(), listOfScores);

return scores;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.opensearch.eval.SearchQualityEvaluationPlugin;
import org.opensearch.eval.metrics.DcgSearchMetric;
import org.opensearch.eval.metrics.NdcgSearchMetric;
import org.opensearch.eval.metrics.PrecisionSearchMetric;
import org.opensearch.eval.metrics.SearchMetric;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.search.SearchHit;
Expand Down Expand Up @@ -109,19 +110,14 @@ public void onResponse(final SearchResponse searchResponse) {

try {

// TODO: If no hits are returned, there's no need to get the relevance scores.
final List<Double> relevanceScores = getRelevanceScores(judgmentsId, userQuery, orderedDocumentIds, k);

// Calculate the metrics for this query.
final SearchMetric dcgSearchMetric = new DcgSearchMetric(k, relevanceScores);
final SearchMetric ndcgSearchmetric = new NdcgSearchMetric(k, relevanceScores);
final SearchMetric precisionSearchMetric = new PrecisionSearchMetric(k, relevanceScores);

// TODO: Add these metrics in, too.
//final SearchMetric precisionSearchMetric = new PrecisionSearchMetric(k, relevanceScores);

//LOGGER.info("size list for query {}: {}", userQuery, relevanceScores.size());
//LOGGER.info("query set ({}) dcg = {}", userQuery, dcgSearchMetric.getValue());

final Collection<SearchMetric> searchMetrics = List.of(dcgSearchMetric); // ndcgSearchmetric, precisionSearchMetric);
final Collection<SearchMetric> searchMetrics = List.of(dcgSearchMetric, ndcgSearchmetric, precisionSearchMetric);

queryResults.add(new QueryResult(userQuery, orderedDocumentIds, k, searchMetrics));

Expand Down

0 comments on commit 4b2c2d6

Please sign in to comment.