renatoh commented on code in PR #2489:
URL: https://github.com/apache/solr/pull/2489#discussion_r1624657341


##########
solr/core/src/java/org/apache/solr/search/combining/ReciprocalRankFusion.java:
##########
@@ -0,0 +1,208 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.search.combining;
+
+import static 
org.apache.lucene.search.TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.StringJoiner;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.search.Explanation;
+import org.apache.lucene.search.Query;
+import org.apache.solr.common.params.CombinerParams;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.SimpleOrderedMap;
+import org.apache.solr.schema.IndexSchema;
+import org.apache.solr.search.DocIterator;
+import org.apache.solr.search.DocList;
+import org.apache.solr.search.DocSlice;
+import org.apache.solr.search.QueryResult;
+import org.apache.solr.search.SolrIndexSearcher;
+import org.apache.solr.search.SortedIntDocSet;
+
+/**
+ * Reciprocal Rank Fusion (RRF) is an algorithm that takes in input multiple 
ranked lists to produce
+ * a unified result set. Examples of use cases where RRF can be used include 
hybrid search and
+ * multiple Knn vector queries executed concurrently. RRF is based on the 
concept of reciprocal
+ * rank, which is the inverse of the rank of a document in a ranked list of 
search results. The
+ * combination of search results happens taking into account the position of 
the items in the
+ * original rankings, and giving higher score to items that are ranked higher 
in multiple lists. RRF
+ * was introduced the first time by Cormack et al. in [1].<br>
+ *
+ * <p>[1] Cormack, Gordon V. et al. “Reciprocal rank fusion outperforms 
condorcet and individual
+ * rank learning methods.” Proceedings of the 32nd international ACM SIGIR 
conference on Research
+ * and development in information retrieval (2009)<br>
+ */
+public class ReciprocalRankFusion extends QueriesCombiner {
+  int k;
+
+  public ReciprocalRankFusion(SolrParams requestParams) {
+    super(requestParams);
+    this.k =
+        requestParams.getInt(CombinerParams.COMBINER_RRF_K, 
CombinerParams.COMBINER_RRF_K_DEFAULT);
+  }
+
+  @Override
+  public QueryResult combine(QueryResult[] rankedLists) {
+    QueryResult combinedRankedList = initCombinedResult(rankedLists);
+    List<DocList> docLists = new ArrayList<>(rankedLists.length);
+    for (QueryResult rankedList : rankedLists) {
+      docLists.add(rankedList.getDocList());
+    }
+    combineResults(combinedRankedList, docLists, false);
+    return combinedRankedList;
+  }
+
+  private Map<Integer, Integer[]> combineResults(
+      QueryResult combinedRankedList,
+      List<DocList> rankedLists,
+      boolean saveRankPositionsForExplain) {
+    Map<Integer, Integer[]> docIdToRanks = null;
+    HashMap<Integer, Float> docIdToScore = new HashMap<>();
+    for (DocList rankedList : rankedLists) {
+      DocIterator docs = rankedList.iterator();
+      int ranking = 1;
+      while (docs.hasNext() && ranking <= upTo) {
+        int docId = docs.nextDoc();
+        float rrfScore = 1f / (k + ranking);
+        docIdToScore.compute(docId, (id, score) -> (score == null) ? rrfScore 
: score + rrfScore);
+        ranking++;
+      }
+    }
+    Stream<Map.Entry<Integer, Float>> sortedByScoreDescending =
+        docIdToScore.entrySet().stream()
+            .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
+
+    int combinedResultsLength = docIdToScore.size();
+    int[] combinedResultsDocIds = new int[combinedResultsLength];
+    float[] combinedResultScores = new float[combinedResultsLength];
+
+    int i = 0;
+    for (Map.Entry<Integer, Float> scoredDoc :
+        sortedByScoreDescending.collect(Collectors.toList())) {
+      combinedResultsDocIds[i] = scoredDoc.getKey();
+      combinedResultScores[i] = scoredDoc.getValue();
+      i++;
+    }
+
+    if (saveRankPositionsForExplain) {
+      docIdToRanks = getRanks(rankedLists, combinedResultsDocIds);
+    }
+
+    DocSlice combinedResultSlice =
+        new DocSlice(
+            0,
+            combinedResultsLength,
+            combinedResultsDocIds,
+            combinedResultScores,
+            combinedResultsLength,
+            combinedResultScores[0],
+            GREATER_THAN_OR_EQUAL_TO);
+    combinedRankedList.setDocList(combinedResultSlice);
+    SortedIntDocSet docSet = new SortedIntDocSet(combinedResultsDocIds, 
combinedResultsLength);
+    combinedRankedList.setDocSet(docSet);
+
+    return docIdToRanks;
+  }
+
+  private Map<Integer, Integer[]> getRanks(List<DocList> docLists, int[] 
combinedResultsDocIDs) {
+    Map<Integer, Integer[]> docIdToRanks;
+    docIdToRanks = new HashMap<>();
+    for (int docID : combinedResultsDocIDs) {
+      docIdToRanks.put(docID, new Integer[docLists.size()]);

Review Comment:
   oh dear, I completely missed the[] and thought you are create a new Integer 
by doing: new Integer() and therefore preventing them from being cached. sorry, 
my bad!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org
For additional commands, e-mail: issues-h...@solr.apache.org

Reply via email to