Search in sources :

Example 1 with BooleanClause

use of org.opennms.newts.api.search.BooleanClause in project newts by OpenNMS.

the class CassandraSearcher method searchForIds.

/**
 * Returns the set of resource ids that match the given
 * boolean query.
 *
 * Separate clauses are performed with separate database queries and their
 * results are joined in memory.
 */
private Set<String> searchForIds(Context context, BooleanQuery query, ConsistencyLevel readConsistency) {
    Set<String> ids = Sets.newTreeSet();
    for (BooleanClause clause : query.getClauses()) {
        Set<String> subQueryIds;
        Query subQuery = clause.getQuery();
        if (subQuery instanceof BooleanQuery) {
            subQueryIds = searchForIds(context, (BooleanQuery) subQuery, readConsistency);
        } else if (subQuery instanceof TermQuery) {
            subQueryIds = searchForIds(context, (TermQuery) subQuery, readConsistency);
        } else {
            throw new IllegalStateException("Unsupported query: " + subQuery);
        }
        switch(clause.getOperator()) {
            case // Intersect
            AND:
                ids.retainAll(subQueryIds);
                break;
            case // Union
            OR:
                ids.addAll(subQueryIds);
                break;
            default:
                throw new IllegalStateException("Unsupported operator: " + clause.getOperator());
        }
    }
    return ids;
}
Also used : BooleanClause(org.opennms.newts.api.search.BooleanClause) BooleanQuery(org.opennms.newts.api.search.BooleanQuery) TermQuery(org.opennms.newts.api.search.TermQuery) TermQuery(org.opennms.newts.api.search.TermQuery) BooleanQuery(org.opennms.newts.api.search.BooleanQuery) Query(org.opennms.newts.api.search.Query)

Aggregations

BooleanClause (org.opennms.newts.api.search.BooleanClause)1 BooleanQuery (org.opennms.newts.api.search.BooleanQuery)1 Query (org.opennms.newts.api.search.Query)1 TermQuery (org.opennms.newts.api.search.TermQuery)1