Search in sources :

Example 46 with QueryImpl

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

the class SemanticSearchServiceHelper method getAttributeIdentifiers.

/**
 * A helper function that gets identifiers of all the attributes from one EntityType
 */
public List<String> getAttributeIdentifiers(EntityType sourceEntityType) {
    Entity EntityTypeEntity = dataService.findOne(ENTITY_TYPE_META_DATA, new QueryImpl<>().eq(EntityTypeMetadata.ID, sourceEntityType.getId()));
    if (EntityTypeEntity == null)
        throw new MolgenisDataAccessException("Could not find EntityTypeEntity by the name of " + sourceEntityType.getId());
    List<String> attributeIdentifiers = new ArrayList<>();
    recursivelyCollectAttributeIdentifiers(EntityTypeEntity.getEntities(EntityTypeMetadata.ATTRIBUTES), attributeIdentifiers);
    return attributeIdentifiers;
}
Also used : Entity(org.molgenis.data.Entity) QueryImpl(org.molgenis.data.support.QueryImpl) MolgenisDataAccessException(org.molgenis.data.MolgenisDataAccessException)

Example 47 with QueryImpl

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

the class UntypedTagService method getTagsForPackage.

@Override
@RunAsSystem
public Iterable<SemanticTag<Package, LabeledResource, LabeledResource>> getTagsForPackage(Package p) {
    Entity packageEntity = dataService.findOne(PACKAGE, new QueryImpl<>().eq(PackageMetadata.ID, p.getId()));
    if (packageEntity == null) {
        throw new UnknownEntityException("Unknown package [" + p.getId() + "]");
    }
    List<SemanticTag<Package, LabeledResource, LabeledResource>> tags = Lists.newArrayList();
    for (Entity tagEntity : packageEntity.getEntities(PackageMetadata.TAGS)) {
        tags.add(SemanticTag.asTag(p, tagEntity));
    }
    return tags;
}
Also used : Entity(org.molgenis.data.Entity) QueryImpl(org.molgenis.data.support.QueryImpl) UnknownEntityException(org.molgenis.data.UnknownEntityException) SemanticTag(org.molgenis.data.semantic.SemanticTag) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Example 48 with QueryImpl

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

the class DynamicRepositoryDecoratorRegistryImpl method decorate.

@Override
public synchronized Repository<Entity> decorate(Repository<Entity> repository) {
    String entityTypeId = repository.getEntityType().getId();
    if (!entityTypeId.equals(DECORATOR_CONFIGURATION) && bootstrappingDone) {
        Query query = new QueryImpl().eq(ENTITY_TYPE_ID, entityTypeId);
        DecoratorConfiguration configuration = dataService.findOne(DECORATOR_CONFIGURATION, query, DecoratorConfiguration.class);
        if (configuration != null) {
            repository = decorateRepository(repository, configuration);
        }
    }
    return repository;
}
Also used : DecoratorConfiguration(org.molgenis.data.decorator.meta.DecoratorConfiguration) QueryImpl(org.molgenis.data.support.QueryImpl) Query(org.molgenis.data.Query)

Example 49 with QueryImpl

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

the class GenomeBrowserServiceTest method testGetReferenceTracksAll.

