@Test void testFindAttributes() { EntityType sourceEntityType = entityTypeFactory.create("sourceEntityType"); List<String> attributeIdentifiers = asList("1", "2"); when(semanticSearchServiceHelper.getAttributeIdentifiers(sourceEntityType)) .thenReturn(attributeIdentifiers); List<QueryRule> rules = newArrayList(); QueryRule targetQueryRuleLabel = new QueryRule(AttributeMetadata.LABEL, QueryRule.Operator.FUZZY_MATCH, "height"); rules.add(targetQueryRuleLabel); QueryRule targetQueryRuleOntologyTermTag = new QueryRule(AttributeMetadata.LABEL, QueryRule.Operator.FUZZY_MATCH, "standing height"); rules.add(targetQueryRuleOntologyTermTag); QueryRule targetQueryRuleOntologyTermTagSyn = new QueryRule(AttributeMetadata.LABEL, QueryRule.Operator.FUZZY_MATCH, "length"); rules.add(targetQueryRuleOntologyTermTagSyn); QueryRule disMaxQueryRule = new QueryRule(rules); disMaxQueryRule.setOperator(QueryRule.Operator.DIS_MAX); when(semanticSearchServiceHelper.createDisMaxQueryRuleForAttribute( newHashSet("targetAttribute"), emptyList())) .thenReturn(disMaxQueryRule); Entity entity1 = mock(Entity.class); when(entity1.getString(AttributeMetadata.NAME)).thenReturn("height_0"); when(entity1.getString(AttributeMetadata.LABEL)).thenReturn("height"); when(entity1.getString(AttributeMetadata.DESCRIPTION)) .thenReturn("this is a height measurement in m!"); List<QueryRule> disMaxQueryRules = newArrayList( new QueryRule(AttributeMetadata.ID, QueryRule.Operator.IN, attributeIdentifiers), new QueryRule(QueryRule.Operator.AND), disMaxQueryRule); Attribute attributeHeight = attrMetaDataFactory.create().setName("height_0"); Attribute attributeWeight = attrMetaDataFactory.create().setName("weight_0"); sourceEntityType.addAttribute(attributeHeight); sourceEntityType.addAttribute(attributeWeight); when(dataService.findAll(ATTRIBUTE_META_DATA, new QueryImpl<>(disMaxQueryRules))) .thenReturn(Stream.of(entity1)); Hits<ExplainedAttribute> termsActual1 = semanticSearchService.findAttributes( sourceEntityType, newHashSet("targetAttribute"), emptyList()); Hits<ExplainedAttribute> termsExpected1 = Hits.create(Hit.create(ExplainedAttribute.create(attributeHeight, emptySet(), false), 1f)); assertEquals(termsExpected1.toString(), termsActual1.toString()); when(dataService.findAll(ATTRIBUTE_META_DATA, new QueryImpl<>(disMaxQueryRules))) .thenReturn(Stream.empty()); Hits<ExplainedAttribute> termsActual2 = semanticSearchService.findAttributes( sourceEntityType, newHashSet("targetAttribute"), emptyList()); Hits<ExplainedAttribute> termsExpected2 = Hits.create(); assertEquals(termsExpected2, termsActual2); 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(create(standingHeight, 0.76471f), result); }
public Hits<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().isEmpty()) { 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); List<ExplainedAttribute> attributeSearchHits = new ArrayList<>(); AtomicInteger count = new AtomicInteger(0); attributeEntities.forEach( attributeEntity -> { Attribute attribute = sourceEntityType.getAttribute(attributeEntity.getString(AttributeMetadata.NAME)); Set<ExplainedQueryString> explainedQueryStrings; boolean isHighQuality; if (count.get() < MAX_NUMBER_EXPLAINED_ATTRIBUTES) { explainedQueryStrings = convertAttributeToExplainedAttribute( attribute, collectExpanedQueryMap, new QueryImpl<>(finalQueryRules)); isHighQuality = isSingleMatchHighQuality( queryTerms, Sets.newHashSet(collectExpanedQueryMap.values()), explainedQueryStrings); } else { explainedQueryStrings = emptySet(); isHighQuality = false; } attributeSearchHits.add( ExplainedAttribute.create(attribute, explainedQueryStrings, isHighQuality)); count.incrementAndGet(); }); return Hits.create( attributeSearchHits.stream() .map(explainedAttribute -> Hit.create(explainedAttribute, 1f)) .collect(toList())); }
SemanticSearchServiceImpl implements SemanticSearchService { public Hits<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().isEmpty()) { 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); List<ExplainedAttribute> attributeSearchHits = new ArrayList<>(); AtomicInteger count = new AtomicInteger(0); attributeEntities.forEach( attributeEntity -> { Attribute attribute = sourceEntityType.getAttribute(attributeEntity.getString(AttributeMetadata.NAME)); Set<ExplainedQueryString> explainedQueryStrings; boolean isHighQuality; if (count.get() < MAX_NUMBER_EXPLAINED_ATTRIBUTES) { explainedQueryStrings = convertAttributeToExplainedAttribute( attribute, collectExpanedQueryMap, new QueryImpl<>(finalQueryRules)); isHighQuality = isSingleMatchHighQuality( queryTerms, Sets.newHashSet(collectExpanedQueryMap.values()), explainedQueryStrings); } else { explainedQueryStrings = emptySet(); isHighQuality = false; } attributeSearchHits.add( ExplainedAttribute.create(attribute, explainedQueryStrings, isHighQuality)); count.incrementAndGet(); }); return Hits.create( attributeSearchHits.stream() .map(explainedAttribute -> Hit.create(explainedAttribute, 1f)) .collect(toList())); } }
SemanticSearchServiceImpl implements SemanticSearchService { public Hits<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().isEmpty()) { 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); List<ExplainedAttribute> attributeSearchHits = new ArrayList<>(); AtomicInteger count = new AtomicInteger(0); attributeEntities.forEach( attributeEntity -> { Attribute attribute = sourceEntityType.getAttribute(attributeEntity.getString(AttributeMetadata.NAME)); Set<ExplainedQueryString> explainedQueryStrings; boolean isHighQuality; if (count.get() < MAX_NUMBER_EXPLAINED_ATTRIBUTES) { explainedQueryStrings = convertAttributeToExplainedAttribute( attribute, collectExpanedQueryMap, new QueryImpl<>(finalQueryRules)); isHighQuality = isSingleMatchHighQuality( queryTerms, Sets.newHashSet(collectExpanedQueryMap.values()), explainedQueryStrings); } else { explainedQueryStrings = emptySet(); isHighQuality = false; } attributeSearchHits.add( ExplainedAttribute.create(attribute, explainedQueryStrings, isHighQuality)); count.incrementAndGet(); }); return Hits.create( attributeSearchHits.stream() .map(explainedAttribute -> Hit.create(explainedAttribute, 1f)) .collect(toList())); } SemanticSearchServiceImpl(
      DataService dataService,
      OntologyService ontologyService,
      SemanticSearchServiceHelper semanticSearchServiceHelper,
      ElasticSearchExplainService elasticSearchExplainService,
      OntologyTagService ontologyTagService); }
