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);
}
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);
}
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;
}
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;
}
Aggregations