Search in sources :

Example 6 with BooleanQuery

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

the class CassandraSearcher method search.

@Override
public SearchResults search(Context context, Query query, boolean populateMetricsAndAttributes) {
    checkNotNull(context, "context argument");
    checkNotNull(query, "query argument");
    Timer.Context ctx = m_searchTimer.time();
    ConsistencyLevel readConsistency = m_contextConfigurations.getReadConsistency(context);
    SearchResults searchResults = new SearchResults();
    try {
        Set<String> ids;
        Query q = query.rewrite();
        if (q instanceof BooleanQuery) {
            ids = searchForIds(context, (BooleanQuery) q, readConsistency);
        } else if (q instanceof TermQuery) {
            ids = searchForIds(context, (TermQuery) q, readConsistency);
        } else {
            throw new IllegalStateException("Unsupported query: " + q);
        }
        for (final String id : ids) {
            if (!populateMetricsAndAttributes) {
                Resource resource = new Resource(id);
                List<String> emptyList = Collections.emptyList();
                searchResults.addResult(resource, emptyList);
            } else {
                // Fetch the metric names and attributes concurrently
                ResultSetFuture attrsFuture = fetchResourceAttributes(context, id, readConsistency);
                ResultSetFuture metricsFuture = fetchMetricNames(context, id, readConsistency);
                try {
                    Map<String, String> attrs = getResourceAttributesFromResults(attrsFuture);
                    Collection<String> metrics = getMetricNamesFromResults(metricsFuture);
                    Resource resource = attrs.size() > 0 ? new Resource(id, Optional.of(attrs)) : new Resource(id);
                    searchResults.addResult(resource, metrics);
                } catch (ExecutionException | InterruptedException e) {
                    throw Throwables.propagate(e);
                }
            }
        }
        return searchResults;
    } finally {
        ctx.stop();
    }
}
Also used : BooleanQuery(org.opennms.newts.api.search.BooleanQuery) TermQuery(org.opennms.newts.api.search.TermQuery) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) 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) ConsistencyLevel(com.datastax.driver.core.ConsistencyLevel) Timer(com.codahale.metrics.Timer) ExecutionException(java.util.concurrent.ExecutionException)

Example 7 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)

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