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();
}
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))));
}
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);
}
Aggregations