Search in sources :

Example 1 with Query

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

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

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

the class QueryParserTest method canParseSimpleQuerys.

@Test
public void canParseSimpleQuerys() throws ParseException {
    Query query = new TermQuery(new Term("beef"));
    assertThat(parse(query), equalTo(query));
    assertThat(parse("beef"), equalTo(query));
    query = new TermQuery(new Term("meat", "beef"));
    assertThat(parse(query), equalTo(query));
    assertThat(parse("meat: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 4 with Query

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

the class SearchResource method search.

@GET
@Timed
public Collection<SearchResultDTO> search(@QueryParam("q") Optional<String> query, @QueryParam("context") Optional<String> contextId) {
    checkArgument(query.isPresent(), "missing required query parameter (q=<argument>)");
    QueryParser qp = new QueryParser();
    Query parsedQuery;
    try {
        parsedQuery = qp.parse(query.get());
    } catch (ParseException e) {
        throw new WebApplicationException(e, Response.status(Status.BAD_REQUEST).entity("Invalid query " + query.get()).build());
    }
    Context context = contextId.isPresent() ? new Context(contextId.get()) : Context.DEFAULT_CONTEXT;
    return Transform.searchResultDTOs(m_searcher.search(context, parsedQuery));
}
Also used : Context(org.opennms.newts.api.Context) QueryParser(org.opennms.newts.api.search.query.QueryParser) Query(org.opennms.newts.api.search.Query) WebApplicationException(javax.ws.rs.WebApplicationException) ParseException(org.opennms.newts.api.search.query.ParseException) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 5 with Query

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

the class NewtsResourceStorageDao method searchFor.

private SearchResults searchFor(ResourcePath path, int depth, boolean fetchMetrics) {
    final Query q = findResourcesWithMetricsAtDepth(path, depth);
    LOG.trace("Searching for '{}'.", q);
    final SearchResults results = m_searcher.search(m_context, q, fetchMetrics);
    LOG.trace("Found {} results.", results.size());
    return results;
}
Also used : Query(org.opennms.newts.api.search.Query) SearchResults(org.opennms.newts.api.search.SearchResults)

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