Example usage for org.apache.lucene.search BooleanQuery BooleanQuery

List of usage examples for org.apache.lucene.search BooleanQuery BooleanQuery

Introduction

In this page you can find the example usage for org.apache.lucene.search BooleanQuery BooleanQuery.

Prototype

BooleanQuery

Source Link

Usage

From source file:com.peergreen.deployment.repository.internal.maven.MavenRepositoryServiceImpl.java

License:Open Source License

private Query createQuery(String filter) {
    BooleanQuery query = new BooleanQuery();
    String f = wildCardFilter(filter);
    Query q = new WildcardQuery(new Term(ArtifactInfo.GROUP_ID, f));
    query.add(q, BooleanClause.Occur.SHOULD);
    q = new WildcardQuery(new Term(ArtifactInfo.ARTIFACT_ID, f));
    query.add(q, BooleanClause.Occur.SHOULD);
    q = new WildcardQuery(new Term(ArtifactInfo.CLASSIFIER, f));
    query.add(q, BooleanClause.Occur.SHOULD);
    q = new WildcardQuery(new Term(ArtifactInfo.VERSION, f));
    query.add(q, BooleanClause.Occur.SHOULD);
    return query;
}

From source file:com.peergreen.deployment.repository.internal.maven.MavenRepositoryServiceImpl.java

License:Open Source License

protected Query createQuery(com.peergreen.deployment.repository.search.Query[] queries) {
    BooleanQuery query = new BooleanQuery();
    // gets all packaging
    Query q = new WildcardQuery(new Term(ArtifactInfo.PACKAGING, "*"));
    query.add(q, BooleanClause.Occur.SHOULD);

    // Transform Queries
    for (com.peergreen.deployment.repository.search.Query pQuery : queries) {
        if (!(pQuery instanceof RepositoryQuery)) {
            BaseQueryVisitor visitor = new BaseQueryVisitor();
            pQuery.walk(visitor);//  w  ww . ja  v a  2  s .  co  m
            query.add(visitor.getQuery(), BooleanClause.Occur.MUST);
        }
    }

    return query;
}

From source file:com.peergreen.deployment.repository.internal.search.BaseQueryVisitor.java

License:Open Source License

public BaseQueryVisitor() {
    finalQuery = new BooleanQuery();
}

From source file:com.plugtree.solradvert.core.AdvertQueryImpl.java

License:Apache License

@Override
public void boost(String qstr) {
    logger.debug("Adding boost query: " + qstr);
    try {/*w  w w.j  a  va 2 s  .co m*/
        QParser qparser = QParser.getParser(qstr, FunctionQParserPlugin.NAME, rb.req);
        Query qq = qparser.parse();

        BooleanQuery newq = new BooleanQuery();
        newq.add(new BooleanClause(q, Occur.MUST));
        newq.add(new BooleanClause(qq, Occur.SHOULD));

        rb.setQuery(newq);
    } catch (ParseException ex) {
        logger.error("Error while adding boost query: " + ex);
    }
}

From source file:com.pongasoft.kiwidoc.index.impl.keyword.impl.KeywordIndexImpl.java

License:Apache License

/**
 * Generates a simple query: a boolean query made of TermQuery separated
 * by AND./*  w  ww. j a v a2s. co  m*/
 *
 * @param query
 * @return <code>null</code> if there is no terms
 * @throws ParseException
 */
private Query generateSimpleQuery(String keyword, String field) throws ParseException {
    int termCount = 0;
    TokenStream source = _analyzer.tokenStream(field, new StringReader(keyword));

    BooleanQuery q = new BooleanQuery();
    org.apache.lucene.analysis.Token t = new org.apache.lucene.analysis.Token();

    while (true) {
        try {
            t = source.next(t);
        } catch (IOException e) {
            if (log.isDebugEnabled())
                log.debug("ingnored exception", e);

            t = null;
        }

        if (t == null)
            break;

        termCount++;
        q.add(new TermQuery(new Term(field, t.term())), BooleanClause.Occur.MUST);
    }
    try {
        source.close();
    } catch (IOException e) {
        if (log.isDebugEnabled())
            log.debug("ingnored exception", e);
    }

    if (termCount == 0) {
        return null;
    }

    BooleanClause[] clauses = q.getClauses();

    if (clauses != null && clauses.length == 1)
        return clauses[0].getQuery();

    return q;
}

