Search in sources :

Example 6 with TermQuery

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

the class QueryParserTest method canParseQueriesWithSpecialCharacters.

@Test
public void canParseQueriesWithSpecialCharacters() throws ParseException {
    char[] specialCharacters = new char[] { '-', '+', '!', '~', '*', '?', '^', '[', ']', '{', '}' };
    for (char specialChar : specialCharacters) {
        Query query = new TermQuery(new Term("meat", "ground" + specialChar + "beef"));
        assertThat(parse(query), equalTo((Query) query));
        assertThat(parse("meat:ground" + specialChar + "beef"), equalTo(query));
    }
}
Also used : 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 7 with TermQuery

use of org.opennms.newts.api.search.TermQuery 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 8 with TermQuery

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

the class CassandraIndexerITCase method test.

@Test
public void test() {
    Map<String, String> base = map("meat", "people", "bread", "beer");
    List<Sample> samples = Lists.newArrayList();
    samples.add(sampleFor(new Resource("aaa", Optional.of(base)), "m0"));
    samples.add(sampleFor(new Resource("aab", Optional.of(map(base, "music", "metal", "beverage", "beer"))), "m0"));
    samples.add(sampleFor(new Resource("aac:aaa", Optional.of(map(base, "music", "country"))), "m0"));
    CassandraSession session = newtsInstance.getCassandraSession();
    ResourceMetadataCache mockCache = mock(ResourceMetadataCache.class);
    when(mockCache.get(any(Context.class), any(Resource.class))).thenReturn(Optional.<ResourceMetadata>absent());
    MetricRegistry registry = new MetricRegistry();
    ContextConfigurations contextConfigurations = new ContextConfigurations();
    CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(false).build();
    Indexer indexer = new CassandraIndexer(session, 86400, mockCache, registry, options, new SimpleResourceIdSplitter(), contextConfigurations);
    indexer.update(samples);
    CassandraSearcher searcher = new CassandraSearcher(session, registry, contextConfigurations);
    // Match path components
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(2));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aac")).size(), equalTo(1));
    // Match attribute values
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("people")).size(), equalTo(3));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("metal")).size(), equalTo(1));
    // Match attribute key + value pairs
    BooleanQuery query = new BooleanQuery();
    query.add(new TermQuery(new Term("beverage", "beer")), Operator.OR);
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, query).size(), equalTo(1));
    // Or'd terms
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("metal", "country")).size(), equalTo(2));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("beer", "wine")).size(), equalTo(3));
    // And'd terms
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAllValues("metal", "country")).size(), equalTo(0));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAllValues("aaa", "aac")).size(), equalTo(1));
    // Groups queries
    // (beer AND metal) OR (aaa AND country)
    BooleanQuery subquery1 = new BooleanQuery();
    subquery1.add(new TermQuery(new Term("beer")), Operator.OR);
    subquery1.add(new TermQuery(new Term("metal")), Operator.AND);
    BooleanQuery subquery2 = new BooleanQuery();
    subquery2.add(new TermQuery(new Term("aaa")), Operator.OR);
    subquery2.add(new TermQuery(new Term("country")), Operator.AND);
    query = new BooleanQuery();
    query.add(subquery1, Operator.OR);
    query.add(subquery2, Operator.OR);
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, query).size(), equalTo(2));
    // Attributes are retrieved
    Result r = searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("metal")).iterator().next();
    assertThat(r.getResource().getId(), is(equalTo("aab")));
    assertThat(r.getResource().getAttributes().isPresent(), is(true));
    assertThat(r.getResource().getAttributes().get(), equalTo(map(base, "music", "metal", "beverage", "beer")));
    // Metrics too
    assertThat(r.getMetrics().size(), equalTo(1));
    assertThat(r.getMetrics().iterator().next(), equalTo("m0"));
}
Also used : Context(org.opennms.newts.api.Context) BooleanQuery(org.opennms.newts.api.search.BooleanQuery) TermQuery(org.opennms.newts.api.search.TermQuery) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) QueryBuilder(org.opennms.newts.api.search.QueryBuilder) Resource(org.opennms.newts.api.Resource) CassandraSession(org.opennms.newts.cassandra.CassandraSession) Term(org.opennms.newts.api.search.Term) Result(org.opennms.newts.api.search.SearchResults.Result) Indexer(org.opennms.newts.api.search.Indexer) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) Test(org.junit.Test)

Example 9 with TermQuery

use of org.opennms.newts.api.search.TermQuery 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 10 with TermQuery

use of org.opennms.newts.api.search.TermQuery 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)10 TermQuery (org.opennms.newts.api.search.TermQuery)10 Query (org.opennms.newts.api.search.Query)8 Term (org.opennms.newts.api.search.Term)7 Test (org.junit.Test)6 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