|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | +package org.elasticsearch.xpack.rank.linear; |
| 9 | + |
| 10 | +/* |
| 11 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 12 | + * or more contributor license agreements. See the NOTICE file |
| 13 | + * distributed with this work for additional information |
| 14 | + * regarding copyright ownership. The ASF licenses this file |
| 15 | + * to you under the Apache License, Version 2.0 (the |
| 16 | + * "License"); you may not use this file except in compliance |
| 17 | + * with the License. You may obtain a copy of the License at |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | + * |
| 21 | + * Unless required by applicable law or agreed to in writing, |
| 22 | + * software distributed under the License is distributed on an |
| 23 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 24 | + * KIND, either express or implied. See the License for the |
| 25 | + * specific language governing permissions and limitations |
| 26 | + * under the License. |
| 27 | + */ |
| 28 | + |
| 29 | + |
| 30 | +import org.apache.lucene.search.ScoreDoc; |
| 31 | +import org.elasticsearch.search.rank.RankDoc; |
| 32 | +import org.elasticsearch.search.retriever.CompoundRetrieverBuilder.RetrieverSource; |
| 33 | +import org.elasticsearch.search.retriever.TestRetrieverBuilder; |
| 34 | +import org.elasticsearch.test.ESTestCase; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.List; |
| 38 | +import java.util.stream.Stream; |
| 39 | + |
| 40 | +public class LinearRetrieverBuilderTests extends ESTestCase { |
| 41 | + |
| 42 | + public void testCombineInnerRetrieverResultsCalculatesWeightedSum() throws IOException { |
| 43 | + List<RetrieverSource> sources = List.of( |
| 44 | + new RetrieverSource(new TestRetrieverBuilder("r1"), null), |
| 45 | + new RetrieverSource(new TestRetrieverBuilder("r2"), null) |
| 46 | + ); |
| 47 | + int rankWindowSize = 5; |
| 48 | + float[] weights = new float[] { 1.0f, 2.0f }; |
| 49 | + ScoreNormalizer[] normalizers = new ScoreNormalizer[] { IdentityScoreNormalizer.INSTANCE, IdentityScoreNormalizer.INSTANCE }; |
| 50 | + float minScore = 0f; |
| 51 | + |
| 52 | + LinearRetrieverBuilder builder = new LinearRetrieverBuilder(sources, rankWindowSize, weights, normalizers, minScore); |
| 53 | + |
| 54 | + ScoreDoc[] left = new ScoreDoc[] { new ScoreDoc(5, 1.0f, 0), new ScoreDoc(6, 2.0f, 0) }; |
| 55 | + ScoreDoc[] right = new ScoreDoc[] { new ScoreDoc(5, 3.0f, 0), new ScoreDoc(6, 1.0f, 0), new ScoreDoc(7, 0.5f, 0) }; |
| 56 | + |
| 57 | + RankDoc[] results = builder.combineInnerRetrieverResults(List.of(left, right), false); |
| 58 | + |
| 59 | + assertEquals("Should have 3 combined docs", 3, results.length); |
| 60 | + |
| 61 | + assertEquals("Doc 5 score", 7.0f, results[0].score, 0.001f); |
| 62 | + assertEquals("Doc 5 ID", 5, results[0].doc); |
| 63 | + assertEquals("Doc 5 rank", 1, results[0].rank); |
| 64 | + |
| 65 | + assertEquals("Doc 6 score", 4.0f, results[1].score, 0.001f); |
| 66 | + assertEquals("Doc 6 ID", 6, results[1].doc); |
| 67 | + assertEquals("Doc 6 rank", 2, results[1].rank); |
| 68 | + |
| 69 | + assertEquals("Doc 7 score", 1.0f, results[2].score, 0.001f); |
| 70 | + assertEquals("Doc 7 ID", 7, results[2].doc); |
| 71 | + assertEquals("Doc 7 rank", 3, results[2].rank); |
| 72 | + } |
| 73 | + |
| 74 | + public void testCombineAndFilterWithMinScore() throws IOException { |
| 75 | + List<RetrieverSource> sources = List.of( |
| 76 | + new RetrieverSource(new TestRetrieverBuilder("r1"), null), |
| 77 | + new RetrieverSource(new TestRetrieverBuilder("r2"), null) |
| 78 | + ); |
| 79 | + int rankWindowSize = 5; |
| 80 | + float[] weights = new float[] { 1.0f, 1.0f }; |
| 81 | + ScoreNormalizer[] normalizers = new ScoreNormalizer[] { IdentityScoreNormalizer.INSTANCE, IdentityScoreNormalizer.INSTANCE }; |
| 82 | + final float minScoreThreshold = 1.5f; |
| 83 | + |
| 84 | + LinearRetrieverBuilder builder = new LinearRetrieverBuilder(sources, rankWindowSize, weights, normalizers, minScoreThreshold); |
| 85 | + assertEquals(minScoreThreshold, builder.getMinScore(), 0f); |
| 86 | + |
| 87 | + ScoreDoc[] left = new ScoreDoc[] { new ScoreDoc(0, 1.0f, 0), new ScoreDoc(1, 0.8f, 0) }; |
| 88 | + ScoreDoc[] right = new ScoreDoc[] { new ScoreDoc(0, 2.0f, 0), new ScoreDoc(1, 0.6f, 0), new ScoreDoc(2, 1.8f, 0) }; |
| 89 | + |
| 90 | + RankDoc[] combinedRankDocs = builder.combineInnerRetrieverResults(List.of(left, right), false); |
| 91 | + assertEquals("Combined docs count (already filtered)", 2, combinedRankDocs.length); |
| 92 | + |
| 93 | + List<RankDoc> filteredDocs = Stream.of(combinedRankDocs).sorted().toList(); |
| 94 | + |
| 95 | + assertEquals("Filtered docs count", 2, filteredDocs.size()); |
| 96 | + |
| 97 | + boolean foundDoc0 = false; |
| 98 | + boolean foundDoc2 = false; |
| 99 | + for (RankDoc scoreDoc : filteredDocs) { |
| 100 | + assertTrue("Score should be >= minScore", scoreDoc.score >= minScoreThreshold); |
| 101 | + if (scoreDoc.doc == 0) { |
| 102 | + assertEquals("Doc 0 score", 3.0f, scoreDoc.score, 0.001f); |
| 103 | + foundDoc0 = true; |
| 104 | + } else if (scoreDoc.doc == 2) { |
| 105 | + assertEquals("Doc 2 score", 1.8f, scoreDoc.score, 0.001f); |
| 106 | + foundDoc2 = true; |
| 107 | + } else { |
| 108 | + fail("Unexpected document ID returned: " + scoreDoc.doc + " (should have been filtered)"); |
| 109 | + } |
| 110 | + } |
| 111 | + assertTrue("Document 0 should have been found", foundDoc0); |
| 112 | + assertTrue("Document 2 should have been found", foundDoc2); |
| 113 | + } |
| 114 | +} |
0 commit comments