📄 booleanscorer2.cs
字号:
/*
* 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.
*/
using System;
namespace Lucene.Net.Search
{
/// <summary>An alternative to BooleanScorer.
/// <br>Uses ConjunctionScorer, DisjunctionScorer, ReqOptScorer and ReqExclScorer.
/// <br>Implements skipTo(), and has no limitations on the numbers of added scorers.
/// </summary>
class BooleanScorer2 : Scorer
{
private class AnonymousClassDisjunctionSumScorer : DisjunctionSumScorer
{
private void InitBlock(BooleanScorer2 enclosingInstance)
{
this.enclosingInstance = enclosingInstance;
}
private BooleanScorer2 enclosingInstance;
public BooleanScorer2 Enclosing_Instance
{
get
{
return enclosingInstance;
}
}
internal AnonymousClassDisjunctionSumScorer(BooleanScorer2 enclosingInstance, System.Collections.IList Param1, int Param2):base(Param1, Param2)
{
InitBlock(enclosingInstance);
}
private int lastScoredDoc = - 1;
public override float Score()
{
if (this.Doc() > lastScoredDoc)
{
lastScoredDoc = this.Doc();
Enclosing_Instance.coordinator.nrMatchers += base.nrMatchers;
}
return base.Score();
}
}
private class AnonymousClassConjunctionScorer : ConjunctionScorer
{
private void InitBlock(int requiredNrMatchers, BooleanScorer2 enclosingInstance)
{
this.requiredNrMatchers = requiredNrMatchers;
this.enclosingInstance = enclosingInstance;
}
private int requiredNrMatchers;
private BooleanScorer2 enclosingInstance;
public BooleanScorer2 Enclosing_Instance
{
get
{
return enclosingInstance;
}
}
internal AnonymousClassConjunctionScorer(int requiredNrMatchers, BooleanScorer2 enclosingInstance, Lucene.Net.Search.Similarity Param1):base(Param1)
{
InitBlock(requiredNrMatchers, enclosingInstance);
}
private int lastScoredDoc = - 1;
public override float Score()
{
if (this.Doc() > lastScoredDoc)
{
lastScoredDoc = this.Doc();
Enclosing_Instance.coordinator.nrMatchers += requiredNrMatchers;
}
// All scorers match, so defaultSimilarity super.score() always has 1 as
// the coordination factor.
// Therefore the sum of the scores of the requiredScorers
// is used as score.
return base.Score();
}
}
private System.Collections.ArrayList requiredScorers = new System.Collections.ArrayList();
private System.Collections.ArrayList optionalScorers = new System.Collections.ArrayList();
private System.Collections.ArrayList prohibitedScorers = new System.Collections.ArrayList();
private class Coordinator
{
public Coordinator(BooleanScorer2 enclosingInstance)
{
InitBlock(enclosingInstance);
}
private void InitBlock(BooleanScorer2 enclosingInstance)
{
this.enclosingInstance = enclosingInstance;
}
private BooleanScorer2 enclosingInstance;
public BooleanScorer2 Enclosing_Instance
{
get
{
return enclosingInstance;
}
}
internal int maxCoord = 0; // to be increased for each non prohibited scorer
private float[] coordFactors = null;
internal virtual void Init()
{
// use after all scorers have been added.
coordFactors = new float[maxCoord + 1];
Similarity sim = Enclosing_Instance.GetSimilarity();
for (int i = 0; i <= maxCoord; i++)
{
coordFactors[i] = sim.Coord(i, maxCoord);
}
}
internal int nrMatchers; // to be increased by score() of match counting scorers.
internal virtual void InitDoc()
{
nrMatchers = 0;
}
internal virtual float CoordFactor()
{
return coordFactors[nrMatchers];
}
}
private Coordinator coordinator;
/// <summary>The scorer to which all scoring will be delegated,
/// except for computing and using the coordination factor.
/// </summary>
private Scorer countingSumScorer = null;
/// <summary>The number of optionalScorers that need to match (if there are any) </summary>
private int minNrShouldMatch;
/// <summary>Create a BooleanScorer2.</summary>
/// <param name="similarity">The similarity to be used.
/// </param>
/// <param name="minNrShouldMatch">The minimum number of optional added scorers
/// that should match during the search.
/// In case no required scorers are added,
/// at least one of the optional scorers will have to
/// match during the search.
/// </param>
public BooleanScorer2(Similarity similarity, int minNrShouldMatch) : base(similarity)
{
if (minNrShouldMatch < 0)
{
throw new System.ArgumentException("Minimum number of optional scorers should not be negative");
}
coordinator = new Coordinator(this);
this.minNrShouldMatch = minNrShouldMatch;
}
/// <summary>Create a BooleanScorer2.
/// In no required scorers are added,
/// at least one of the optional scorers will have to match during the search.
/// </summary>
/// <param name="similarity">The similarity to be used.
/// </param>
public BooleanScorer2(Similarity similarity) : this(similarity, 0)
{
}
public virtual void Add(Scorer scorer, bool required, bool prohibited)
{
if (!prohibited)
{
coordinator.maxCoord++;
}
if (required)
{
if (prohibited)
{
throw new System.ArgumentException("scorer cannot be required and prohibited");
}
requiredScorers.Add(scorer);
}
else if (prohibited)
{
prohibitedScorers.Add(scorer);
}
else
{
optionalScorers.Add(scorer);
}
}
/// <summary>Initialize the match counting scorer that sums all the
/// scores. <p>
/// When "counting" is used in a name it means counting the number
/// of matching scorers.<br>
/// When "sum" is used in a name it means score value summing
/// over the matching scorers
/// </summary>
private void InitCountingSumScorer()
{
coordinator.Init();
countingSumScorer = MakeCountingSumScorer();
}
/// <summary>Count a scorer as a single match. </summary>
private class SingleMatchScorer : Scorer
{
private void InitBlock(BooleanScorer2 enclosingInstance)
{
this.enclosingInstance = enclosingInstance;
}
private BooleanScorer2 enclosingInstance;
public BooleanScorer2 Enclosing_Instance
{
get
{
return enclosingInstance;
}
}
private Scorer scorer;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -