Commit c22ffcd9 authored by Matija Obreza's avatar Matija Obreza Committed by Maxym Borodenko
Browse files

ES: Support for OR operator

parent c6d005a8
......@@ -54,14 +54,10 @@ public class ElasticQueryBuilder implements Visitor<Void, Void> {
private final Map<String, RangeQueryBuilder> ranges = new HashMap<>();
private final ElasticQueryBuilder self = this;
public QueryBuilder getQuery() {
BoolQueryBuilder root = QueryBuilders.boolQuery();
mustClauses.forEach(must -> root.filter(must));
mustNotClauses.forEach(mustNot -> root.mustNot(mustNot));
// shouldClauses.forEach(should -> root.should(should));
// if (shouldClauses.size() > 0) root.minimumNumberShouldMatch(1);
return root;
}
......@@ -124,10 +120,7 @@ public class ElasticQueryBuilder implements Visitor<Void, Void> {
if (operator == Ops.AND) {
handleAnd(args);
} else if (operator == Ops.OR) {
for (Expression<?> expr : args) {
printExpression(".. " + operator, expr);
expr.accept(self, null);
}
handleOr(args);
} else if (operator == Ops.EQ || operator == Ops.IN) {
if (Path.class.isAssignableFrom(args.get(0).getClass())) {
LOG.debug("EQUALS: {}", args);
......@@ -195,7 +188,7 @@ public class ElasticQueryBuilder implements Visitor<Void, Void> {
// printExpression("a1: " + type.getName() + " " + operator, a1);
}
private int size() {
public int size() {
return this.mustClauses.size() + this.mustNotClauses.size();
}
......@@ -211,15 +204,29 @@ public class ElasticQueryBuilder implements Visitor<Void, Void> {
mustNotClauses.addAll(andBuilder.mustNotClauses);
} else {
mustClauses.add(andBuilder.getQuery());
}
}
}
private void handleOr(List<Expression<?>> args) {
LOG.debug("OR expr: {}", args);
ElasticQueryBuilder orBuilder = new ElasticQueryBuilder();
for (Expression<?> a : args) {
a.accept(orBuilder, null);
}
BoolQueryBuilder orQuery = boolQuery();
orQuery.minimumShouldMatch(1);
orBuilder.mustClauses.forEach(should -> orQuery.should(should));
mustClauses.add(orQuery);
}
private void handleNot(Expression<?> notExp) {
LOG.debug("NOT expr: {}", notExp);
ElasticQueryBuilder notBuilder = new ElasticQueryBuilder();
notExp.accept(notBuilder, null);
// TODO Is this a problem?
notBuilder.mustClauses.forEach(mustNot -> mustNotClauses.add(mustNot));
notBuilder.mustNotClauses.forEach(must -> mustClauses.add(must));
}
private void handleLike(Operator operator, Path<?> path, Expression<?> val) {
......@@ -308,7 +315,7 @@ public class ElasticQueryBuilder implements Visitor<Void, Void> {
}
return obj;
}
private Collection<?> toValues(Expression<?> value) {
if (value instanceof Constant<?>) {
Constant<?> cons = (Constant<?>) value;
......
......@@ -22,9 +22,11 @@ import static org.hamcrest.Matchers.*;
import java.io.IOException;
import java.util.List;
import org.apache.commons.collections4.SetUtils;
import org.elasticsearch.index.query.QueryBuilder;
import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys2.server.component.elastic.ElasticQueryBuilder;
import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.filter.AccessionGeoFilter;
import org.junit.Test;
......@@ -32,6 +34,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jayway.jsonpath.JsonPath;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import net.minidev.json.JSONArray;
......@@ -91,4 +94,39 @@ public class EsQueryTest {
assertThat(((List<?>) storages.get(0)), containsInAnyOrder(20, 10));
}
@Test
public void testNotFilter() {
AccessionFilter af = new AccessionFilter();
AccessionFilter not = af.NOT = new AccessionFilter();
not.active = false;
not.origin().code3 = SetUtils.hashSet("SVN", "DEU");
Predicate predicate = af.buildPredicate();
ElasticQueryBuilder visitor = new ElasticQueryBuilder();
predicate.accept(visitor, null);
LOG.debug("ES query: {}", visitor.getQuery());
// Object active = JsonPath.read(esQuery.toString(),
// "bool.filter.bool.must.terms.active");
// assertThat(active, notNullValue());
// assertThat(((List<?>) active), hasSize(1));
}
@Test
public void testOrFilter() {
Predicate predicate = ExpressionUtils.allOf(
QAccession.accession.instituteCode.eq("NGA039"),
QAccession.accession.countryOfOrigin.code2.eq("TG").or(QAccession.accession.countryOfOrigin.code3.eq("TGO"))
);
ElasticQueryBuilder visitor = new ElasticQueryBuilder();
predicate.accept(visitor, null);
LOG.debug("ES query: {}", visitor.getQuery());
// Object active = JsonPath.read(esQuery.toString(),
// "bool.filter.bool.must.terms.active");
// assertThat(active, notNullValue());
// assertThat(((List<?>) active), hasSize(1));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment