Search in sources :

Example 21 with QueryRule

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

the class SemanticSearchServiceHelperTest method testCreateShouldQueryRule.

@Test
public void testCreateShouldQueryRule() {
    String multiOntologyTermIri = "http://www.molgenis.org/1,http://www.molgenis.org/2";
    OntologyTerm ontologyTerm_1 = OntologyTerm.create("http://www.molgenis.org/1", "molgenis label in the gcc");
    OntologyTerm ontologyTerm_2 = OntologyTerm.create("http://www.molgenis.org/2", "molgenis label 2 in the genetics", singletonList("label 2"));
    when(ontologyService.getOntologyTerm(ontologyTerm_1.getIRI())).thenReturn(ontologyTerm_1);
    when(ontologyService.getOntologyTerm(ontologyTerm_2.getIRI())).thenReturn(ontologyTerm_2);
    QueryRule actualShouldQueryRule = semanticSearchServiceHelper.createShouldQueryRule(multiOntologyTermIri);
    String expectedShouldQueryRuleToString = "SHOULD (DIS_MAX ('label' FUZZY_MATCH 'gcc molgenis label', 'description' FUZZY_MATCH 'gcc molgenis label'), DIS_MAX ('label' FUZZY_MATCH '2 label', 'description' FUZZY_MATCH '2 label', 'label' FUZZY_MATCH '2 genetics molgenis label', 'description' FUZZY_MATCH '2 genetics molgenis label'))";
    assertEquals(actualShouldQueryRule.toString(), expectedShouldQueryRuleToString);
    assertEquals(actualShouldQueryRule.getOperator(), QueryRule.Operator.SHOULD);
}
Also used : QueryRule(org.molgenis.data.QueryRule) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 22 with QueryRule

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

the class SemanticSearchServiceHelperTest method testCreateTargetAttributeQueryTerms.

@Test
public void testCreateTargetAttributeQueryTerms() {
    Attribute targetAttribute_1 = attrMetaFactory.create().setName("targetAttribute 1");
    targetAttribute_1.setDescription("Height");
    Attribute targetAttribute_2 = attrMetaFactory.create().setName("targetAttribute 2");
    targetAttribute_2.setLabel("Height");
    Multimap<Relation, OntologyTerm> tags = LinkedHashMultimap.create();
    OntologyTerm ontologyTerm1 = OntologyTerm.create("http://onto/standingheight", "Standing height", "Description is not used", singletonList("body_length"));
    OntologyTerm ontologyTerm2 = OntologyTerm.create("http://onto/sittingheight", "Sitting height", "Description is not used", singletonList("sitting_length"));
    OntologyTerm ontologyTerm3 = OntologyTerm.create("http://onto/height", "Height", "Description is not used", singletonList("sature"));
    tags.put(Relation.isAssociatedWith, ontologyTerm1);
    tags.put(Relation.isRealizationOf, ontologyTerm2);
    tags.put(Relation.isDefinedBy, ontologyTerm3);
    // Case 1
    QueryRule actualTargetAttributeQueryTerms_1 = semanticSearchServiceHelper.createDisMaxQueryRuleForAttribute(Sets.newLinkedHashSet(asList("targetAttribute 1", "Height")), tags.values());
    String expecteddisMaxQueryRuleToString_1 = "DIS_MAX ('label' FUZZY_MATCH '1 targetattribute', 'description' FUZZY_MATCH '1 targetattribute', 'label' FUZZY_MATCH 'height', 'description' FUZZY_MATCH 'height', 'label' FUZZY_MATCH 'length body', 'description' FUZZY_MATCH 'length body', 'label' FUZZY_MATCH 'standing height', 'description' FUZZY_MATCH 'standing height', 'label' FUZZY_MATCH 'length sitting', 'description' FUZZY_MATCH 'length sitting', 'label' FUZZY_MATCH 'sitting height', 'description' FUZZY_MATCH 'sitting height', 'label' FUZZY_MATCH 'sature', 'description' FUZZY_MATCH 'sature', 'label' FUZZY_MATCH 'height', 'description' FUZZY_MATCH 'height')";
    assertEquals(actualTargetAttributeQueryTerms_1.toString(), expecteddisMaxQueryRuleToString_1);
    // Case 2
    QueryRule expecteddisMaxQueryRuleToString_2 = semanticSearchServiceHelper.createDisMaxQueryRuleForAttribute(Sets.newHashSet("Height"), tags.values());
    String expectedTargetAttributeQueryTermsToString_2 = "DIS_MAX ('label' FUZZY_MATCH 'height', 'description' FUZZY_MATCH 'height', 'label' FUZZY_MATCH 'length body', 'description' FUZZY_MATCH 'length body', 'label' FUZZY_MATCH 'standing height', 'description' FUZZY_MATCH 'standing height', 'label' FUZZY_MATCH 'length sitting', 'description' FUZZY_MATCH 'length sitting', 'label' FUZZY_MATCH 'sitting height', 'description' FUZZY_MATCH 'sitting height', 'label' FUZZY_MATCH 'sature', 'description' FUZZY_MATCH 'sature', 'label' FUZZY_MATCH 'height', 'description' FUZZY_MATCH 'height')";
    assertEquals(expecteddisMaxQueryRuleToString_2.toString(), expectedTargetAttributeQueryTermsToString_2);
    // Case 3
    QueryRule expecteddisMaxQueryRuleToString_3 = semanticSearchServiceHelper.createDisMaxQueryRuleForAttribute(Sets.newHashSet("targetAttribute 3"), tags.values());
    String expectedTargetAttributeQueryTermsToString_3 = "DIS_MAX ('label' FUZZY_MATCH '3 targetattribute', 'description' FUZZY_MATCH '3 targetattribute', 'label' FUZZY_MATCH 'length body', 'description' FUZZY_MATCH 'length body', 'label' FUZZY_MATCH 'standing height', 'description' FUZZY_MATCH 'standing height', 'label' FUZZY_MATCH 'length sitting', 'description' FUZZY_MATCH 'length sitting', 'label' FUZZY_MATCH 'sitting height', 'description' FUZZY_MATCH 'sitting height', 'label' FUZZY_MATCH 'sature', 'description' FUZZY_MATCH 'sature', 'label' FUZZY_MATCH 'height', 'description' FUZZY_MATCH 'height')";
    assertEquals(expecteddisMaxQueryRuleToString_3.toString(), expectedTargetAttributeQueryTermsToString_3);
}
Also used : Relation(org.molgenis.data.semantic.Relation) QueryRule(org.molgenis.data.QueryRule) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 23 with QueryRule

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

the class SemanticSearchServiceHelper method createShouldQueryRule.

/**
 * Create a boolean should query for composite tags containing multiple ontology terms
 *
 * @return return a boolean should queryRule
 */
public QueryRule createShouldQueryRule(String multiOntologyTermIri) {
    QueryRule shouldQueryRule = new QueryRule(new ArrayList<>());
    shouldQueryRule.setOperator(Operator.SHOULD);
    for (String ontologyTermIri : multiOntologyTermIri.split(COMMA_CHAR)) {
        OntologyTerm ontologyTerm = ontologyService.getOntologyTerm(ontologyTermIri);
        List<String> queryTerms = parseOntologyTermQueries(ontologyTerm);
        Double termFrequency = getBestInverseDocumentFrequency(queryTerms);
        shouldQueryRule.getNestedRules().add(createBoostedDisMaxQueryRuleForTerms(queryTerms, termFrequency));
    }
    return shouldQueryRule;
}
Also used : QueryRule(org.molgenis.data.QueryRule) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm)

Example 24 with QueryRule

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

the class SemanticSearchServiceHelper method createDisMaxQueryRuleForTerms.

/**
 * Create disMaxJunc query rule based a list of queryTerm. All queryTerms are lower cased and stop words are removed
 *
 * @return disMaxJunc queryRule
 */
public QueryRule createDisMaxQueryRuleForTerms(List<String> queryTerms) {
    List<QueryRule> rules = new ArrayList<>();
    queryTerms.stream().filter(StringUtils::isNotEmpty).map(this::escapeCharsExcludingCaretChar).forEach(query -> {
        rules.add(new QueryRule(AttributeMetadata.LABEL, Operator.FUZZY_MATCH, query));
        rules.add(new QueryRule(AttributeMetadata.DESCRIPTION, Operator.FUZZY_MATCH, query));
    });
    QueryRule finalDisMaxQuery = new QueryRule(rules);
    finalDisMaxQuery.setOperator(Operator.DIS_MAX);
    return finalDisMaxQuery;
}
Also used : StringUtils(org.apache.commons.lang3.StringUtils) QueryRule(org.molgenis.data.QueryRule)

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