Search in sources :

Example 86 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class SearchDataTransaction method buildSort.

/**
 * Builds a lucene Sort object from and Elide Sorting object.
 * @param sorting Elide sorting object
 * @param entityType The entity being sorted.
 * @return A lucene Sort object
 */
private Sort buildSort(Sorting sorting, Type<?> entityType) {
    Class<?> entityClass = null;
    if (entityType != null) {
        Preconditions.checkState(entityType instanceof ClassType);
        entityClass = ((ClassType) entityType).getCls();
    }
    QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity(entityClass).get();
    SortFieldContext context = null;
    for (Map.Entry<Path, Sorting.SortOrder> entry : sorting.getSortingPaths().entrySet()) {
        String fieldName = entry.getKey().lastElement().get().getFieldName();
        SortableField sortableField = dictionary.getAttributeOrRelationAnnotation(entityType, SortableField.class, fieldName);
        fieldName = sortableField.forField().isEmpty() ? fieldName : sortableField.forField();
        if (context == null) {
            context = builder.sort().byField(fieldName);
        } else {
            context.andByField(fieldName);
        }
        Sorting.SortOrder order = entry.getValue();
        if (order == Sorting.SortOrder.asc) {
            context = context.asc();
        } else {
            context = context.desc();
        }
    }
    if (context == null) {
        throw new IllegalStateException("Invalid Sort rules");
    }
    return context.createSort();
}
Also used : Path(com.yahoo.elide.core.Path) SortableField(org.hibernate.search.annotations.SortableField) QueryBuilder(org.hibernate.search.query.dsl.QueryBuilder) ClassType(com.yahoo.elide.core.type.ClassType) Sorting(com.yahoo.elide.core.request.Sorting) SortFieldContext(org.hibernate.search.query.dsl.sort.SortFieldContext) Map(java.util.Map)

Example 87 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class FilterTranslatorTest method testBetweenOperator.

@Test
public void testBetweenOperator() throws Exception {
    List<Path.PathElement> authorId = Arrays.asList(new Path.PathElement(Book.class, Author.class, "authors"), new Path.PathElement(Author.class, Long.class, "id"));
    List<Path.PathElement> publishDate = Arrays.asList(new Path.PathElement(Book.class, Long.class, "publishDate"));
    FilterPredicate authorPred = new FilterPredicate(new Path(authorId), Operator.BETWEEN, Arrays.asList(1, 15));
    FilterPredicate publishPred = new FilterPredicate(new Path(publishDate), Operator.NOTBETWEEN, Arrays.asList(1, 15));
    AndFilterExpression andFilter = new AndFilterExpression(authorPred, publishPred);
    FilterTranslator filterOp = new FilterTranslator(dictionary);
    String query = filterOp.apply(andFilter, false);
    String authorP1 = authorPred.getParameters().get(0).getPlaceholder();
    String authorP2 = authorPred.getParameters().get(1).getPlaceholder();
    String publishP1 = publishPred.getParameters().get(0).getPlaceholder();
    String publishP2 = publishPred.getParameters().get(1).getPlaceholder();
    String expected = "(authors.id BETWEEN " + authorP1 + " AND " + authorP2 + " AND " + "publishDate NOT BETWEEN " + publishP1 + " AND " + publishP2 + ")";
    assertEquals(expected, query);
    // Assert excepetion if parameter length is not 2
    assertThrows(IllegalArgumentException.class, () -> filterOp.apply(new FilterPredicate(new Path(authorId), Operator.BETWEEN, Arrays.asList(3))));
}
Also used : Path(com.yahoo.elide.core.Path) Book(example.Book) Author(example.Author) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Test(org.junit.jupiter.api.Test)

Example 88 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class FilterTranslatorTest method testMemberOfOperator.

@Test
public void testMemberOfOperator() throws Exception {
    List<Path.PathElement> path = Arrays.asList(new Path.PathElement(Book.class, String.class, "awards"));
    FilterPredicate p1 = new FilterPredicate(new Path(path), Operator.HASMEMBER, Arrays.asList("awards1"));
    FilterPredicate p2 = new FilterPredicate(new Path(path), Operator.HASNOMEMBER, Arrays.asList("awards2"));
    AndFilterExpression and = new AndFilterExpression(p1, p2);
    FilterTranslator filterOp = new FilterTranslator(dictionary);
    String query = filterOp.apply(and, false);
    String p1Params = p1.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
    String p2Params = p2.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
    String expected = "(" + p1Params + " MEMBER OF awards " + "AND " + p2Params + " NOT MEMBER OF awards)";
    assertEquals(expected, query);
}
Also used : Path(com.yahoo.elide.core.Path) Book(example.Book) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Test(org.junit.jupiter.api.Test)

Aggregations

Path (com.yahoo.elide.core.Path)88 Test (org.junit.jupiter.api.Test)67 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)51 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)41 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)35 Query (com.yahoo.elide.datastores.aggregation.query.Query)35 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)34 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)33 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)31 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)29 Book (example.Book)29 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)22 Date (java.util.Date)19 Argument (com.yahoo.elide.core.request.Argument)14 EntityProjection (com.yahoo.elide.core.request.EntityProjection)14 GameRevenue (example.GameRevenue)14 HashSet (java.util.HashSet)14 Author (example.Author)12 Publisher (example.Publisher)9 Sorting (com.yahoo.elide.core.request.Sorting)8