Search in sources :

Example 1 with BooleanQuery

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

the class NewtsResourceStorageDaoTest method replay.

private void replay() {
    EasyMock.expect(m_searcher.search(EasyMock.eq(m_context), EasyMock.anyObject(), EasyMock.anyBoolean())).andAnswer(new IAnswer<SearchResults>() {

        public SearchResults answer() throws Throwable {
            // Assume there is a single term query
            Query q = (Query) EasyMock.getCurrentArguments()[1];
            BooleanQuery bq = (BooleanQuery) q;
            TermQuery tq = (TermQuery) bq.getClauses().get(0).getQuery();
            String field = tq.getTerm().getField("");
            String value = tq.getTerm().getValue();
            SearchResults searchResults = new SearchResults();
            for (Entry<ResourcePath, Set<String>> entry : m_indexedPaths.entrySet()) {
                Map<String, String> attributes = Maps.newHashMap();
                // Build the indexed attributes and attempt to match them against the given query
                NewtsUtils.addIndicesToAttributes(entry.getKey(), attributes);
                if (value.equals(attributes.get(field))) {
                    searchResults.addResult(new Resource(NewtsUtils.toResourceId(entry.getKey())), entry.getValue());
                }
            }
            return searchResults;
        }
    }).atLeastOnce();
    EasyMock.expect(m_searcher.getResourceAttributes(EasyMock.eq(m_context), EasyMock.anyObject())).andReturn(Maps.newHashMap()).anyTimes();
    EasyMock.replay(m_searcher);
}
Also used : IAnswer(org.easymock.IAnswer) BooleanQuery(org.opennms.newts.api.search.BooleanQuery) TermQuery(org.opennms.newts.api.search.TermQuery) ResourcePath(org.opennms.netmgt.model.ResourcePath) Set(java.util.Set) TermQuery(org.opennms.newts.api.search.TermQuery) BooleanQuery(org.opennms.newts.api.search.BooleanQuery) Query(org.opennms.newts.api.search.Query) Resource(org.opennms.newts.api.Resource) SearchResults(org.opennms.newts.api.search.SearchResults)

Example 2 with BooleanQuery

use of org.opennms.newts.api.search.BooleanQuery 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)

Example 3 with BooleanQuery

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

the class NewtsUtils method findResourcesWithMetricsAtDepth.

/**
 * Constructs a query used to find all of the resources that have
 * one or more metrics at the given depth bellow the path.
 *
 * Requires resources to have been indexed using {@link #addIndicesToAttributes}.
 */
public static Query findResourcesWithMetricsAtDepth(ResourcePath path, int depth) {
    // Numeric suffix for the index name, based on the length of parent path
    int idxSuffix = path.elements().length - 1;
    // The length of the resource ids we're interested in finding
    int targetLen = idxSuffix + depth + 2;
    TermQuery tq = new TermQuery(new Term(// key
    "_idx" + idxSuffix, // value
    String.format("(%s,%d)", toResourceId(path), targetLen)));
    BooleanQuery q = new BooleanQuery();
    q.add(tq, Operator.OR);
    return q;
}
Also used : TermQuery(org.opennms.newts.api.search.TermQuery) BooleanQuery(org.opennms.newts.api.search.BooleanQuery) Term(org.opennms.newts.api.search.Term)

Example 4 with BooleanQuery

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

the class QueryParserTest method canParseGroupedQueries.

@Test
public void canParseGroupedQueries() throws ParseException {
    BooleanQuery subQuery1 = new BooleanQuery();
    subQuery1.add(new TermQuery(new Term("meat", "beef")), Operator.OR);
    subQuery1.add(new TermQuery(new Term("music", "rock")), Operator.OR);
    BooleanQuery subQuery2 = new BooleanQuery();
    subQuery2.add(new TermQuery(new Term("meat", "chicken")), Operator.OR);
    subQuery2.add(new TermQuery(new Term("music", "country")), Operator.AND);
    TermQuery subQuery3 = new TermQuery(new Term("sauce"));
    BooleanQuery query = new BooleanQuery();
    query.add(subQuery1, Operator.OR);
    query.add(subQuery2, Operator.OR);
    query.add(subQuery3, Operator.AND);
    assertThat(parse(query), equalTo((Query) query));
    assertThat(parse("(meat:beef OR music:rock) OR (meat:chicken AND music:country) AND sauce"), equalTo((Query) query));
}
Also used : BooleanQuery(org.opennms.newts.api.search.BooleanQuery) TermQuery(org.opennms.newts.api.search.TermQuery) Query(org.opennms.newts.api.search.Query) TermQuery(org.opennms.newts.api.search.TermQuery) BooleanQuery(org.opennms.newts.api.search.BooleanQuery) Term(org.opennms.newts.api.search.Term) Test(org.junit.Test)

Example 5 with BooleanQuery

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

the class QueryParserTest method canParseCompoundQuerys.

@Test
public void canParseCompoundQuerys() throws ParseException {
    BooleanQuery query = new BooleanQuery();
    query.add(new TermQuery(new Term("meat", "beef")), Operator.OR);
    query.add(new TermQuery(new Term("music", "rock")), Operator.OR);
    assertThat(parse(query), equalTo((Query) query));
    assertThat(parse("meat:beef music:rock"), equalTo((Query) query));
    assertThat(parse("meat:beef OR music:rock"), equalTo((Query) query));
    assertThat(parse("meat:beef || music:rock"), equalTo((Query) query));
    query = new BooleanQuery();
    query.add(new TermQuery(new Term("meat", "beef")), Operator.OR);
    query.add(new TermQuery(new Term("music", "rock")), Operator.AND);
    assertThat(parse(query), equalTo((Query) query));
    assertThat(parse("meat:beef AND music:rock"), equalTo((Query) query));
    assertThat(parse("meat:beef && music:rock"), equalTo((Query) query));
    query = new BooleanQuery();
    query.add(new TermQuery(new Term("meat", "beef")), Operator.OR);
    query.add(new TermQuery(new Term("music", "rock")), Operator.AND);
    query.add(new TermQuery(new Term("sauce")), Operator.OR);
    assertThat(parse(query), equalTo((Query) query));
    assertThat(parse("meat:beef AND music:rock OR sauce"), equalTo((Query) query));
    assertThat(parse("meat:beef && music:rock || sauce"), equalTo((Query) query));
}
Also used : BooleanQuery(org.opennms.newts.api.search.BooleanQuery) TermQuery(org.opennms.newts.api.search.TermQuery) Query(org.opennms.newts.api.search.Query) TermQuery(org.opennms.newts.api.search.TermQuery) BooleanQuery(org.opennms.newts.api.search.BooleanQuery) Term(org.opennms.newts.api.search.Term) Test(org.junit.Test)

Aggregations

BooleanQuery (org.opennms.newts.api.search.BooleanQuery)7 TermQuery (org.opennms.newts.api.search.TermQuery)7 Query (org.opennms.newts.api.search.Query)5 Term (org.opennms.newts.api.search.Term)4 Test (org.junit.Test)3 Resource (org.opennms.newts.api.Resource)3 SearchResults (org.opennms.newts.api.search.SearchResults)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 Timer (com.codahale.metrics.Timer)1 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)1 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 IAnswer (org.easymock.IAnswer)1 ResourcePath (org.opennms.netmgt.model.ResourcePath)1 Context (org.opennms.newts.api.Context)1 Sample (org.opennms.newts.api.Sample)1 BooleanClause (org.opennms.newts.api.search.BooleanClause)1 Indexer (org.opennms.newts.api.search.Indexer)1 QueryBuilder (org.opennms.newts.api.search.QueryBuilder)1