Search in sources :

Example 6 with Query

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

the class QueryParserTest method canEscapeColons.

@Test
public void canEscapeColons() throws ParseException {
    Query query = new TermQuery(new Term("meat", "be:ef\\"));
    assertThat(parse(query), equalTo((Query) query));
    assertThat(parse("meat:be\\:ef\\\\"), 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 Query

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

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

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

Example 10 with Query

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

Aggregations

Query (org.opennms.newts.api.search.Query)10 BooleanQuery (org.opennms.newts.api.search.BooleanQuery)8 TermQuery (org.opennms.newts.api.search.TermQuery)8 Test (org.junit.Test)5 Term (org.opennms.newts.api.search.Term)5 SearchResults (org.opennms.newts.api.search.SearchResults)3 Resource (org.opennms.newts.api.Resource)2 Timer (com.codahale.metrics.Timer)1 Timed (com.codahale.metrics.annotation.Timed)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 GET (javax.ws.rs.GET)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 IAnswer (org.easymock.IAnswer)1 ResourcePath (org.opennms.netmgt.model.ResourcePath)1 Context (org.opennms.newts.api.Context)1 BooleanClause (org.opennms.newts.api.search.BooleanClause)1 ParseException (org.opennms.newts.api.search.query.ParseException)1