@Test
public void testGetReferenceTracksAll() {
    EntityType entity = mock(EntityType.class);
    when(entity.getLabel()).thenReturn("label");
    GenomeBrowserAttributes genomeBrowserAttributes = getGenomeBrowserAttributes("postion", "chrom", "normal", "mutant");
    GenomeBrowserTrack reference = GenomeBrowserTrack.create("ref_id", "label", "ref_label", entity, GenomeBrowserSettings.TrackType.VARIANT, null, GenomeBrowserSettings.MolgenisReferenceMode.NONE, genomeBrowserAttributes, null, null, null, null);
    EntityType molgenisEntity = mock(EntityType.class);
    when(molgenisEntity.getLabel()).thenReturn("label");
    GenomeBrowserTrack track = GenomeBrowserTrack.create("id", "label", "entityLabel", molgenisEntity, GenomeBrowserSettings.TrackType.VARIANT, Collections.singletonList(reference), GenomeBrowserSettings.MolgenisReferenceMode.ALL, genomeBrowserAttributes, "alert(\"test\")", "attr 1:attr1,reference attribute:REF,position on genome:POS", null, null);
    GenomeBrowserAttributes attrs1 = getGenomeBrowserAttributes("pos", "chr", "ref", "alt");
    GenomeBrowserAttributes attrs2 = getGenomeBrowserAttributes("POS", "CHROM", null, null);
    when(dataService.findAll(GenomeBrowserAttributesMetadata.GENOMEBROWSERATTRIBUTES, new QueryImpl<GenomeBrowserAttributes>().eq(GenomeBrowserAttributesMetadata.DEFAULT, true), GenomeBrowserAttributes.class)).thenReturn(Stream.of(attrs1, attrs2));
    EntityType type1 = mock(EntityType.class);
    EntityType type2 = mock(EntityType.class);
    EntityType type3 = mock(EntityType.class);
    Attribute labelAttr = mock(Attribute.class);
    when(labelAttr.getName()).thenReturn("refLabel");
    List<EntityType> types = new ArrayList<>();
    types.add(type1);
    when(type1.getIdValue()).thenReturn("type1");
    when(type1.getLabel()).thenReturn("label1");
    when(type1.getAttributeNames()).thenReturn(Arrays.asList("POS", "CHROM", "AAP", "NOOT", "MIES"));
    when(type1.getLabelAttribute()).thenReturn(labelAttr);
    types.add(type2);
    when(type2.getIdValue()).thenReturn("type2");
    when(type2.getLabel()).thenReturn("label2");
    when(type2.getAttributeNames()).thenReturn(Arrays.asList("pos", "chr", "ref", "alt", "aap"));
    when(type2.getLabelAttribute()).thenReturn(labelAttr);
    types.add(type3);
    when(type3.getIdValue()).thenReturn("type3");
    when(type3.getLabel()).thenReturn("label3");
    when(type3.getAttributeNames()).thenReturn(Arrays.asList("pos", "chr", "ref", "alternative", "monkey"));
    when(type3.getLabelAttribute()).thenReturn(labelAttr);
    MetaDataService metaDataService = mock(MetaDataService.class);
    when(dataService.getMeta()).thenReturn(metaDataService);
    when(metaDataService.getEntityTypes()).thenReturn(types.stream());
    Map<String, GenomeBrowserTrack> result = genomeBrowserService.getReferenceTracks(track);
    assertEquals(result.size(), 2);
    assertEquals(result.get("type2"), GenomeBrowserTrack.create("type2", "label2", "refLabel", type2, GenomeBrowserSettings.TrackType.VARIANT, Collections.emptyList(), GenomeBrowserSettings.MolgenisReferenceMode.ALL, attrs1, null, null, null, null));
    assertEquals(result.get("type1"), GenomeBrowserTrack.create("type1", "label1", "refLabel", type1, GenomeBrowserSettings.TrackType.VARIANT, Collections.emptyList(), GenomeBrowserSettings.MolgenisReferenceMode.ALL, attrs2, null, null, null, null));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) GenomeBrowserAttributes(org.molgenis.genomebrowser.meta.GenomeBrowserAttributes) QueryImpl(org.molgenis.data.support.QueryImpl) MetaDataService(org.molgenis.data.meta.MetaDataService) Attribute(org.molgenis.data.meta.model.Attribute) GenomeBrowserTrack(org.molgenis.genomebrowser.GenomeBrowserTrack) Test(org.testng.annotations.Test)

Example 50 with QueryImpl

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

the class SearchServiceIT method testSemanticSearch.