SemanticSearchServiceImpl implements SemanticSearchService { public Hits<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().isEmpty()) { 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); List<ExplainedAttribute> attributeSearchHits = new ArrayList<>(); AtomicInteger count = new AtomicInteger(0); attributeEntities.forEach( attributeEntity -> { Attribute attribute = sourceEntityType.getAttribute(attributeEntity.getString(AttributeMetadata.NAME)); Set<ExplainedQueryString> explainedQueryStrings; boolean isHighQuality; if (count.get() < MAX_NUMBER_EXPLAINED_ATTRIBUTES) { explainedQueryStrings = convertAttributeToExplainedAttribute( attribute, collectExpanedQueryMap, new QueryImpl<>(finalQueryRules)); isHighQuality = isSingleMatchHighQuality( queryTerms, Sets.newHashSet(collectExpanedQueryMap.values()), explainedQueryStrings); } else { explainedQueryStrings = emptySet(); isHighQuality = false; } attributeSearchHits.add( ExplainedAttribute.create(attribute, explainedQueryStrings, isHighQuality)); count.incrementAndGet(); }); return Hits.create( attributeSearchHits.stream() .map(explainedAttribute -> Hit.create(explainedAttribute, 1f)) .collect(toList())); } SemanticSearchServiceImpl(
      DataService dataService,
      OntologyService ontologyService,
      SemanticSearchServiceHelper semanticSearchServiceHelper,
      ElasticSearchExplainService elasticSearchExplainService,
      OntologyTagService ontologyTagService); Hits<ExplainedAttribute> findAttributes(
      EntityType sourceEntityType, Set<String> queryTerms, Collection<OntologyTerm> ontologyTerms); @Override EntityTypeSearchResults findAttributes(
      EntityType sourceEntityType, EntityType targetEntityType, Set<String> searchTerms); @Override AttributeSearchResults findAttributes(
      EntityType sourceEntityType,
      EntityType targetEntityType,
      Attribute targetAttribute,
      Set<String> searchTerms); Set<String> createLexicalSearchQueryTerms(
      Attribute targetAttribute, Set<String> searchTerms); Set<ExplainedQueryString> convertAttributeToExplainedAttribute(
      Attribute attribute, Map<String, String> collectExpandedQueryMap, Query<Entity> query); @Override Hits<OntologyTerm> findOntologyTerms(
      Attribute attribute, Collection<Ontology> ontologies); }
