cpoerschke commented on a change in pull request #463:
URL: https://github.com/apache/lucene/pull/463#discussion_r780405554



##########
File path: lucene/core/src/java/org/apache/lucene/search/IndriAndWeight.java
##########
@@ -38,8 +38,21 @@ public IndriAndWeight(
     this.boost = boost;
     this.scoreMode = scoreMode;
     weights = new ArrayList<>();
+    // Calculate total boost score so that boost can be normalized
+    float boostSum = 0;
     for (BooleanClause c : query) {
-      Weight w = searcher.createWeight(c.getQuery(), scoreMode, 1.0f);
+      if (c.getQuery() instanceof BoostQuery) {
+        boostSum += ((BoostQuery) c.getQuery()).getBoost();

Review comment:
       subjective: perhaps introduce a local variable to avoid duplicate 
`c.getQuery()` calls.
   
   ```suggestion
         Query q = c.getQuery();
         if (q instanceof BoostQuery) {
           boostSum += ((BoostQuery) q).getBoost();
   ```

##########
File path: lucene/core/src/java/org/apache/lucene/search/IndriOrWeight.java
##########
@@ -0,0 +1,135 @@
+/*
+ * 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.lucene.search;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import org.apache.lucene.index.LeafReaderContext;
+
+/** The Weight for IndriAndQuery, used to normalize, score and explain these 
queries. */
+public class IndriOrWeight extends Weight {
+
+  private final IndriQuery query;
+  private final float boost;
+  private final ArrayList<Weight> weights;
+  private final ScoreMode scoreMode;
+
+  public IndriOrWeight(IndriOrQuery query, IndexSearcher searcher, ScoreMode 
scoreMode, float boost)
+      throws IOException {
+    super(query);
+    this.query = query;
+    this.boost = boost;
+    this.scoreMode = scoreMode;
+    weights = new ArrayList<>();

Review comment:
       subjective/style: `this.weights` here and at line 55 even though (unlike 
for the other members) it's not necessary
   ```suggestion
       this.weights = new ArrayList<>();
   ```

##########
File path: lucene/core/src/java/org/apache/lucene/search/IndriOrWeight.java
##########
@@ -0,0 +1,135 @@
+/*
+ * 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.lucene.search;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import org.apache.lucene.index.LeafReaderContext;
+
+/** The Weight for IndriAndQuery, used to normalize, score and explain these 
queries. */

Review comment:
       ```suggestion
   /** The Weight for IndriOrQuery, used to normalize, score and explain these 
queries. */
   ```

##########
File path: lucene/core/src/java/org/apache/lucene/search/IndriOrScorer.java
##########
@@ -0,0 +1,61 @@
+/*
+ * 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.lucene.search;
+
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * Combines scores of subscorers. If a subscorer does not contain the docId, a 
smoothing score is
+ * calculated for that document/subscorer combination.
+ */
+public class IndriOrScorer extends IndriDisjunctionScorer {
+
+  protected IndriOrScorer(Weight weight, List<Scorer> subScorers, ScoreMode 
scoreMode, float boost)
+      throws IOException {
+    super(weight, subScorers, scoreMode, boost);
+  }
+
+  @Override
+  public float score(List<Scorer> subScorers) throws IOException {
+    int docId = this.docID();
+    return scoreDoc(subScorers, docId);
+  }
+
+  @Override
+  public float smoothingScore(List<Scorer> subScorers, int docId) throws 
IOException {
+    return scoreDoc(subScorers, docId);
+  }
+
+  private float scoreDoc(List<Scorer> subScorers, int docId) throws 
IOException {
+    float score = 1;
+    for (Scorer scorer : subScorers) {
+      int scorerDocId = scorer.docID();
+      // If the query exists in the document, score the document
+      // Otherwise, compute a smoothing score, which acts like an idf
+      // for subqueries/terms
+      double tempScore = 0;
+      if (docId == scorerDocId) {
+        tempScore = (1 - Math.exp(scorer.score()));
+      } else {
+        tempScore = (1 - Math.exp(scorer.smoothingScore(docId)));
+      }
+      score *= tempScore;

Review comment:
       Curious if this is the bot here trying to say that `score` and 
`tempScore` should be `float` instead? Noting that elsewhere above in the PR 
some locals are `float` rather than `double`.

##########
File path: 
lucene/core/src/java/org/apache/lucene/search/IndriWeightedSumWeight.java
##########
@@ -0,0 +1,135 @@
+/*
+ * 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.lucene.search;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import org.apache.lucene.index.LeafReaderContext;
+
+/** The Weight for IndriAndQuery, used to normalize, score and explain these 
queries. */
+public class IndriWeightedSumWeight extends Weight {
+  private final IndriWeightedSumQuery query;
+
+  private final ArrayList<Weight> weights;
+  private final ScoreMode scoreMode;
+  private final float boost;
+
+  public IndriWeightedSumWeight(
+      IndriWeightedSumQuery query, IndexSearcher searcher, ScoreMode 
scoreMode, float boost)
+      throws IOException {
+    super(query);
+    this.query = query;
+    this.boost = boost;
+    this.scoreMode = scoreMode;
+    weights = new ArrayList<>();
+    // Calculate total boost score so that boost can be normalized
+    float boostSum = 0;
+    for (BooleanClause c : query) {
+      if (c.getQuery() instanceof BoostQuery) {
+        boostSum += ((BoostQuery) c.getQuery()).getBoost();
+      } else {
+        boostSum++;
+      }
+    }
+    for (BooleanClause c : query) {
+      float subBoost = 1.0f;
+      if (c.getQuery() instanceof BoostQuery) {
+        subBoost = ((BoostQuery) c.getQuery()).getBoost() / boostSum;
+      }
+      Weight w = searcher.createWeight(c.getQuery(), scoreMode, subBoost);
+      weights.add(w);
+    }
+  }
+
+  private Scorer getScorer(LeafReaderContext context) throws IOException {
+    List<Scorer> subScorers = new ArrayList<>();
+    for (Weight w : weights) {
+      Scorer scorer = w.scorer(context);
+      if (scorer != null) {
+        subScorers.add(scorer);
+      }
+    }
+    if (subScorers.isEmpty()) {
+      return null;
+    }
+
+    Scorer scorer = subScorers.get(0);
+    if (subScorers.size() > 1) {
+      scorer = new IndriWeightedSumScorer(this, subScorers, scoreMode, boost);
+    }
+    return scorer;
+  }
+
+  @Override
+  public Scorer scorer(LeafReaderContext context) throws IOException {
+    return getScorer(context);
+  }
+
+  @Override
+  public BulkScorer bulkScorer(LeafReaderContext context) throws IOException {
+    Scorer scorer = getScorer(context);
+    if (scorer != null) {
+      BulkScorer bulkScorer = new DefaultBulkScorer(scorer);
+      return bulkScorer;
+    }
+    return null;
+  }
+
+  @Override
+  public boolean isCacheable(LeafReaderContext ctx) {
+    for (Weight w : weights) {
+      if (w.isCacheable(ctx) == false) return false;
+    }
+    return true;
+  }
+
+  @Override
+  public Explanation explain(LeafReaderContext context, int doc) throws 
IOException {

Review comment:
       It seems the three classes for and/or/sum share some common code e.g. in 
`explain` here. Have you considered having a common `IndriWeight` class which 
they can extend to reduce common code repetition?




-- 
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...@lucene.apache.org

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



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

Reply via email to