Java Code Examples for org.apache.lucene.search.BooleanClause.Occur#values()

The following examples show how to use org.apache.lucene.search.BooleanClause.Occur#values() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestBooleanRewrites.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private Query randomBooleanQuery() {
  if (random().nextInt(10) == 0) {
    return new BoostQuery(randomBooleanQuery(), TestUtil.nextInt(random(), 1, 10));
  }
  final int numClauses = random().nextInt(5);
  BooleanQuery.Builder b = new BooleanQuery.Builder();
  int numShoulds = 0;
  for (int i = 0; i < numClauses; ++i) {
    final Occur occur = Occur.values()[random().nextInt(Occur.values().length)];
    if (occur == Occur.SHOULD) {
      numShoulds++;
    }
    final Query query = randomQuery();
    b.add(query, occur);
  }
  b.setMinimumNumberShouldMatch(random().nextBoolean() ? 0 : TestUtil.nextInt(random(), 0, numShoulds + 1));
  return b.build();
}
 
Example 2
Source File: TestBoolean2ScorerSupplier.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testDisjunctionCost() throws IOException {
  Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
  for (Occur occur : Occur.values()) {
    subs.put(occur, new ArrayList<>());
  }

  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(42));
  ScorerSupplier s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0);
  assertEquals(42, s.cost());
  assertEquals(42, s.get(random().nextInt(100)).iterator().cost());

  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(12));
  s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0);
  assertEquals(42 + 12, s.cost());
  assertEquals(42 + 12, s.get(random().nextInt(100)).iterator().cost());

  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(20));
  s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0);
  assertEquals(42 + 12 + 20, s.cost());
  assertEquals(42 + 12 + 20, s.get(random().nextInt(100)).iterator().cost());
}
 
Example 3
Source File: TestBoolean2ScorerSupplier.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testProhibitedLeadCost() throws IOException {
  Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
  for (Occur occur : Occur.values()) {
    subs.put(occur, new ArrayList<>());
  }

  // The MUST_NOT clause is called with the same lead cost as the MUST clause
  subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 42));
  subs.get(Occur.MUST_NOT).add(new FakeScorerSupplier(30, 42));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(100); // triggers assertions as a side-effect

  subs.get(Occur.MUST).clear();
  subs.get(Occur.MUST_NOT).clear();
  subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 42));
  subs.get(Occur.MUST_NOT).add(new FakeScorerSupplier(80, 42));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(100); // triggers assertions as a side-effect

  subs.get(Occur.MUST).clear();
  subs.get(Occur.MUST_NOT).clear();
  subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 20));
  subs.get(Occur.MUST_NOT).add(new FakeScorerSupplier(30, 20));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(20); // triggers assertions as a side-effect
}
 
Example 4
Source File: TestBoolean2ScorerSupplier.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testMixedLeadCost() throws IOException {
  Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
  for (Occur occur : Occur.values()) {
    subs.put(occur, new ArrayList<>());
  }

  // The SHOULD clause is always called with the same lead cost as the MUST clause
  subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 42));
  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(30, 42));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, ScoreMode.COMPLETE, 0).get(100); // triggers assertions as a side-effect

  subs.get(Occur.MUST).clear();
  subs.get(Occur.SHOULD).clear();
  subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 42));
  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(80, 42));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, ScoreMode.COMPLETE, 0).get(100); // triggers assertions as a side-effect

  subs.get(Occur.MUST).clear();
  subs.get(Occur.SHOULD).clear();
  subs.get(Occur.MUST).add(new FakeScorerSupplier(42, 20));
  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(80, 20));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, ScoreMode.COMPLETE, 0).get(20); // triggers assertions as a side-effect
}
 
