1 package org.apache.lucene.search; 2 17 18 import java.io.IOException ; 19 20 25 public class ReqOptSumScorer extends Scorer { 26 29 private Scorer reqScorer; 30 private Scorer optScorer; 31 32 36 public ReqOptSumScorer( 37 Scorer reqScorer, 38 Scorer optScorer) 39 { 40 super(null); this.reqScorer = reqScorer; 42 this.optScorer = optScorer; 43 } 44 45 private boolean firstTimeOptScorer = true; 46 47 public boolean next() throws IOException { 48 return reqScorer.next(); 49 } 50 51 public boolean skipTo(int target) throws IOException { 52 return reqScorer.skipTo(target); 53 } 54 55 public int doc() { 56 return reqScorer.doc(); 57 } 58 59 64 public float score() throws IOException { 65 int curDoc = reqScorer.doc(); 66 float reqScore = reqScorer.score(); 67 if (firstTimeOptScorer) { 68 firstTimeOptScorer = false; 69 if (! optScorer.skipTo(curDoc)) { 70 optScorer = null; 71 return reqScore; 72 } 73 } else if (optScorer == null) { 74 return reqScore; 75 } else if ((optScorer.doc() < curDoc) && (! optScorer.skipTo(curDoc))) { 76 optScorer = null; 77 return reqScore; 78 } 79 return (optScorer.doc() == curDoc) 81 ? reqScore + optScorer.score() 82 : reqScore; 83 } 84 85 89 public Explanation explain(int doc) throws IOException { 90 Explanation res = new Explanation(); 91 res.setDescription("required, optional"); 92 res.addDetail(reqScorer.explain(doc)); 93 res.addDetail(optScorer.explain(doc)); 94 return res; 95 } 96 } 97 98 | Popular Tags |