From source file:com.querydsl.lucene3.LuceneSerializer.java

License:Apache License

private Query toQuery(Operation<?> operation, QueryMetadata metadata) {
    Operator op = operation.getOperator();
    if (op == Ops.OR) {
        return toTwoHandSidedQuery(operation, Occur.SHOULD, metadata);
    } else if (op == Ops.AND) {
        return toTwoHandSidedQuery(operation, Occur.MUST, metadata);
    } else if (op == Ops.NOT) {
        BooleanQuery bq = new BooleanQuery();
        bq.add(new BooleanClause(toQuery(operation.getArg(0), metadata), Occur.MUST_NOT));
        bq.add(new BooleanClause(new MatchAllDocsQuery(), Occur.MUST));
        return bq;
    } else if (op == Ops.LIKE) {
        return like(operation, metadata);
    } else if (op == Ops.EQ) {
        return eq(operation, metadata, false);
    } else if (op == Ops.EQ_IGNORE_CASE) {
        throw new IgnoreCaseUnsupportedException();
    } else if (op == Ops.NE) {
        return ne(operation, metadata, false);
    } else if (op == Ops.STARTS_WITH) {
        return startsWith(metadata, operation, false);
    } else if (op == Ops.STARTS_WITH_IC) {
        throw new IgnoreCaseUnsupportedException();
    } else if (op == Ops.ENDS_WITH) {
        return endsWith(operation, metadata, false);
    } else if (op == Ops.ENDS_WITH_IC) {
        throw new IgnoreCaseUnsupportedException();
    } else if (op == Ops.STRING_CONTAINS) {
        return stringContains(operation, metadata, false);
    } else if (op == Ops.STRING_CONTAINS_IC) {
        throw new IgnoreCaseUnsupportedException();
    } else if (op == Ops.BETWEEN) {
        return between(operation, metadata);
    } else if (op == Ops.IN) {
        return in(operation, metadata, false);
    } else if (op == Ops.NOT_IN) {
        return notIn(operation, metadata, false);
    } else if (op == Ops.LT) {
        return lt(operation, metadata);
    } else if (op == Ops.GT) {
        return gt(operation, metadata);
    } else if (op == Ops.LOE) {
        return le(operation, metadata);
    } else if (op == Ops.GOE) {
        return ge(operation, metadata);
    } else if (op == LuceneOps.LUCENE_QUERY) {
        @SuppressWarnings("unchecked") //This is the expected type
        Query rv = ((Constant<Query>) operation.getArg(0)).getConstant();
        return rv;
    }/*from   w  w  w . jav a 2  s. c  o  m*/
    throw new UnsupportedOperationException("Illegal operation " + operation);
}

From source file:com.querydsl.lucene3.LuceneSerializer.java

License:Apache License

protected Query in(Operation<?> operation, QueryMetadata metadata, boolean ignoreCase) {
    Path<?> path = getPath(operation.getArg(0));
    String field = toField(path);
    @SuppressWarnings("unchecked") //This is the second argument type
    Constant<Collection<?>> collConstant = (Constant<Collection<?>>) operation.getArg(1);
    Collection<?> values = collConstant.getConstant();
    BooleanQuery bq = new BooleanQuery();
    if (Number.class.isAssignableFrom(path.getType())) {
        for (Object value : values) {
            TermQuery eq = new TermQuery(new Term(field, convertNumber((Number) value)));
            bq.add(eq, Occur.SHOULD);/*from  w  ww.ja  v  a  2  s .com*/
        }
    } else {
        for (Object value : values) {
            String[] str = convert(path, value);
            bq.add(eq(field, str, ignoreCase), Occur.SHOULD);
        }
    }
    return bq;
}

From source file:com.querydsl.lucene3.LuceneSerializer.java

License:Apache License

