use of org.opennms.newts.api.search.TermQuery 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;
}
use of org.opennms.newts.api.search.TermQuery 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));
}
use of org.opennms.newts.api.search.TermQuery 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);
}
use of org.opennms.newts.api.search.TermQuery 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));
}
use of org.opennms.newts.api.search.TermQuery 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));
}
Aggregations