sims. */
+ public SwitchingSimilarity(Similarity... sims) {
+ this.sims = sims;
+ }
+
+ @Override
+ public long computeNorm(FieldInvertState state) {
+ return sims[0].computeNorm(state);
+ }
+
+ @Override
+ public SimWeight computeWeight(float queryBoost, CollectionStatistics collectionStats, TermStatistics... termStats) {
+ SimWeight subStats[] = new SimWeight[sims.length];
+ for (int i = 0; i < subStats.length; i++) {
+ subStats[i] = sims[i].computeWeight(queryBoost, collectionStats, termStats);
+ }
+ return new SwitchingStats(subStats);
+ }
+
+ @Override
+ public ExactSimScorer exactSimScorer(SimWeight stats, AtomicReaderContext context) throws IOException {
+ ExactSimScorer subScorers[] = new ExactSimScorer[sims.length];
+ for (int i = 0; i < subScorers.length; i++) {
+ subScorers[i] = sims[i].exactSimScorer(((SwitchingStats)stats).subStats[i], context);
+ }
+ return new SwitchingExactSimScorer(subScorers);
+ }
+
+ @Override
+ public SloppySimScorer sloppySimScorer(SimWeight stats, AtomicReaderContext context) throws IOException {
+ SloppySimScorer subScorers[] = new SloppySimScorer[sims.length];
+ for (int i = 0; i < subScorers.length; i++) {
+ subScorers[i] = sims[i].sloppySimScorer(((SwitchingStats)stats).subStats[i], context);
+ }
+ return new SwitchingSloppySimScorer(subScorers);
+ }
+
+ /** Moves to the next sub-scorer. */
+ public void switchScorer() {
+ scorerIndex++;
+ if (scorerIndex == sims.length) {
+ scorerIndex = 0;
+ }
+ }
+
+ public class SwitchingExactSimScorer extends ExactSimScorer {
+ private final ExactSimScorer subScorers[];
+
+ SwitchingExactSimScorer(ExactSimScorer subScorers[]) {
+ this.subScorers = subScorers;
+ }
+
+ @Override
+ public float score(int doc, int freq) {
+ return subScorers[scorerIndex].score(doc, freq);
+ }
+
+ @Override
+ public Explanation explain(int doc, Explanation freq) {
+ return subScorers[scorerIndex].explain(doc, freq);
+ }
+ }
+
+ public class SwitchingSloppySimScorer extends SloppySimScorer {
+ private final SloppySimScorer subScorers[];
+
+ SwitchingSloppySimScorer(SloppySimScorer subScorers[]) {
+ this.subScorers = subScorers;
+ }
+
+ @Override
+ public float score(int doc, float freq) {
+ return subScorers[scorerIndex].score(doc, freq);
+ }
+
+ @Override
+ public Explanation explain(int doc, Explanation freq) {
+ return subScorers[scorerIndex].explain(doc, freq);
+ }
+
+ @Override
+ public float computeSlopFactor(int distance) {
+ return subScorers[0].computeSlopFactor(distance);
+ }
+
+ @Override
+ public float computePayloadFactor(int doc, int start, int end, BytesRef payload) {
+ return subScorers[0].computePayloadFactor(doc, start, end, payload);
+ }
+ }
+
+ static class SwitchingStats extends SimWeight {
+ final SimWeight subStats[];
+
+ SwitchingStats(SimWeight subStats[]) {
+ this.subStats = subStats;
+ }
+
+ @Override
+ public float getValueForNormalization() {
+ float sum = 0.0f;
+ for (SimWeight stat : subStats) {
+ sum += stat.getValueForNormalization();
+ }
+ return sum / subStats.length;
+ }
+
+ @Override
+ public void normalize(float queryNorm, float topLevelBoost) {
+ for (SimWeight stat : subStats) {
+ stat.normalize(queryNorm, topLevelBoost);
+ }
+ }
+ }
+}
Property changes on: lucene/misc/src/java/org/apache/lucene/search/SwitchingSimilarity.java
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Index: lucene/misc/src/java/org/apache/lucene/search/SwitchingSimilarityCollector.java
===================================================================
--- lucene/misc/src/java/org/apache/lucene/search/SwitchingSimilarityCollector.java (revision 0)
+++ lucene/misc/src/java/org/apache/lucene/search/SwitchingSimilarityCollector.java (working copy)
@@ -0,0 +1,77 @@
+package org.apache.lucene.search;
+
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+
+import org.apache.lucene.index.AtomicReaderContext;
+
+/** When used with {@link SwitchingSimilarity}, lets you collect
+ * hits for a single query using multiple similarities into
+ * separate collectors. Be sure the number of collectors
+ * you pass here matches exactly the number of
+ * similarities you pass to {@link SwitchingSimilarity}.
+ *
+ * There can easily be cases where this does not work,
+ * e.g. if the {@link ScoreCachingWrapperScorer} is involved
+ * in the scoring chain.
+ *
+ * @lucene.experimental */
+
+public final class SwitchingSimilarityCollector extends Collector {
+ private final Collector[] subs;
+ private final SwitchingSimilarity switchingSim;
+ private Scorer scorer;
+
+ public SwitchingSimilarityCollector(SwitchingSimilarity switchingSim, Collector... subs) {
+ this.subs = subs;
+ this.switchingSim = switchingSim;
+ }
+
+ @Override
+ public void setScorer(Scorer scorer) throws IOException {
+ this.scorer = scorer;
+ for(Collector sub : subs) {
+ sub.setScorer(scorer);
+ }
+ }
+
+ @Override
+ public void collect(int docID) throws IOException {
+ System.out.println("collect doc=" + docID + " scorer=" + scorer);
+ for(Collector sub : subs) {
+ switchingSim.switchScorer();
+ System.out.println(" score=" + scorer.score());
+ sub.collect(docID);
+ }
+ }
+
+ @Override
+ public void setNextReader(AtomicReaderContext context) throws IOException {
+ for(Collector sub : subs) {
+ sub.setNextReader(context);
+ }
+ }
+
+ @Override
+ public boolean acceptsDocsOutOfOrder() {
+ // We must return false here, because we need the
+ // .score() to not be cached for each hit:
+ return false;
+ }
+}
Property changes on: lucene/misc/src/java/org/apache/lucene/search/SwitchingSimilarityCollector.java
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property