Search in sources :

Example 1 with AttributeFilter

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);
}
Also used : ArrayList(java.util.ArrayList) AttributeFilter(org.kie.kogito.persistence.api.query.AttributeFilter)

Example 2 with AttributeFilter

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());
}
Also used : Query(io.redisearch.Query) ArrayList(java.util.ArrayList) SearchResult(io.redisearch.SearchResult) Document(io.redisearch.Document) AttributeFilter(org.kie.kogito.persistence.api.query.AttributeFilter) Client(io.redisearch.client.Client) Test(org.junit.jupiter.api.Test)

Example 3 with AttributeFilter

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());
}
Also used : MatchedExecutionHeaders(org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders) AttributeFilter(org.kie.kogito.persistence.api.query.AttributeFilter) ArrayList(java.util.ArrayList) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision)

Example 4 with AttributeFilter

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));
}
Also used : Query(io.redisearch.Query) AttributeFilter(org.kie.kogito.persistence.api.query.AttributeFilter) AttributeFilter(org.kie.kogito.persistence.api.query.AttributeFilter) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 5 with AttributeFilter

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());
        }
    }
}
Also used : AttributeFilter(org.kie.kogito.persistence.api.query.AttributeFilter) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)5 AttributeFilter (org.kie.kogito.persistence.api.query.AttributeFilter)5 Query (io.redisearch.Query)2 Test (org.junit.jupiter.api.Test)2 Document (io.redisearch.Document)1 SearchResult (io.redisearch.SearchResult)1 Client (io.redisearch.client.Client)1 List (java.util.List)1 MatchedExecutionHeaders (org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders)1 Decision (org.kie.kogito.trusty.storage.api.model.decision.Decision)1