Search in sources :

Example 1 with Occur

use of org.apache.lucene.search.BooleanClause.Occur in project elasticsearch by elastic.

the class CommonTermsQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    String field;
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType != null) {
        field = fieldType.name();
    } else {
        field = fieldName;
    }
    Analyzer analyzerObj;
    if (analyzer == null) {
        if (fieldType != null) {
            analyzerObj = context.getSearchAnalyzer(fieldType);
        } else {
            analyzerObj = context.getMapperService().searchAnalyzer();
        }
    } else {
        analyzerObj = context.getMapperService().getIndexAnalyzers().get(analyzer);
        if (analyzerObj == null) {
            throw new QueryShardException(context, "[common] analyzer [" + analyzer + "] not found");
        }
    }
    Occur highFreqOccur = highFreqOperator.toBooleanClauseOccur();
    Occur lowFreqOccur = lowFreqOperator.toBooleanClauseOccur();
    ExtendedCommonTermsQuery commonsQuery = new ExtendedCommonTermsQuery(highFreqOccur, lowFreqOccur, cutoffFrequency, disableCoord, fieldType);
    return parseQueryString(commonsQuery, text, field, analyzerObj, lowFreqMinimumShouldMatch, highFreqMinimumShouldMatch);
}
Also used : ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Occur(org.apache.lucene.search.BooleanClause.Occur) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 2 with Occur

use of org.apache.lucene.search.BooleanClause.Occur in project lucene-solr by apache.

the class BooleanWeight method scorerSupplier.

@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<>());
    }
    Iterator<BooleanClause> cIter = query.iterator();
    for (Weight w : weights) {
        BooleanClause c = cIter.next();
        ScorerSupplier subScorer = w.scorerSupplier(context);
        if (subScorer == null) {
            if (c.isRequired()) {
                return null;
            }
        } else {
            scorers.get(c.getOccur()).add(subScorer);
        }
    }
    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) {
        // no documents will be matched by the query
        return null;
    }
    // we don't need scores, so if we have required clauses, drop optional clauses completely
    if (!needsScores && minShouldMatch == 0 && scorers.get(Occur.MUST).size() + scorers.get(Occur.FILTER).size() > 0) {
        scorers.get(Occur.SHOULD).clear();
    }
    return new Boolean2ScorerSupplier(this, scorers, needsScores, minShouldMatch);
}
Also used : Collection(java.util.Collection) Occur(org.apache.lucene.search.BooleanClause.Occur) EnumMap(java.util.EnumMap)

Example 3 with Occur

use of org.apache.lucene.search.BooleanClause.Occur in project lucene-solr by apache.

the class TestBoolean2ScorerSupplier method testConjunctionCost.

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, random().nextBoolean(), 0).cost());
    subs.get(RandomPicks.randomFrom(random(), Arrays.asList(Occur.FILTER, Occur.MUST))).add(new FakeScorerSupplier(12));
    assertEquals(12, new Boolean2ScorerSupplier(null, subs, random().nextBoolean(), 0).cost());
    subs.get(RandomPicks.randomFrom(random(), Arrays.asList(Occur.FILTER, Occur.MUST))).add(new FakeScorerSupplier(20));
    assertEquals(12, new Boolean2ScorerSupplier(null, subs, random().nextBoolean(), 0).cost());
}
Also used : Collection(java.util.Collection) Occur(org.apache.lucene.search.BooleanClause.Occur) EnumMap(java.util.EnumMap)

Example 4 with Occur

use of org.apache.lucene.search.BooleanClause.Occur in project lucene-solr by apache.

the class TestBoolean2ScorerSupplier method testMixedRandomAccess.

public void testMixedRandomAccess() throws IOException {
    for (boolean randomAccess : new boolean[] { false, true }) {
        Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
        for (Occur occur : Occur.values()) {
            subs.put(occur, new ArrayList<>());
        }
        // The SHOULD clause always uses random-access if there is a MUST clause
        subs.get(Occur.MUST).add(new FakeScorerSupplier(42, randomAccess));
        subs.get(Occur.SHOULD).add(new FakeScorerSupplier(TestUtil.nextInt(random(), 1, 100), true));
        // triggers assertions as a side-effect
        new Boolean2ScorerSupplier(null, subs, true, 0).get(randomAccess);
    }
}
Also used : Collection(java.util.Collection) Occur(org.apache.lucene.search.BooleanClause.Occur) EnumMap(java.util.EnumMap)

Example 5 with Occur

use of org.apache.lucene.search.BooleanClause.Occur in project lucene-solr by apache.

the class TestBoolean2ScorerSupplier method testDisjunctionRandomAccess.

public void testDisjunctionRandomAccess() throws IOException {
    // disjunctions propagate
    for (boolean randomAccess : new boolean[] { false, true }) {
        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, randomAccess));
        subs.get(Occur.SHOULD).add(new FakeScorerSupplier(12, randomAccess));
        // triggers assertions as a side-effect
        new Boolean2ScorerSupplier(null, subs, random().nextBoolean(), 0).get(randomAccess);
    }
}
Also used : Collection(java.util.Collection) Occur(org.apache.lucene.search.BooleanClause.Occur) EnumMap(java.util.EnumMap)

Aggregations

Occur (org.apache.lucene.search.BooleanClause.Occur)21 Collection (java.util.Collection)10 EnumMap (java.util.EnumMap)10 Term (org.apache.lucene.index.Term)5 Query (org.apache.lucene.search.Query)5 ArrayList (java.util.ArrayList)4 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)3 TermQuery (org.apache.lucene.search.TermQuery)3 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)2 BooleanClause (org.apache.lucene.search.BooleanClause)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 Directory (org.apache.lucene.store.Directory)2 StringReader (java.io.StringReader)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 Matcher (java.util.regex.Matcher)1 Analyzer (org.apache.lucene.analysis.Analyzer)1