Search in sources :

Example 6 with QueryRule

use of org.molgenis.data.QueryRule in project molgenis by molgenis.

the class InformationContentServiceTest method createWordIDF.

@Test
public void createWordIDF() {
    String ontologyIri = "http://www.molgenis.org";
    Ontology ontology = ontologyFactory.create();
    ontology.setOntologyIri(ontologyIri);
    when(dataService.findOne(ONTOLOGY, new QueryImpl<>().eq(OntologyMetaData.ONTOLOGY_IRI, ontologyIri))).thenReturn(ontology);
    when(dataService.count(ONTOLOGY_TERM, new QueryImpl<>().eq(OntologyTermMetaData.ONTOLOGY, ontology))).thenReturn((long) 100);
    QueryRule queryRule = new QueryRule(singletonList(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, Operator.FUZZY_MATCH, "hear")));
    queryRule.setOperator(Operator.DIS_MAX);
    QueryRule finalQuery = new QueryRule(asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, Operator.EQUALS, ontology), new QueryRule(Operator.AND), queryRule));
    when(dataService.count(ONTOLOGY_TERM, new QueryImpl<>(finalQuery))).thenReturn((long) 30);
    QueryRule queryRule2 = new QueryRule(singletonList(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, Operator.FUZZY_MATCH, "impair")));
    queryRule2.setOperator(Operator.DIS_MAX);
    QueryRule finalQuery2 = new QueryRule(asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, Operator.EQUALS, ontology), new QueryRule(Operator.AND), queryRule2));
    when(dataService.count(ONTOLOGY_TERM, new QueryImpl<>(finalQuery2))).thenReturn((long) 10);
    Map<String, Double> expectedWordIDF = informationContentService.createWordIDF("hearing impairment", ontologyIri);
    Assert.assertEquals(expectedWordIDF.get("hear").intValue(), 2);
    Assert.assertEquals(expectedWordIDF.get("impair").intValue(), 3);
}
Also used : QueryImpl(org.molgenis.data.support.QueryImpl) Ontology(org.molgenis.ontology.core.meta.Ontology) QueryRule(org.molgenis.data.QueryRule) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 7 with QueryRule

use of org.molgenis.data.QueryRule in project molgenis by molgenis.

the class PostgreSqlRepositoryTest method countQueryOneToManyEquals.

