From b9bf8ca6129201b9d5fdfa377f270c8794fe94e9 Mon Sep 17 00:00:00 2001 From: jzonthemtn Date: Wed, 11 Dec 2024 16:48:53 -0500 Subject: [PATCH] Adding precision test. Signed-off-by: jzonthemtn --- .../eval/metrics/PrecisionSearchMetric.java | 4 +-- .../eval/metrics/NdcgSearchMetricTest.java | 4 +-- .../metrics/PrecisionSearchMetricTest.java | 30 +++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 opensearch-search-quality-evaluation-plugin/src/test/java/org/opensearch/eval/metrics/PrecisionSearchMetricTest.java diff --git a/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/metrics/PrecisionSearchMetric.java b/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/metrics/PrecisionSearchMetric.java index fc4b80c..a2ac50b 100644 --- a/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/metrics/PrecisionSearchMetric.java +++ b/opensearch-search-quality-evaluation-plugin/src/main/java/org/opensearch/eval/metrics/PrecisionSearchMetric.java @@ -40,7 +40,7 @@ public String getName() { @Override public double calculate() { - int numberOfRelevantItems = 0; + double numberOfRelevantItems = 0; for(final double relevanceScore : relevanceScores) { if(relevanceScore >= threshold) { @@ -48,7 +48,7 @@ public double calculate() { } } - return (double) numberOfRelevantItems / (double) k; + return numberOfRelevantItems / (double) k; } diff --git a/opensearch-search-quality-evaluation-plugin/src/test/java/org/opensearch/eval/metrics/NdcgSearchMetricTest.java b/opensearch-search-quality-evaluation-plugin/src/test/java/org/opensearch/eval/metrics/NdcgSearchMetricTest.java index 437a6f1..b0d672b 100644 --- a/opensearch-search-quality-evaluation-plugin/src/test/java/org/opensearch/eval/metrics/NdcgSearchMetricTest.java +++ b/opensearch-search-quality-evaluation-plugin/src/test/java/org/opensearch/eval/metrics/NdcgSearchMetricTest.java @@ -20,9 +20,9 @@ public void testCalculate() { final List relevanceScores = List.of(1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 0.0); final NdcgSearchMetric ndcgSearchMetric = new NdcgSearchMetric(k, relevanceScores); - final double dcg = ndcgSearchMetric.calculate(); + final double ndcg = ndcgSearchMetric.calculate(); - assertEquals(0.7151195094457645, dcg, 0.0); + assertEquals(0.7151195094457645, ndcg, 0.0); } diff --git a/opensearch-search-quality-evaluation-plugin/src/test/java/org/opensearch/eval/metrics/PrecisionSearchMetricTest.java b/opensearch-search-quality-evaluation-plugin/src/test/java/org/opensearch/eval/metrics/PrecisionSearchMetricTest.java new file mode 100644 index 0000000..14f6c9a --- /dev/null +++ b/opensearch-search-quality-evaluation-plugin/src/test/java/org/opensearch/eval/metrics/PrecisionSearchMetricTest.java @@ -0,0 +1,30 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ +package org.opensearch.eval.metrics; + +import org.opensearch.test.OpenSearchTestCase; + +import java.util.List; + +public class PrecisionSearchMetricTest extends OpenSearchTestCase { + + public void testCalculate() { + + final int k = 5; + final double threshold = 1.0; + final List relevanceScores = List.of(1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 0.0); + + final PrecisionSearchMetric precisionSearchMetric = new PrecisionSearchMetric(k, threshold, relevanceScores); + final double precision = precisionSearchMetric.calculate(); + + assertEquals(1.8, precision, 0.0); + + } + +}