diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/search/SearchClient.java b/openmetadata-service/src/main/java/org/openmetadata/service/search/SearchClient.java index 534185b6e1bf..5f165328f330 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/search/SearchClient.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/search/SearchClient.java @@ -36,12 +36,17 @@ public interface SearchClient { String REMOVE_PROPAGATED_ENTITY_REFERENCE_FIELD_SCRIPT = "if((ctx._source.%s != null) && (ctx._source.%s.id == '%s')){ ctx._source.remove('%s')}"; + String REMOVE_PROPAGATED_FIELD_SCRIPT = "ctx._source.remove('%s')"; String UPDATE_PROPAGATED_ENTITY_REFERENCE_FIELD_SCRIPT = "if((ctx._source.%s == null) || (ctx._source.%s.id == '%s')) { ctx._source.put('%s', params)}"; String SOFT_DELETE_RESTORE_SCRIPT = "ctx._source.put('deleted', '%s')"; String REMOVE_TAGS_CHILDREN_SCRIPT = "for (int i = 0; i < ctx._source.tags.length; i++) { if (ctx._source.tags[i].tagFQN == '%s') { ctx._source.tags.remove(i) }}"; + String ADD_GLOSSARY_TAGS = + "for (int i = 0; i < ctx._source.tags.length; i++) { if (ctx._source.tags[i].tagFQN == '%s') { ctx._source.tags.addAll(params.tags) }}"; + String REMOVE_GLOSSARY_TAGS = + "for (int i = 0; i < ctx._source.tags.length; i++) { if (ctx._source.tags[i].tagFQN == '%s') { ctx._source.tags.removeAll(params.tags) }}"; String REMOVE_TEST_SUITE_CHILDREN_SCRIPT = "for (int i = 0; i < ctx._source.testSuites.length; i++) { if (ctx._source.testSuites[i].id == '%s') { ctx._source.testSuites.remove(i) }}"; diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/search/SearchRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/search/SearchRepository.java index f1e014453397..8da7814c25c8 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/search/SearchRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/search/SearchRepository.java @@ -8,6 +8,7 @@ import static org.openmetadata.service.Entity.RAW_COST_ANALYSIS_REPORT_DATA; import static org.openmetadata.service.Entity.WEB_ANALYTIC_ENTITY_VIEW_REPORT_DATA; import static org.openmetadata.service.Entity.WEB_ANALYTIC_USER_ACTIVITY_REPORT_DATA; +import static org.openmetadata.service.search.SearchClient.ADD_GLOSSARY_TAGS; import static org.openmetadata.service.search.SearchClient.DEFAULT_UPDATE_SCRIPT; import static org.openmetadata.service.search.SearchClient.GLOBAL_SEARCH_ALIAS; import static org.openmetadata.service.search.SearchClient.PROPAGATE_ENTITY_REFERENCE_FIELD_SCRIPT; @@ -33,6 +34,8 @@ import java.util.Objects; import java.util.Set; import java.util.TreeMap; +import java.util.stream.Collectors; +import javax.json.Json; import javax.json.JsonObject; import javax.ws.rs.core.Response; import lombok.Getter; @@ -49,6 +52,7 @@ import org.openmetadata.schema.type.ChangeDescription; import org.openmetadata.schema.type.EntityReference; import org.openmetadata.schema.type.FieldChange; +import org.openmetadata.schema.type.TagLabel; import org.openmetadata.schema.type.UsageDetails; import org.openmetadata.service.Entity; import org.openmetadata.service.exception.UnhandledServerException; @@ -71,6 +75,8 @@ public class SearchRepository { private final List inheritableFields = List.of(Entity.FIELD_OWNER, Entity.FIELD_DOMAIN, Entity.FIELD_DISABLED); + private final List propagateFields = List.of(Entity.FIELD_TAGS); + @Getter private final ElasticSearchConfiguration elasticSearchConfiguration; public final List dataInsightReports = @@ -254,6 +260,7 @@ public void updateEntity(EntityInterface entity) { } searchClient.updateEntity(indexMapping.getIndexName(), entityId, doc, scriptTxt); propagateInheritedFieldsToChildren(entityType, entityId, entity.getChangeDescription(), indexMapping); + propagateGlossaryTags(entityType, entity.getFullyQualifiedName(), entity.getChangeDescription(), indexMapping); } catch (Exception ie) { LOG.error( String.format( @@ -294,6 +301,23 @@ public void propagateInheritedFieldsToChildren( } } + public void propagateGlossaryTags(String entityType, String glossaryFQN, ChangeDescription changeDescription, + IndexMapping indexMapping) { + StringBuilder scriptTxt = new StringBuilder(); + Map fieldData = new HashMap<>(); + if (changeDescription != null && entityType.equalsIgnoreCase(Entity.GLOSSARY_TERM) ) { + for (FieldChange field : changeDescription.getFieldsAdded()) { + if (propagateFields.contains(field.getName())) { + try { + List tagLabels = JsonUtils.readObjects(field.getNewValue().toString(), TagLabel.class); + scriptTxt.append(String.format(ADD_GLOSSARY_TAGS, glossaryFQN)); + fieldData = + } + } + } + } + } + private Pair> getInheritedFieldChanges(ChangeDescription changeDescription) { StringBuilder scriptTxt = new StringBuilder(); Map fieldData = new HashMap<>();