SemanticSearchServiceImpl implements SemanticSearchService { public Hits<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().isEmpty()) { 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); List<ExplainedAttribute> attributeSearchHits = new ArrayList<>(); AtomicInteger count = new AtomicInteger(0); attributeEntities.forEach( attributeEntity -> { Attribute attribute = sourceEntityType.getAttribute(attributeEntity.getString(AttributeMetadata.NAME)); Set<ExplainedQueryString> explainedQueryStrings; boolean isHighQuality; if (count.get() < MAX_NUMBER_EXPLAINED_ATTRIBUTES) { explainedQueryStrings = convertAttributeToExplainedAttribute( attribute, collectExpanedQueryMap, new QueryImpl<>(finalQueryRules)); isHighQuality = isSingleMatchHighQuality( queryTerms, Sets.newHashSet(collectExpanedQueryMap.values()), explainedQueryStrings); } else { explainedQueryStrings = emptySet(); isHighQuality = false; } attributeSearchHits.add( ExplainedAttribute.create(attribute, explainedQueryStrings, isHighQuality)); count.incrementAndGet(); }); return Hits.create( attributeSearchHits.stream() .map(explainedAttribute -> Hit.create(explainedAttribute, 1f)) .collect(toList())); } SemanticSearchServiceImpl(
      DataService dataService,
      OntologyService ontologyService,
      SemanticSearchServiceHelper semanticSearchServiceHelper,
      ElasticSearchExplainService elasticSearchExplainService,
      OntologyTagService ontologyTagService); Hits<ExplainedAttribute> findAttributes(
      EntityType sourceEntityType, Set<String> queryTerms, Collection<OntologyTerm> ontologyTerms); @Override EntityTypeSearchResults findAttributes(
      EntityType sourceEntityType, EntityType targetEntityType, Set<String> searchTerms); @Override AttributeSearchResults findAttributes(
      EntityType sourceEntityType,
      EntityType targetEntityType,
      Attribute targetAttribute,
      Set<String> searchTerms); Set<String> createLexicalSearchQueryTerms(
      Attribute targetAttribute, Set<String> searchTerms); Set<ExplainedQueryString> convertAttributeToExplainedAttribute(
      Attribute attribute, Map<String, String> collectExpandedQueryMap, Query<Entity> query); @Override Hits<OntologyTerm> findOntologyTerms(
      Attribute attribute, Collection<Ontology> ontologies);  }