// TODO test all query operators for one-to-many
@Test
public void countQueryOneToManyEquals() throws Exception {
    String oneToManyAttrName = "oneToManyAttr";
    String refIdAttrName = "refEntityId";
    Attribute refIdAttr = mock(Attribute.class);
    when(refIdAttr.getName()).thenReturn(refIdAttrName);
    when(refIdAttr.getDataType()).thenReturn(INT);
    String xrefAttrName = "xrefAttr";
    Attribute xrefAttr = mock(Attribute.class);
    when(xrefAttr.getName()).thenReturn(xrefAttrName);
    EntityType refEntityMeta = mock(EntityType.class);
    when(refEntityMeta.getId()).thenReturn("refEntityId");
    when(refEntityMeta.getIdAttribute()).thenReturn(refIdAttr);
    String idAttrName = "entityId";
    Attribute idAttr = mock(Attribute.class);
    when(idAttr.getName()).thenReturn(idAttrName);
    Attribute oneToManyAttr = mock(Attribute.class);
    when(oneToManyAttr.getName()).thenReturn(oneToManyAttrName);
    when(oneToManyAttr.getDataType()).thenReturn(ONE_TO_MANY);
    when(oneToManyAttr.getRefEntity()).thenReturn(refEntityMeta);
    when(oneToManyAttr.isMappedBy()).thenReturn(true);
    when(oneToManyAttr.getMappedBy()).thenReturn(xrefAttr);
    when(entityType.getId()).thenReturn("entityId");
    when(entityType.getIdAttribute()).thenReturn(idAttr);
    doReturn(oneToManyAttr).when(entityType).getAttribute(oneToManyAttrName);
    int queryValue = 2;
    QueryRule queryRule = new QueryRule(oneToManyAttrName, EQUALS, queryValue);
    when(query.getRules()).thenReturn(singletonList(queryRule));
    String sql = "SELECT COUNT(DISTINCT this.\"entityId\") FROM \"entityId#fc2928f6\" AS this LEFT JOIN \"refEntityId#07f902bf\" AS \"oneToManyAttr_filter1\" ON (this.\"entityId\" = \"oneToManyAttr_filter1\".\"xrefAttr\") WHERE \"oneToManyAttr_filter1\".\"refEntityId\" = ?";
    long count = 123L;
    when(jdbcTemplate.queryForObject(sql, new Object[] { queryValue }, Long.class)).thenReturn(count);
    assertEquals(postgreSqlRepo.count(query), count);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Attribute(org.molgenis.data.meta.model.Attribute) QueryRule(org.molgenis.data.QueryRule) Test(org.testng.annotations.Test)

Example 8 with QueryRule

use of org.molgenis.data.QueryRule in project molgenis by molgenis.

the class SemanticSearchServiceHelper method createDisMaxQueryRuleForAttribute.

/**
 * Create a disMaxJunc query rule based on the given search terms as well as the information from given ontology
 * terms
 *
 * @return disMaxJunc queryRule
 */
public QueryRule createDisMaxQueryRuleForAttribute(Set<String> searchTerms, Collection<OntologyTerm> ontologyTerms) {
    List<String> queryTerms = new ArrayList<>();
    if (searchTerms != null) {
        queryTerms.addAll(searchTerms.stream().filter(StringUtils::isNotBlank).map(this::processQueryString).collect(Collectors.toList()));
    }
    // Handle tags with only one ontologyterm
    ontologyTerms.stream().filter(ontologyTerm -> !ontologyTerm.getIRI().contains(COMMA_CHAR)).forEach(ot -> queryTerms.addAll(parseOntologyTermQueries(ot)));
    QueryRule disMaxQueryRule = createDisMaxQueryRuleForTerms(queryTerms);
    // Handle tags with multiple ontologyterms
    ontologyTerms.stream().filter(ontologyTerm -> ontologyTerm.getIRI().contains(COMMA_CHAR)).forEach(ot -> disMaxQueryRule.getNestedRules().add(createShouldQueryRule(ot.getIRI())));
    return disMaxQueryRule;
}
Also used : NGramDistanceAlgorithm(org.molgenis.semanticsearch.string.NGramDistanceAlgorithm) Stemmer(org.molgenis.semanticsearch.string.Stemmer) java.util(java.util) EntityTypeMetadata(org.molgenis.data.meta.model.EntityTypeMetadata) TermFrequencyService(org.molgenis.ontology.core.ic.TermFrequencyService) Operator(org.molgenis.data.QueryRule.Operator) QueryImpl(org.molgenis.data.support.QueryImpl) StringUtils(org.apache.commons.lang3.StringUtils) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ENTITY_TYPE_META_DATA(org.molgenis.data.meta.model.EntityTypeMetadata.ENTITY_TYPE_META_DATA) MolgenisDataAccessException(org.molgenis.data.MolgenisDataAccessException) COMPOUND(org.molgenis.data.meta.AttributeType.COMPOUND) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) Objects.requireNonNull(java.util.Objects.requireNonNull) DataService(org.molgenis.data.DataService) AttributeMetadata(org.molgenis.data.meta.model.AttributeMetadata) OntologyService(org.molgenis.ontology.core.service.OntologyService) Arrays.stream(java.util.Arrays.stream) QueryRule(org.molgenis.data.QueryRule) Entity(org.molgenis.data.Entity) QueryRule(org.molgenis.data.QueryRule)

Example 9 with QueryRule

use of org.molgenis.data.QueryRule in project molgenis by molgenis.

the class ExplainServiceHelperTest method testFindQuery.

@Test
public void testFindQuery() {
    QueryRule queryRule_1 = new QueryRule(AttributeMetadata.LABEL, Operator.FUZZY_MATCH, "hypertension");
    QueryRule queryRule_2 = new QueryRule(AttributeMetadata.LABEL, Operator.FUZZY_MATCH, "hypertensive disorder");
    QueryRule queryRule_3 = new QueryRule(AttributeMetadata.LABEL, Operator.FUZZY_MATCH, "high blood pressure");
    QueryRule queryRule_4 = new QueryRule(AttributeMetadata.LABEL, Operator.FUZZY_MATCH, "drug");
    QueryRule queryRule_5 = new QueryRule(AttributeMetadata.LABEL, Operator.FUZZY_MATCH, "medication");
    QueryRule queryRule_6 = new QueryRule(AttributeMetadata.LABEL, Operator.FUZZY_MATCH, "pill");
    QueryRule disMaxQueryRule_1 = new QueryRule(Arrays.asList(queryRule_1, queryRule_2, queryRule_3));
    disMaxQueryRule_1.setOperator(Operator.DIS_MAX);
    QueryRule disMaxQueryRule_2 = new QueryRule(Arrays.asList(queryRule_4, queryRule_5, queryRule_6));
    disMaxQueryRule_2.setOperator(Operator.DIS_MAX);
    QueryRule shouldQueryRule = new QueryRule(Arrays.asList(disMaxQueryRule_1, disMaxQueryRule_2));
    shouldQueryRule.setOperator(Operator.SHOULD);
    QueryRule finalDisMaxQueryRule = new QueryRule(Arrays.asList(shouldQueryRule));
    finalDisMaxQueryRule.setOperator(Operator.DIS_MAX);
    Map<String, String> expanedQueryMap = new HashMap<>();
    expanedQueryMap.put("hypertension", "hypertension");
    expanedQueryMap.put("hypertensive disorder", "hypertension");
    expanedQueryMap.put("high blood pressure", "hypertension");
    expanedQueryMap.put("medication", "medication");
    expanedQueryMap.put("drug", "medication");
    expanedQueryMap.put("pill", "medication");
    Map<String, Double> findMatchQueries = explainServiceHelper.findMatchQueries("blood high", expanedQueryMap);
    assertEquals(findMatchQueries.get("high blood pressure"), 73.333, 0.001);
}
Also used : HashMap(java.util.HashMap) QueryRule(org.molgenis.data.QueryRule) Test(org.testng.annotations.Test)

