Search in sources :

Example 91 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class QueryGeneratorTest method generateOneQueryRuleNotEqualsCompoundNull.

@Test(expectedExceptions = MolgenisQueryException.class)
public void generateOneQueryRuleNotEqualsCompoundNull() {
    Object value = null;
    Query<Entity> q = new QueryImpl<>().not().eq(compoundAttrName, value);
    QueryBuilder query = queryGenerator.createQueryBuilder(q, entityType);
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) QueryImpl(org.molgenis.data.support.QueryImpl) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Test(org.testng.annotations.Test)

Example 92 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class L3Cache method createCacheLoader.

/**
 * Create a cacheloader that loads entity ids from the repository and stores them together with their query
 *
 * @return the {@link CacheLoader}
 */
private CacheLoader<Query<Entity>, List<Object>> createCacheLoader(final Repository<Entity> repository) {
    String repositoryName = repository.getName();
    Fetch idAttributeFetch = new Fetch().field(repository.getEntityType().getIdAttribute().getName());
    return new CacheLoader<Query<Entity>, List<Object>>() {

        /**
         * Loads {@link Entity} identifiers for a {@link Query}
         * @param query the cache key to load
         * @return {@link List} of identifier {@link Object}s
         */
        @Override
        public List<Object> load(@Nonnull Query<Entity> query) {
            LOG.trace("Loading identifiers from repository {} for query {}", repositoryName, query);
            return repository.findAll(new QueryImpl<>(query).fetch(idAttributeFetch)).map(Entity::getIdValue).collect(toList());
        }
    };
}
Also used : Fetch(org.molgenis.data.Fetch) Entity(org.molgenis.data.Entity) QueryImpl(org.molgenis.data.support.QueryImpl) Query(org.molgenis.data.Query) Nonnull(javax.annotation.Nonnull) CacheLoader(com.google.common.cache.CacheLoader)

Example 93 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class QueryGeneratorReferencesTest method generateOneQueryRuleNotEqualsEqualsBool.

@Test
public void generateOneQueryRuleNotEqualsEqualsBool() {
    Boolean value = Boolean.TRUE;
    Query<Entity> q = new QueryImpl<>().not().eq(PREFIX + refBoolAttributeName, value);
    QueryBuilder query = queryGenerator.createQueryBuilder(q, entityType);
    QueryBuilder expectedQuery = boolQuery().mustNot(constantScoreQuery(nestedQuery(REF_ENTITY_ATT, termQuery(PREFIX + refBoolAttributeName, value), ScoreMode.Avg)));
    assertQueryBuilderEquals(query, expectedQuery);
}
Also used : QueryImpl(org.molgenis.data.support.QueryImpl) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Test(org.testng.annotations.Test)

Example 94 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class QueryGeneratorReferencesTest method generateOneQueryRuleGreaterEqualDate.

@Test
public void generateOneQueryRuleGreaterEqualDate() throws ParseException {
    String date = "2015-05-22";
    LocalDate value = LocalDate.parse(date);
    Query<Entity> q;
    q = new QueryImpl<>().ge(PREFIX + refDateAttributeName, value);
    QueryBuilder query = queryGenerator.createQueryBuilder(q, entityType);
    QueryBuilder expectedQuery = constantScoreQuery(nestedQuery(REF_ENTITY_ATT, rangeQuery(PREFIX + refDateAttributeName).gte(date), ScoreMode.Avg));
    assertQueryBuilderEquals(query, expectedQuery);
}
Also used : QueryImpl(org.molgenis.data.support.QueryImpl) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test)

Example 95 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class SearchAllServiceTest method testFindAll.

@Test
public void testFindAll() throws Exception {
    when(dataService.findAll(PackageMetadata.PACKAGE, Package.class)).thenReturn(Stream.of(pack1, pack2, pack3, pack_sys));
    when(dataService.findAll(EntityTypeMetadata.ENTITY_TYPE_META_DATA, EntityType.class)).thenReturn(Stream.of(entity1, entity2, entity3, entity4, abstractEntity));
    when(dataService.count("entity id 1", new QueryImpl<>().search("test"))).thenReturn(2L);
    when(dataService.count("entity id 3", new QueryImpl<>().search("test"))).thenReturn(6L);
    when(dataService.count("entity id 4", new QueryImpl<>().search("test"))).thenReturn(11L);
    PackageResult packageResult = PackageResult.create("package id 1", "package test nr 1", "package description 1");
    AttributeResult attributeResult = AttributeResult.create("attr test nr 1", "attr description 1", "BOOL");
    EntityTypeResult entityTypeResult1 = EntityTypeResult.builder().setId("entity id 1").setLabel("entity nr 1").setDescription("entity description 1").setPackageId("package id 2").setLabelMatch(false).setDescriptionMatch(false).setAttributes(singletonList(attributeResult)).setNrOfMatchingEntities(2).build();
    EntityTypeResult entityTypeResult2 = EntityTypeResult.builder().setId("entity id 3").setLabel("entity test nr 3").setDescription("entity description 3").setPackageId("package id 3").setLabelMatch(true).setDescriptionMatch(false).setAttributes(Collections.emptyList()).setNrOfMatchingEntities(6).build();
    EntityTypeResult entityTypeResult3 = EntityTypeResult.builder().setId("entity id 4").setLabel("entity nr 4").setDescription("entity test description 4").setPackageId(null).setLabelMatch(false).setDescriptionMatch(true).setAttributes(Collections.emptyList()).setNrOfMatchingEntities(11).build();
    Result result = Result.builder().setEntityTypes(Arrays.asList(entityTypeResult1, entityTypeResult2, entityTypeResult3)).setPackages(singletonList(packageResult)).build();
    assertEquals(searchAllService.searchAll("test"), result);
}
Also used : EntityTypeResult(org.molgenis.searchall.model.EntityTypeResult) QueryImpl(org.molgenis.data.support.QueryImpl) PackageResult(org.molgenis.searchall.model.PackageResult) AttributeResult(org.molgenis.searchall.model.AttributeResult) EntityTypeResult(org.molgenis.searchall.model.EntityTypeResult) AttributeResult(org.molgenis.searchall.model.AttributeResult) PackageResult(org.molgenis.searchall.model.PackageResult) Result(org.molgenis.searchall.model.Result) Test(org.testng.annotations.Test)

Aggregations

QueryImpl (org.molgenis.data.support.QueryImpl)98 Test (org.testng.annotations.Test)70 DynamicEntity (org.molgenis.data.support.DynamicEntity)37 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)36 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)36 EntityType (org.molgenis.data.meta.model.EntityType)28 Attribute (org.molgenis.data.meta.model.Attribute)25 Entity (org.molgenis.data.Entity)15 WithMockUser (org.springframework.security.test.context.support.WithMockUser)8 Stream (java.util.stream.Stream)7 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)7 AggregateQueryImpl (org.molgenis.data.support.AggregateQueryImpl)7 Objects.requireNonNull (java.util.Objects.requireNonNull)6 QueryRule (org.molgenis.data.QueryRule)6 AggregateQuery (org.molgenis.data.aggregation.AggregateQuery)6 EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)6 BeforeMethod (org.testng.annotations.BeforeMethod)6 Instant (java.time.Instant)5 LocalDate (java.time.LocalDate)5 Operator (org.molgenis.data.QueryRule.Operator)5