protected Query notIn(Operation<?> operation, QueryMetadata metadata, boolean ignoreCase) {
    BooleanQuery bq = new BooleanQuery();
    bq.add(new BooleanClause(in(operation, metadata, false), Occur.MUST_NOT));
    bq.add(new BooleanClause(new MatchAllDocsQuery(), Occur.MUST));
    return bq;//from   w ww  .j  ava2s .co m
}

From source file:com.querydsl.lucene4.LuceneSerializer.java

License:Apache License

private Query toQuery(Operation<?> operation, QueryMetadata metadata) {
    Operator op = operation.getOperator();
    if (op == Ops.OR) {
        return toTwoHandSidedQuery(operation, Occur.SHOULD, metadata);
    } else if (op == Ops.AND) {
        return toTwoHandSidedQuery(operation, Occur.MUST, metadata);
    } else if (op == Ops.NOT) {
        BooleanQuery bq = new BooleanQuery();
        bq.add(new BooleanClause(toQuery(operation.getArg(0), metadata), Occur.MUST_NOT));
        bq.add(new BooleanClause(new MatchAllDocsQuery(), Occur.MUST));
        return bq;
    } else if (op == Ops.LIKE) {
        return like(operation, metadata);
    } else if (op == Ops.EQ) {
        return eq(operation, metadata, false);
    } else if (op == Ops.EQ_IGNORE_CASE) {
        throw new IgnoreCaseUnsupportedException();
    } else if (op == Ops.NE) {
        return ne(operation, metadata, false);
    } else if (op == Ops.STARTS_WITH) {
        return startsWith(metadata, operation, false);
    } else if (op == Ops.STARTS_WITH_IC) {
        throw new IgnoreCaseUnsupportedException();
    } else if (op == Ops.ENDS_WITH) {
        return endsWith(operation, metadata, false);
    } else if (op == Ops.ENDS_WITH_IC) {
        throw new IgnoreCaseUnsupportedException();
    } else if (op == Ops.STRING_CONTAINS) {
        return stringContains(operation, metadata, false);
    } else if (op == Ops.STRING_CONTAINS_IC) {
        throw new IgnoreCaseUnsupportedException();
    } else if (op == Ops.BETWEEN) {
        return between(operation, metadata);
    } else if (op == Ops.IN) {
        return in(operation, metadata, false);
    } else if (op == Ops.NOT_IN) {
        return notIn(operation, metadata, false);
    } else if (op == Ops.LT) {
        return lt(operation, metadata);
    } else if (op == Ops.GT) {
        return gt(operation, metadata);
    } else if (op == Ops.LOE) {
        return le(operation, metadata);
    } else if (op == Ops.GOE) {
        return ge(operation, metadata);
    } else if (op == LuceneOps.LUCENE_QUERY) {
        @SuppressWarnings("unchecked") //this is the expected type
        Constant<Query> expectedConstant = (Constant<Query>) operation.getArg(0);
        return expectedConstant.getConstant();
    }/*www.  j  a  va  2s  .  c o m*/
    throw new UnsupportedOperationException("Illegal operation " + operation);
}

From source file:com.querydsl.lucene4.LuceneSerializer.java

License:Apache License

protected Query in(Operation<?> operation, QueryMetadata metadata, boolean ignoreCase) {
    Path<?> path = getPath(operation.getArg(0));
    String field = toField(path);
    @SuppressWarnings("unchecked") //this is the expected type
    Constant<Collection<?>> expectedConstant = (Constant<Collection<?>>) operation.getArg(1);
    Collection<?> values = expectedConstant.getConstant();
    BooleanQuery bq = new BooleanQuery();
    if (Number.class.isAssignableFrom(path.getType())) {
        for (Object value : values) {
            TermQuery eq = new TermQuery(new Term(field, convertNumber((Number) value)));
            bq.add(eq, Occur.SHOULD);//from  www.ja  v  a 2  s .  co  m
        }
    } else {
        for (Object value : values) {
            String[] str = convert(path, value);
            bq.add(eq(field, str, ignoreCase), Occur.SHOULD);
        }
    }
    return bq;
}