Search in sources :

Example 36 with BooleanClause

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

the class LatLonPoint method newBoxQuery.

// static methods for generating queries
/**
   * Create a query for matching a bounding box.
   * <p>
   * The box may cross over the dateline.
   * @param field field name. must not be null.
   * @param minLatitude latitude lower bound: must be within standard +/-90 coordinate bounds.
   * @param maxLatitude latitude upper bound: must be within standard +/-90 coordinate bounds.
   * @param minLongitude longitude lower bound: must be within standard +/-180 coordinate bounds.
   * @param maxLongitude longitude upper bound: must be within standard +/-180 coordinate bounds.
   * @return query matching points within this box
   * @throws IllegalArgumentException if {@code field} is null, or the box has invalid coordinates.
   */
public static Query newBoxQuery(String field, double minLatitude, double maxLatitude, double minLongitude, double maxLongitude) {
    // and should not drag in extra bogus junk! TODO: should encodeCeil just throw ArithmeticException to be less trappy here?
    if (minLatitude == 90.0) {
        // range cannot match as 90.0 can never exist
        return new MatchNoDocsQuery("LatLonPoint.newBoxQuery with minLatitude=90.0");
    }
    if (minLongitude == 180.0) {
        if (maxLongitude == 180.0) {
            // range cannot match as 180.0 can never exist
            return new MatchNoDocsQuery("LatLonPoint.newBoxQuery with minLongitude=maxLongitude=180.0");
        } else if (maxLongitude < minLongitude) {
            // encodeCeil() with dateline wrapping!
            minLongitude = -180.0;
        }
    }
    byte[] lower = encodeCeil(minLatitude, minLongitude);
    byte[] upper = encode(maxLatitude, maxLongitude);
    // Crosses date line: we just rewrite into OR of two bboxes, with longitude as an open range:
    if (maxLongitude < minLongitude) {
        // Disable coord here because a multi-valued doc could match both rects and get unfairly boosted:
        BooleanQuery.Builder q = new BooleanQuery.Builder();
        // E.g.: maxLon = -179, minLon = 179
        byte[] leftOpen = lower.clone();
        // leave longitude open
        NumericUtils.intToSortableBytes(Integer.MIN_VALUE, leftOpen, Integer.BYTES);
        Query left = newBoxInternal(field, leftOpen, upper);
        q.add(new BooleanClause(left, BooleanClause.Occur.SHOULD));
        byte[] rightOpen = upper.clone();
        // leave longitude open
        NumericUtils.intToSortableBytes(Integer.MAX_VALUE, rightOpen, Integer.BYTES);
        Query right = newBoxInternal(field, lower, rightOpen);
        q.add(new BooleanClause(right, BooleanClause.Occur.SHOULD));
        return new ConstantScoreQuery(q.build());
    } else {
        return newBoxInternal(field, lower, upper);
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery)

Example 37 with BooleanClause

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

the class PayloadSpanUtil method queryToSpanQuery.

private void queryToSpanQuery(Query query, Collection<byte[]> payloads) throws IOException {
    if (query instanceof BooleanQuery) {
        for (BooleanClause clause : (BooleanQuery) query) {
            if (!clause.isProhibited()) {
                queryToSpanQuery(clause.getQuery(), payloads);
            }
        }
    } else if (query instanceof PhraseQuery) {
        Term[] phraseQueryTerms = ((PhraseQuery) query).getTerms();
        SpanQuery[] clauses = new SpanQuery[phraseQueryTerms.length];
        for (int i = 0; i < phraseQueryTerms.length; i++) {
            clauses[i] = new SpanTermQuery(phraseQueryTerms[i]);
        }
        int slop = ((PhraseQuery) query).getSlop();
        boolean inorder = false;
        if (slop == 0) {
            inorder = true;
        }
        SpanNearQuery sp = new SpanNearQuery(clauses, slop, inorder);
        getPayloads(payloads, sp);
    } else if (query instanceof TermQuery) {
        SpanTermQuery stq = new SpanTermQuery(((TermQuery) query).getTerm());
        getPayloads(payloads, stq);
    } else if (query instanceof SpanQuery) {
        getPayloads(payloads, (SpanQuery) query);
    } else if (query instanceof DisjunctionMaxQuery) {
        for (Iterator<Query> iterator = ((DisjunctionMaxQuery) query).iterator(); iterator.hasNext(); ) {
            queryToSpanQuery(iterator.next(), payloads);
        }
    } else if (query instanceof MultiPhraseQuery) {
        final MultiPhraseQuery mpq = (MultiPhraseQuery) query;
        final Term[][] termArrays = mpq.getTermArrays();
        final int[] positions = mpq.getPositions();
        if (positions.length > 0) {
            int maxPosition = positions[positions.length - 1];
            for (int i = 0; i < positions.length - 1; ++i) {
                if (positions[i] > maxPosition) {
                    maxPosition = positions[i];
                }
            }
            @SuppressWarnings({ "rawtypes", "unchecked" }) final List<Query>[] disjunctLists = new List[maxPosition + 1];
            int distinctPositions = 0;
            for (int i = 0; i < termArrays.length; ++i) {
                final Term[] termArray = termArrays[i];
                List<Query> disjuncts = disjunctLists[positions[i]];
                if (disjuncts == null) {
                    disjuncts = (disjunctLists[positions[i]] = new ArrayList<>(termArray.length));
                    ++distinctPositions;
                }
                for (final Term term : termArray) {
                    disjuncts.add(new SpanTermQuery(term));
                }
            }
            int positionGaps = 0;
            int position = 0;
            final SpanQuery[] clauses = new SpanQuery[distinctPositions];
            for (int i = 0; i < disjunctLists.length; ++i) {
                List<Query> disjuncts = disjunctLists[i];
                if (disjuncts != null) {
                    clauses[position++] = new SpanOrQuery(disjuncts.toArray(new SpanQuery[disjuncts.size()]));
                } else {
                    ++positionGaps;
                }
            }
            final int slop = mpq.getSlop();
            final boolean inorder = (slop == 0);
            SpanNearQuery sp = new SpanNearQuery(clauses, slop + positionGaps, inorder);
            getPayloads(payloads, sp);
        }
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) Query(org.apache.lucene.search.Query) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) BooleanClause(org.apache.lucene.search.BooleanClause) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ArrayList(java.util.ArrayList) List(java.util.List) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 38 with BooleanClause

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

the class SolrQueryParserBase method addClause.

protected void addClause(List<BooleanClause> clauses, int conj, int mods, Query q) {
    boolean required, prohibited;
    // unless it's already prohibited
    if (clauses.size() > 0 && conj == CONJ_AND) {
        BooleanClause c = clauses.get(clauses.size() - 1);
        if (!c.isProhibited())
            clauses.set(clauses.size() - 1, new BooleanClause(c.getQuery(), BooleanClause.Occur.MUST));
    }
    if (clauses.size() > 0 && operator == AND_OPERATOR && conj == CONJ_OR) {
        // If this term is introduced by OR, make the preceding term optional,
        // unless it's prohibited (that means we leave -a OR b but +a OR b-->a OR b)
        // notice if the input is a OR b, first term is parsed as required; without
        // this modification a OR b would parsed as +a OR b
        BooleanClause c = clauses.get(clauses.size() - 1);
        if (!c.isProhibited())
            clauses.set(clauses.size() - 1, new BooleanClause(c.getQuery(), BooleanClause.Occur.SHOULD));
    }
    // filtered away by the analyzer.
    if (q == null)
        return;
    if (operator == OR_OPERATOR) {
        // We set REQUIRED if we're introduced by AND or +; PROHIBITED if
        // introduced by NOT or -; make sure not to set both.
        prohibited = (mods == MOD_NOT);
        required = (mods == MOD_REQ);
        if (conj == CONJ_AND && !prohibited) {
            required = true;
        }
    } else {
        // We set PROHIBITED if we're introduced by NOT or -; We set REQUIRED
        // if not PROHIBITED and not introduced by OR
        prohibited = (mods == MOD_NOT);
        required = (!prohibited && conj != CONJ_OR);
    }
    if (required && !prohibited)
        clauses.add(newBooleanClause(q, BooleanClause.Occur.MUST));
    else if (!required && !prohibited)
        clauses.add(newBooleanClause(q, BooleanClause.Occur.SHOULD));
    else if (!required && prohibited)
        clauses.add(newBooleanClause(q, BooleanClause.Occur.MUST_NOT));
    else
        throw new RuntimeException("Clause cannot be both required and prohibited");
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause)

Example 39 with BooleanClause

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

the class DirectUpdateHandler2 method doNormalUpdate.

private void doNormalUpdate(AddUpdateCommand cmd) throws IOException {
    Term updateTerm;
    Term idTerm = getIdTerm(cmd);
    boolean del = false;
    if (cmd.updateTerm == null) {
        updateTerm = idTerm;
    } else {
        // this is only used by the dedup update processor
        del = true;
        updateTerm = cmd.updateTerm;
    }
    RefCounted<IndexWriter> iw = solrCoreState.getIndexWriter(core);
    try {
        IndexWriter writer = iw.get();
        updateDocOrDocValues(cmd, writer, updateTerm);
        if (del) {
            // ensure id remains unique
            BooleanQuery.Builder bq = new BooleanQuery.Builder();
            bq.add(new BooleanClause(new TermQuery(updateTerm), Occur.MUST_NOT));
            bq.add(new BooleanClause(new TermQuery(idTerm), Occur.MUST));
            writer.deleteDocuments(new DeleteByQueryWrapper(bq.build(), core.getLatestSchema()));
        }
        // log version was definitely committed.
        if (ulog != null)
            ulog.add(cmd);
    } finally {
        iw.decref();
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) IndexWriter(org.apache.lucene.index.IndexWriter) Term(org.apache.lucene.index.Term)

Example 40 with BooleanClause

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

the class TestExtendableQueryParser method testExtFieldUnqoted.

public void testExtFieldUnqoted() throws Exception {
    for (int i = 0; i < DELIMITERS.length; i++) {
        Extensions ext = newExtensions(DELIMITERS[i]);
        ext.add("testExt", new ExtensionStub());
        ExtendableQueryParser parser = (ExtendableQueryParser) getParser(null, ext);
        String field = ext.buildExtensionField("testExt", "aField");
        Query query = parser.parse(String.format(Locale.ROOT, "%s:foo bar", field));
        assertTrue("expected instance of BooleanQuery but was " + query.getClass(), query instanceof BooleanQuery);
        BooleanQuery bquery = (BooleanQuery) query;
        BooleanClause[] clauses = bquery.clauses().toArray(new BooleanClause[0]);
        assertEquals(2, clauses.length);
        BooleanClause booleanClause = clauses[0];
        query = booleanClause.getQuery();
        assertTrue("expected instance of TermQuery but was " + query.getClass(), query instanceof TermQuery);
        TermQuery tquery = (TermQuery) query;
        assertEquals("aField", tquery.getTerm().field());
        assertEquals("foo", tquery.getTerm().text());
        booleanClause = clauses[1];
        query = booleanClause.getQuery();
        assertTrue("expected instance of TermQuery but was " + query.getClass(), query instanceof TermQuery);
        tquery = (TermQuery) query;
        assertEquals(getDefaultField(), tquery.getTerm().field());
        assertEquals("bar", tquery.getTerm().text());
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery)

Aggregations

BooleanClause (org.apache.lucene.search.BooleanClause)102 BooleanQuery (org.apache.lucene.search.BooleanQuery)92 Query (org.apache.lucene.search.Query)56 TermQuery (org.apache.lucene.search.TermQuery)46 Term (org.apache.lucene.index.Term)44 BoostQuery (org.apache.lucene.search.BoostQuery)33 ArrayList (java.util.ArrayList)23 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)21 PhraseQuery (org.apache.lucene.search.PhraseQuery)20 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)19 SynonymQuery (org.apache.lucene.search.SynonymQuery)18 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)18 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)17 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)16 SpanQuery (org.apache.lucene.search.spans.SpanQuery)15 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)14 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)14 WildcardQuery (org.apache.lucene.search.WildcardQuery)14 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)13 PrefixQuery (org.apache.lucene.search.PrefixQuery)13