Example 10 with QueryRule

use of org.molgenis.data.QueryRule in project molgenis by molgenis.

the class SemanticSearchServiceImpl method findAttributes.

@Override
public Map<Attribute, ExplainedAttribute> findAttributes(EntityType sourceEntityType, Set<String> queryTerms, Collection<OntologyTerm> ontologyTerms) {
    Iterable<String> attributeIdentifiers = semanticSearchServiceHelper.getAttributeIdentifiers(sourceEntityType);
    QueryRule disMaxQueryRule = semanticSearchServiceHelper.createDisMaxQueryRuleForAttribute(queryTerms, ontologyTerms);
    List<QueryRule> finalQueryRules = Lists.newArrayList(new QueryRule(AttributeMetadata.ID, Operator.IN, attributeIdentifiers));
    if (disMaxQueryRule.getNestedRules().size() > 0) {
        finalQueryRules.addAll(Arrays.asList(new QueryRule(Operator.AND), disMaxQueryRule));
    }
    Stream<Entity> attributeEntities = dataService.findAll(ATTRIBUTE_META_DATA, new QueryImpl<>(finalQueryRules));
    Map<String, String> collectExpanedQueryMap = semanticSearchServiceHelper.collectExpandedQueryMap(queryTerms, ontologyTerms);
    // Because the explain-API can be computationally expensive we limit the explanation to the top 10 attributes
    Map<Attribute, ExplainedAttribute> explainedAttributes = new LinkedHashMap<>();
    AtomicInteger count = new AtomicInteger(0);
    attributeEntities.forEach(attributeEntity -> {
        Attribute attribute = sourceEntityType.getAttribute(attributeEntity.getString(AttributeMetadata.NAME));
        if (count.get() < MAX_NUMBER_EXPLAINED_ATTRIBUTES) {
            Set<ExplainedQueryString> explanations = convertAttributeToExplainedAttribute(attribute, collectExpanedQueryMap, new QueryImpl<>(finalQueryRules));
            boolean singleMatchHighQuality = isSingleMatchHighQuality(queryTerms, Sets.newHashSet(collectExpanedQueryMap.values()), explanations);
            explainedAttributes.put(attribute, ExplainedAttribute.create(attribute, explanations, singleMatchHighQuality));
        } else {
            explainedAttributes.put(attribute, ExplainedAttribute.create(attribute));
        }
        count.incrementAndGet();
    });
    return explainedAttributes;
}
Also used : Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryRule(org.molgenis.data.QueryRule) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString)

Aggregations

QueryRule (org.molgenis.data.QueryRule)24 Entity (org.molgenis.data.Entity)16 Test (org.testng.annotations.Test)15 QueryImpl (org.molgenis.data.support.QueryImpl)7 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)5 Attribute (org.molgenis.data.meta.model.Attribute)4 EntityType (org.molgenis.data.meta.model.EntityType)4 OntologyTerm (org.molgenis.ontology.core.model.OntologyTerm)4 StringUtils (org.apache.commons.lang3.StringUtils)3 Operator (org.molgenis.data.QueryRule.Operator)3 OntologyTermHitEntity (org.molgenis.ontology.sorta.bean.OntologyTermHitEntity)3 Sets (com.google.common.collect.Sets)2 java.util (java.util)2 List (java.util.List)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Collectors (java.util.stream.Collectors)2 DataService (org.molgenis.data.DataService)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 FluentIterable (com.google.common.collect.FluentIterable)1 Iterables (com.google.common.collect.Iterables)1