Example 5
Source File: TestBoolean2ScorerSupplier.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testConjunctionCost() {
  Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
  for (Occur occur : Occur.values()) {
    subs.put(occur, new ArrayList<>());
  }

  subs.get(RandomPicks.randomFrom(random(), Arrays.asList(Occur.FILTER, Occur.MUST))).add(new FakeScorerSupplier(42));
  assertEquals(42, new Boolean2ScorerSupplier(null, subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).cost());

  subs.get(RandomPicks.randomFrom(random(), Arrays.asList(Occur.FILTER, Occur.MUST))).add(new FakeScorerSupplier(12));
  assertEquals(12, new Boolean2ScorerSupplier(null, subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).cost());

  subs.get(RandomPicks.randomFrom(random(), Arrays.asList(Occur.FILTER, Occur.MUST))).add(new FakeScorerSupplier(20));
  assertEquals(12, new Boolean2ScorerSupplier(null, subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).cost());
}
 
Example 6
Source File: TestBoolean2ScorerSupplier.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDisjunctionWithMinShouldMatchCost() throws IOException {
  Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
  for (Occur occur : Occur.values()) {
    subs.put(occur, new ArrayList<>());
  }

  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(42));
  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(12));
  ScorerSupplier s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 1);
  assertEquals(42 + 12, s.cost());
  assertEquals(42 + 12, s.get(random().nextInt(100)).iterator().cost());

  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(20));
  s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 1);
  assertEquals(42 + 12 + 20, s.cost());
  assertEquals(42 + 12 + 20, s.get(random().nextInt(100)).iterator().cost());
  s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 2);
  assertEquals(12 + 20, s.cost());
  assertEquals(12 + 20, s.get(random().nextInt(100)).iterator().cost());

  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(30));
  s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 1);
  assertEquals(42 + 12 + 20 + 30, s.cost());
  assertEquals(42 + 12 + 20 + 30, s.get(random().nextInt(100)).iterator().cost());
  s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 2);
  assertEquals(12 + 20 + 30, s.cost());
  assertEquals(12 + 20 + 30, s.get(random().nextInt(100)).iterator().cost());
  s = new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 3);
  assertEquals(12 + 20, s.cost());
  assertEquals(12 + 20, s.get(random().nextInt(100)).iterator().cost());
}
 
Example 7
Source File: TestBoolean2ScorerSupplier.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDisjunctionLeadCost() throws IOException {
  Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
  for (Occur occur : Occur.values()) {
    subs.put(occur, new ArrayList<>());
  }
  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(42, 54));
  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(12, 54));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(100); // triggers assertions as a side-effect

  subs.get(Occur.SHOULD).clear();
  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(42, 20));
  subs.get(Occur.SHOULD).add(new FakeScorerSupplier(12, 20));
  new Boolean2ScorerSupplier(new FakeWeight(), subs, RandomPicks.randomFrom(random(), ScoreMode.values()), 0).get(20); // triggers assertions as a side-effect
}
 
Example 8
Source File: BooleanWeight.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOException {
  int minShouldMatch = query.getMinimumNumberShouldMatch();

  final Map<Occur, Collection<ScorerSupplier>> scorers = new EnumMap<>(Occur.class);
  for (Occur occur : Occur.values()) {
    scorers.put(occur, new ArrayList<>());
  }

  for (WeightedBooleanClause wc : weightedClauses) {
    Weight w = wc.weight;
    BooleanClause c = wc.clause;
    ScorerSupplier subScorer = w.scorerSupplier(context);
    if (subScorer == null) {
      if (c.isRequired()) {
        return null;
      }
    } else {
      scorers.get(c.getOccur()).add(subScorer);
    }
  }

  // scorer simplifications:
  
  if (scorers.get(Occur.SHOULD).size() == minShouldMatch) {
    // any optional clauses are in fact required
    scorers.get(Occur.MUST).addAll(scorers.get(Occur.SHOULD));
    scorers.get(Occur.SHOULD).clear();
    minShouldMatch = 0;
  }
  
  if (scorers.get(Occur.FILTER).isEmpty() && scorers.get(Occur.MUST).isEmpty() && scorers.get(Occur.SHOULD).isEmpty()) {
    // no required and optional clauses.
    return null;
  } else if (scorers.get(Occur.SHOULD).size() < minShouldMatch) {
    // either >1 req scorer, or there are 0 req scorers and at least 1
    // optional scorer. Therefore if there are not enough optional scorers
    // no documents will be matched by the query
    return null;
  }

  return new Boolean2ScorerSupplier(this, scorers, scoreMode, minShouldMatch);
}