/** * Create a boolean should query for composite tags containing multiple ontology terms * * @param multiOntologyTermIri * @return return a boolean should queryRule */ public QueryRule createShouldQueryRule(String multiOntologyTermIri) { QueryRule shouldQueryRule = new QueryRule(new ArrayList<QueryRule>()); shouldQueryRule.setOperator(Operator.SHOULD); for (String ontologyTermIri : multiOntologyTermIri.split(",")) { OntologyTerm ontologyTerm = ontologyService.getOntologyTerm(ontologyTermIri); List<String> queryTerms = parseOntologyTermQueries(ontologyTerm); Double termFrequency = termFrequencyService.getTermFrequency(ontologyTerm.getLabel()); shouldQueryRule .getNestedRules() .add(createDisMaxQueryRuleForTermsWithBoost(queryTerms, termFrequency)); } return shouldQueryRule; }
/** * Create disMaxJunc query rule based a list of queryTerm. All queryTerms are lower cased and stop * words are removed * * @param queryTerms * @return disMaxJunc queryRule */ public QueryRule createDisMaxQueryRuleForTerms(List<String> queryTerms) { List<QueryRule> rules = new ArrayList<QueryRule>(); queryTerms .stream() .filter(query -> StringUtils.isNotEmpty(query)) .map(QueryParser::escape) .map(this::reverseEscapeLuceneChar) .forEach( query -> { rules.add( new QueryRule(AttributeMetaDataMetaData.LABEL, Operator.FUZZY_MATCH, query)); rules.add( new QueryRule( AttributeMetaDataMetaData.DESCRIPTION, Operator.FUZZY_MATCH, query)); }); QueryRule finalDisMaxQuery = new QueryRule(rules); finalDisMaxQuery.setOperator(Operator.DIS_MAX); return finalDisMaxQuery; }
@Test public void testFindAttributes() { DefaultEntityMetaData sourceEntityMetaData = new DefaultEntityMetaData("sourceEntityMetaData"); EntityMetaData targetEntityMetaData = new DefaultEntityMetaData("targetEntityMetaData"); DefaultAttributeMetaData targetAttribute = new DefaultAttributeMetaData("targetAttribute"); // Mock the id's of the attribute entities that should be searched List<String> attributeIdentifiers = Arrays.asList("1", "2"); when(semanticSearchServiceHelper.getAttributeIdentifiers(sourceEntityMetaData)) .thenReturn(attributeIdentifiers); // Mock the createDisMaxQueryRule method List<QueryRule> rules = new ArrayList<QueryRule>(); QueryRule targetQueryRuleLabel = new QueryRule(AttributeMetaDataMetaData.LABEL, Operator.FUZZY_MATCH, "height"); rules.add(targetQueryRuleLabel); QueryRule targetQueryRuleOntologyTermTag = new QueryRule(AttributeMetaDataMetaData.LABEL, Operator.FUZZY_MATCH, "standing height"); rules.add(targetQueryRuleOntologyTermTag); QueryRule targetQueryRuleOntologyTermTagSyn = new QueryRule(AttributeMetaDataMetaData.LABEL, Operator.FUZZY_MATCH, "length"); rules.add(targetQueryRuleOntologyTermTagSyn); QueryRule disMaxQueryRule = new QueryRule(rules); disMaxQueryRule.setOperator(Operator.DIS_MAX); when(semanticSearchServiceHelper.createDisMaxQueryRuleForAttribute( targetEntityMetaData, targetAttribute)) .thenReturn(disMaxQueryRule); MapEntity entity1 = new MapEntity( ImmutableMap.of( AttributeMetaDataMetaData.NAME, "height_0", AttributeMetaDataMetaData.LABEL, "height", AttributeMetaDataMetaData.DESCRIPTION, "this is a height measurement in m!")); List<Entity> attributeMetaDataEntities = Arrays.<Entity>asList(entity1); List<QueryRule> disMaxQueryRules = Lists.newArrayList( new QueryRule(AttributeMetaDataMetaData.IDENTIFIER, Operator.IN, attributeIdentifiers), new QueryRule(Operator.AND), disMaxQueryRule); AttributeMetaData attributeHeight = new DefaultAttributeMetaData("height_0"); AttributeMetaData attributeWeight = new DefaultAttributeMetaData("weight_0"); sourceEntityMetaData.addAttributeMetaData(attributeHeight); sourceEntityMetaData.addAttributeMetaData(attributeWeight); // Case 1 when(dataService.findAll( AttributeMetaDataMetaData.ENTITY_NAME, new QueryImpl(disMaxQueryRules))) .thenReturn(attributeMetaDataEntities); Iterable<AttributeMetaData> termsActual1 = semanticSearchService.findAttributes( sourceEntityMetaData, targetEntityMetaData, targetAttribute); Iterable<AttributeMetaData> termsExpected1 = Arrays.<AttributeMetaData>asList(attributeHeight); assertEquals(termsActual1, termsExpected1); // Case 2 when(dataService.findAll( AttributeMetaDataMetaData.ENTITY_NAME, new QueryImpl(disMaxQueryRules))) .thenReturn(Arrays.<Entity>asList()); Iterable<AttributeMetaData> termsActual2 = semanticSearchService.findAttributes( sourceEntityMetaData, targetEntityMetaData, targetAttribute); Iterable<AttributeMetaData> termsExpected2 = Arrays.<AttributeMetaData>asList(); assertEquals(termsActual2, termsExpected2); Mockito.reset(ontologyService); attribute.setDescription("Standing height (Ångstrøm)"); when(ontologyService.findOntologyTerms( ontologies, ImmutableSet.of("standing", "height", "ångstrøm"), 100)) .thenReturn(ontologyTerms); Hit<OntologyTerm> result = semanticSearchService.findTags(attribute, ontologies); assertEquals(result, Hit.<OntologyTerm>create(standingHeight, 0.76471f)); }