@Test
public void testSemanticSearch() {
    List<Entity> attributes = createDynamic(6).collect(toList());
    attributes.get(0).set(ATTR_STRING, "High chance of pulmonary disease");
    attributes.get(1).set(ATTR_STRING, "And now for something completely different...");
    attributes.get(2).set(ATTR_STRING, "Are you taking hypertensive medication?");
    attributes.get(3).set(ATTR_STRING, "Have you ever had high blood pressure? (Repeat) (1)");
    attributes.get(4).set(ATTR_STRING, "Do you suffer from Ocular hypertension?");
    attributes.get(5).set(ATTR_STRING, "Do you have a vascular disorder?");
    Entity ontology1 = attributes.get(0).getEntity(ATTR_CATEGORICAL);
    Entity ontology2 = attributes.get(1).getEntity(ATTR_CATEGORICAL);
    for (Entity term : attributes) {
        term.set(ATTR_CATEGORICAL, ontology1);
    }
    attributes.get(5).set(ATTR_CATEGORICAL, ontology2);
    searchService.index(entityTypeDynamic, attributes.stream());
    searchService.refreshIndex();
    List<String> queryTerms = asList("hypertension", "disorder vascular hypertensive", "increased pressure blood", "high pressure blood", "ocular^0.5 hypertension^0.5", "hypertension^0.25 idiopathic^0.25 pulmonary^0.25");
    QueryRule finalDisMaxQuery = new QueryRule(queryTerms.stream().flatMap(term -> Stream.of(new QueryRule(ATTR_STRING, FUZZY_MATCH, term), new QueryRule(ATTR_SCRIPT, FUZZY_MATCH, term))).collect(toList()));
    finalDisMaxQuery.setOperator(DIS_MAX);
    List<String> attributeIds = asList("0", "1", "2", "3", "4", "5");
    Query<Entity> query = new QueryImpl<>(asList(new QueryRule(ATTR_ID, IN, attributeIds), new QueryRule(AND), finalDisMaxQuery));
    List<Object> matchingAttributeIDs = searchService.search(entityTypeDynamic, query).collect(toList());
    assertEquals(matchingAttributeIDs.get(0), "3");
    assertEquals(matchingAttributeIDs.get(1), "5");
    assertFalse(matchingAttributeIDs.contains("1"));
    List<Explanation> explanations = attributeIds.stream().map(id -> explainService.explain(query, entityTypeDynamic, id)).collect(toList());
    List<Float> scores = explanations.stream().map(Explanation::getValue).collect(toList());
    // FIXME these scores vary between runs
    // assertEquals(scores, asList(0.3463153, 0, 0.7889965, 1.7814579, 0.76421005, 1.0707202));
    Map<String, String> expandedQueryMap = new HashMap<>();
    for (String term : asList("hypertens", "disord vascular hypertens", "increased pressur blood", "high pressur blood", "ocular hypertens", "hypertens idiopathic pulmonary")) {
        expandedQueryMap.put(term, "hypertension");
    }
    List<Set<ExplainedQueryString>> explanationStrings = explanations.stream().map(explanation -> explainService.findQueriesFromExplanation(expandedQueryMap, explanation)).collect(toList());
    List<Set<ExplainedQueryString>> expectedExplanationStrings = asList(// High chance of pulmonary disease
    singleton(ExplainedQueryString.create("high", "high pressur blood", "hypertension", 41.66666666666667)), // And now for something completely different...
    emptySet(), // Are you taking hypertensive medication?
    singleton(ExplainedQueryString.create("hypertens", "hypertens", "hypertension", 100.0)), // Have you ever had high blood pressure? (Repeat) (1)
    singleton(ExplainedQueryString.create("high pressur blood", "high pressur blood", "hypertension", 100.0)), // Do you suffer from Ocular hypertension?
    singleton(ExplainedQueryString.create("ocular hypertens", "ocular hypertens", "hypertension", 100.0)), // Do you have a vascular disorder?
    singleton(ExplainedQueryString.create("disord vascular", "disord vascular hypertens", "hypertension", 78.04878048780488)));
    assertEquals(explanationStrings, expectedExplanationStrings);
}
Also used : DataProvider(org.testng.annotations.DataProvider) Operator(org.molgenis.data.QueryRule.Operator) MolgenisDateFormat.parseLocalDate(org.molgenis.data.util.MolgenisDateFormat.parseLocalDate) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) QueryImpl(org.molgenis.data.support.QueryImpl) AfterMethod(org.testng.annotations.AfterMethod) Supplier(java.util.function.Supplier) Assert(org.testng.Assert) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) ElasticsearchService(org.molgenis.data.elasticsearch.ElasticsearchService) AbstractTestNGSpringContextTests(org.springframework.test.context.testng.AbstractTestNGSpringContextTests) EntityTestHarness(org.molgenis.data.EntityTestHarness) Explanation(org.apache.lucene.search.Explanation) org.molgenis.data(org.molgenis.data) ElasticSearchExplainService(org.molgenis.semanticsearch.explain.service.ElasticSearchExplainService) MolgenisDateFormat.parseInstant(org.molgenis.data.util.MolgenisDateFormat.parseInstant) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) EntityType(org.molgenis.data.meta.model.EntityType) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) ContextConfiguration(org.springframework.test.context.ContextConfiguration) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) Collections(java.util.Collections) Set(java.util.Set) HashMap(java.util.HashMap) Explanation(org.apache.lucene.search.Explanation) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) QueryImpl(org.molgenis.data.support.QueryImpl) 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