Search in sources :

Example 71 with BooleanClause

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

the class FastVectorHighlighterTest method testWithSynonym.

public void testWithSynonym() throws IOException {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
    FieldType type = new FieldType(TextField.TYPE_STORED);
    type.setStoreTermVectorOffsets(true);
    type.setStoreTermVectorPositions(true);
    type.setStoreTermVectors(true);
    type.freeze();
    Document doc = new Document();
    doc.add(new Field("field", "the quick brown fox", type));
    writer.addDocument(doc);
    FastVectorHighlighter highlighter = new FastVectorHighlighter();
    IndexReader reader = DirectoryReader.open(writer);
    int docId = 0;
    // query1: simple synonym query
    SynonymQuery synQuery = new SynonymQuery(new Term("field", "quick"), new Term("field", "fast"));
    FieldQuery fieldQuery = highlighter.getFieldQuery(synQuery, reader);
    String[] bestFragments = highlighter.getBestFragments(fieldQuery, reader, docId, "field", 54, 1);
    assertEquals("the <b>quick</b> brown fox", bestFragments[0]);
    // query2: boolean query with synonym query
    BooleanQuery.Builder bq = new BooleanQuery.Builder().add(new BooleanClause(synQuery, Occur.MUST)).add(new BooleanClause(new TermQuery(new Term("field", "fox")), Occur.MUST));
    fieldQuery = highlighter.getFieldQuery(bq.build(), reader);
    bestFragments = highlighter.getBestFragments(fieldQuery, reader, docId, "field", 54, 1);
    assertEquals("the <b>quick</b> brown <b>fox</b>", bestFragments[0]);
    reader.close();
    writer.close();
    dir.close();
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) FieldType(org.apache.lucene.document.FieldType) BooleanClause(org.apache.lucene.search.BooleanClause) StoredField(org.apache.lucene.document.StoredField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) Directory(org.apache.lucene.store.Directory)

Example 72 with BooleanClause

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

the class SpellChecker method add.

/**
   * Add a clause to a boolean query.
   */
private static void add(BooleanQuery.Builder q, String name, String value, float boost) {
    Query tq = new TermQuery(new Term(name, value));
    q.add(new BooleanClause(new BoostQuery(tq, boost), BooleanClause.Occur.SHOULD));
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 73 with BooleanClause

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

the class QueryUtils method fixNegativeQuery.

/** Fixes a negative query by adding a MatchAllDocs query clause.
   * The query passed in *must* be a negative query.
   */
public static Query fixNegativeQuery(Query q) {
    float boost = 1f;
    if (q instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) q;
        boost = bq.getBoost();
        q = bq.getQuery();
    }
    BooleanQuery bq = (BooleanQuery) q;
    BooleanQuery.Builder newBqB = new BooleanQuery.Builder();
    newBqB.setMinimumNumberShouldMatch(bq.getMinimumNumberShouldMatch());
    for (BooleanClause clause : bq) {
        newBqB.add(clause);
    }
    newBqB.add(new MatchAllDocsQuery(), Occur.MUST);
    BooleanQuery newBq = newBqB.build();
    return new BoostQuery(newBq, boost);
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 74 with BooleanClause

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

the class QueryUtils method isNegative.

/** return true if this query has no positive components */
public static boolean isNegative(Query q) {
    if (!(q instanceof BooleanQuery))
        return false;
    BooleanQuery bq = (BooleanQuery) q;
    Collection<BooleanClause> clauses = bq.clauses();
    if (clauses.size() == 0)
        return false;
    for (BooleanClause clause : clauses) {
        if (!clause.isProhibited())
            return false;
    }
    return true;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery)

Example 75 with BooleanClause

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

the class QueryUtils method getAbs.

/** Returns the original query if it was already a positive query, otherwise
   * return the negative of the query (i.e., a positive query).
   * <p>
   * Example: both id:10 and id:-10 will return id:10
   * <p>
   * The caller can tell the sign of the original by a reference comparison between
   * the original and returned query.
   * @param q Query to create the absolute version of
   * @return Absolute version of the Query
   */
public static Query getAbs(Query q) {
    if (q instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) q;
        Query subQ = bq.getQuery();
        Query absSubQ = getAbs(subQ);
        if (absSubQ == subQ)
            return q;
        return new BoostQuery(absSubQ, bq.getBoost());
    }
    if (q instanceof WrappedQuery) {
        Query subQ = ((WrappedQuery) q).getWrappedQuery();
        Query absSubQ = getAbs(subQ);
        if (absSubQ == subQ)
            return q;
        return new WrappedQuery(absSubQ);
    }
    if (!(q instanceof BooleanQuery))
        return q;
    BooleanQuery bq = (BooleanQuery) q;
    Collection<BooleanClause> clauses = bq.clauses();
    if (clauses.size() == 0)
        return q;
    for (BooleanClause clause : clauses) {
        if (!clause.isProhibited())
            return q;
    }
    if (clauses.size() == 1) {
        // if only one clause, dispense with the wrapping BooleanQuery
        Query negClause = clauses.iterator().next().getQuery();
        // not contribute to a score.
        return negClause;
    } else {
        BooleanQuery.Builder newBqB = new BooleanQuery.Builder();
        // the inverse of -a -b is a OR b
        for (BooleanClause clause : clauses) {
            newBqB.add(clause.getQuery(), BooleanClause.Occur.SHOULD);
        }
        return newBqB.build();
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

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