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();
}
}
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;
}
Aggregations