use of org.kie.kogito.persistence.api.query.AttributeFilter in project kogito-apps by kiegroup.
the class RedisQueryFactory method buildQueryBody.
static String buildQueryBody(String indexName, List<AttributeFilter<?>> filters) {
List<String> components = new ArrayList<>();
components.add(String.format("@%s:%s", INDEX_NAME_FIELD, indexName));
for (AttributeFilter attributeFilter : filters) {
switch(attributeFilter.getCondition()) {
// Indexed values have to be escaped according to https://github.com/RediSearch/RediSearch/issues/1148
case EQUAL:
components.add(String.format("@%s:%s", attributeFilter.getAttribute(), Sanitizer.sanitize(attributeFilter.getValue())));
break;
case LIKE:
if (!"".equals(attributeFilter.getValue()) && !"*".equals(attributeFilter.getValue())) {
components.add(String.format("@%s:%s", attributeFilter.getAttribute(), Sanitizer.sanitize(attributeFilter.getValue())));
}
break;
}
}
return String.join(" ", components);
}
use of org.kie.kogito.persistence.api.query.AttributeFilter in project kogito-apps by kiegroup.
the class RedisQueryTest method executeTest.
@Test
public void executeTest() throws JsonProcessingException {
Client client = Mockito.mock(Client.class);
Person person = new Person("pippo", 20);
// Add the raw object to the map
Map<String, Object> map = JsonUtils.getMapper().convertValue(person, Map.class);
map.put(RAW_OBJECT_FIELD, JsonUtils.getMapper().writeValueAsString(person));
// Mock the response
Document document = new Document("pippo", map);
SearchResult searchResult = new SearchResult(singletonList(0L), false, false, false);
searchResult.docs.add(document);
when(client.search(any(Query.class))).thenReturn(searchResult);
RedisQuery<Person> redisQuery = new RedisQuery<>(client, TEST_INDEX_NAME, Person.class);
// Add a filter
List<AttributeFilter<?>> filters = new ArrayList<>();
filters.add(QueryFilterFactory.equalTo(NAME_PROPERTY, "pippo"));
redisQuery.filter(filters);
List<Person> result = redisQuery.execute();
Assertions.assertEquals(1, result.size());
Assertions.assertEquals("pippo", result.get(0).getName());
Assertions.assertEquals(20, result.get(0).getAge());
}
use of org.kie.kogito.persistence.api.query.AttributeFilter in project kogito-apps by kiegroup.
the class TrustyServiceImpl method getExecutionHeaders.
@Override
public MatchedExecutionHeaders getExecutionHeaders(OffsetDateTime from, OffsetDateTime to, int limit, int offset, String prefix) {
Storage<String, Decision> storage = storageService.getDecisionsStorage();
List<AttributeFilter<?>> filters = new ArrayList<>();
filters.add(QueryFilterFactory.like(Execution.EXECUTION_ID_FIELD, prefix + "*"));
filters.add(QueryFilterFactory.greaterThanEqual(Execution.EXECUTION_TIMESTAMP_FIELD, from.toInstant().toEpochMilli()));
filters.add(QueryFilterFactory.lessThanEqual(Execution.EXECUTION_TIMESTAMP_FIELD, to.toInstant().toEpochMilli()));
ArrayList result = new ArrayList<>(storage.query().sort(asList(orderBy(Execution.EXECUTION_TIMESTAMP_FIELD, DESC))).filter(filters).execute());
if (result.size() < offset) {
throw new IllegalArgumentException("Out of bound start offset in result");
}
return new MatchedExecutionHeaders(result.subList(offset, Math.min(offset + limit, result.size())), result.size());
}
use of org.kie.kogito.persistence.api.query.AttributeFilter in project kogito-apps by kiegroup.
the class RedisQueryFactoryTest method addFiltersTest.
@Test
public void addFiltersTest() {
List<AttributeFilter<?>> filters = new ArrayList<>();
filters.add(QueryFilterFactory.equalTo("shouldBeIgnored", 0));
filters.add(QueryFilterFactory.like("shouldBeIgnoredAsWell", "test"));
filters.add(QueryFilterFactory.between("betweenAttribute", 0, 1));
filters.add(QueryFilterFactory.greaterThan("greaterThanAttribute", 0d));
filters.add(QueryFilterFactory.greaterThanEqual("greaterThanEqualAttribute", 0));
filters.add(QueryFilterFactory.lessThan("lessThanAttribute", 0d));
filters.add(QueryFilterFactory.lessThanEqual("lessThanEqualAttribute", 0));
Query query = Mockito.mock(Query.class);
when(query.addFilter(any(io.redisearch.Query.NumericFilter.class))).thenReturn(query);
RedisQueryFactory.addFilters(query, filters);
verify(query, times(5)).addFilter(any(Query.Filter.class));
}
use of org.kie.kogito.persistence.api.query.AttributeFilter in project kogito-apps by kiegroup.
the class RedisQueryFactory method addFilters.
static void addFilters(io.redisearch.Query query, List<AttributeFilter<?>> filters) {
for (AttributeFilter attributeFilter : filters) {
switch(attributeFilter.getCondition()) {
case EQUAL:
case LIKE:
break;
case GT:
query.addFilter(new io.redisearch.Query.NumericFilter(attributeFilter.getAttribute(), convertNumeric(attributeFilter.getValue()), true, Double.POSITIVE_INFINITY, false));
break;
case GTE:
query.addFilter(new io.redisearch.Query.NumericFilter(attributeFilter.getAttribute(), convertNumeric(attributeFilter.getValue()), false, Double.POSITIVE_INFINITY, false));
break;
case LT:
query.addFilter(new io.redisearch.Query.NumericFilter(attributeFilter.getAttribute(), Double.NEGATIVE_INFINITY, false, convertNumeric(attributeFilter.getValue()), true));
break;
case LTE:
query.addFilter(new io.redisearch.Query.NumericFilter(attributeFilter.getAttribute(), Double.NEGATIVE_INFINITY, false, convertNumeric(attributeFilter.getValue()), false));
break;
case BETWEEN:
List<?> value = (List<?>) attributeFilter.getValue();
query.addFilter(new io.redisearch.Query.NumericFilter(attributeFilter.getAttribute(), convertNumeric(value.get(0)), false, convertNumeric(value.get(1)), false));
break;
default:
throw new UnsupportedOperationException("Redis does not support query filter: " + attributeFilter.getCondition());
}
}
}
Aggregations