Commit 75d0cbca authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

ElasticQueryBuilder updates for ES6

- UUID must be serialized as String
parent 77648e16
......@@ -5,7 +5,6 @@ import static org.elasticsearch.index.query.QueryBuilders.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -41,11 +40,17 @@ import com.querydsl.core.types.TemplateExpression;
import com.querydsl.core.types.Visitor;
import com.querydsl.core.types.dsl.NumberPath;
/**
* Converter from a Querydsl predicate to Elasticsearch query.
*
* @author Matija Obreza
* @author Maxym Borodenko
*/
public class ElasticQueryBuilder implements Visitor<Void, Void> {
private static Logger LOG = LoggerFactory.getLogger(ElasticQueryBuilder.class);
private final List<QueryBuilder> mustClauses = new ArrayList<>();
private final List<QueryBuilder> mustNotClauses = new ArrayList<>();
private final List<QueryBuilder> mustClauses = new ArrayList<>();
private final List<QueryBuilder> mustNotClauses = new ArrayList<>();
private final Map<String, RangeQueryBuilder> ranges = new HashMap<>();
......@@ -280,35 +285,38 @@ public class ElasticQueryBuilder implements Visitor<Void, Void> {
if (value instanceof Constant<?>) {
Constant<?> cons = (Constant<?>) value;
Object obj = cons.getConstant();
if (obj.getClass().isAssignableFrom(Date.class)) {
obj = ((Date) (obj)).getTime();
}
LOG.debug("toValue of {}: c={}", obj, obj.getClass());
return obj;
return convertValue(obj);
}
throw new RuntimeException("Unhandled value " + value);
}
private Object convertValue(Object obj) {
if (obj == null) {
return null;
}
Class<? extends Object> objClass = obj.getClass();
LOG.debug("toValue of {}: c={}", obj, objClass);
if (objClass.isEnum()) {
return obj.toString();
}
if (UUID.class.isAssignableFrom(objClass)) {
return obj.toString();
}
return obj;
}
private Collection<?> toValues(Expression<?> value) {
if (value instanceof Constant<?>) {
Constant<?> cons = (Constant<?>) value;
Object obj = cons.getConstant();
Class<? extends Object> obyClazz = obj.getClass();
LOG.debug("toValues of {}: c={}", obj, obyClazz);
if (obyClazz.isEnum() || UUID.class.isAssignableFrom(obyClazz)) {
if (obj instanceof Collection<?>) {
Collection<?> c = (Collection<?>) obj;
return c.stream().map((forString) -> forString.toString()).collect(Collectors.toList());
} else {
return newArrayList(obj.toString());
}
}
LOG.debug("toValues of {}", obj);
if (obj instanceof Collection<?>) {
Collection<?> c = (Collection<?>) obj;
return c;
return c.stream().map((forString) -> convertValue(forString)).collect(Collectors.toList());
} else {
return newArrayList(obj);
return newArrayList(convertValue(obj));
}
}
......
......@@ -19,12 +19,17 @@ import static com.google.common.collect.Sets.newHashSet;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.filters.DatasetFilter;
import org.genesys.test.base.AbstractElasticServiceTest;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionIdRepository;
......@@ -256,6 +261,44 @@ public class ElasticQueryBuilderTest extends AbstractElasticServiceTest {
}
@Test
public void testDate() throws SearchException, JsonProcessingException {
AccessionFilter af = new AccessionFilter();
af.createdDate().le = new Date();
List<Accession> search = elasticsearchService.find(Accession.class, af);
assertThat(search.size(), is(4));
}
@Test
public void testUUID() throws SearchException, JsonProcessingException {
AccessionFilter af = new AccessionFilter();
af.uuid().add(UUID.randomUUID());
List<Accession> search = elasticsearchService.find(Accession.class, af);
assertThat(search.size(), is(0));
af.uuid().add(UUID.randomUUID());
search = elasticsearchService.find(Accession.class, af);
assertThat(search.size(), is(0));
}
@Test
public void testEnum() throws SearchException, JsonProcessingException {
DatasetFilter df = new DatasetFilter();
df.state(PublishState.DRAFT);
List<Dataset> search = elasticsearchService.find(Dataset.class, df);
assertThat(search.size(), is(0));
df.state(PublishState.PUBLISHED);
search = elasticsearchService.find(Dataset.class, df);
assertThat(search.size(), is(0));
}
/**
* Test that removing documents from index
*/
......
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