Skip to content

Commit

Permalink
Minor: ES propagate glossary tags
Browse files Browse the repository at this point in the history
  • Loading branch information
harshach committed Dec 7, 2023
1 parent ce72fff commit dfb1302
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) }}";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -71,6 +75,8 @@ public class SearchRepository {
private final List<String> inheritableFields =
List.of(Entity.FIELD_OWNER, Entity.FIELD_DOMAIN, Entity.FIELD_DISABLED);

private final List<String> propagateFields = List.of(Entity.FIELD_TAGS);

@Getter private final ElasticSearchConfiguration elasticSearchConfiguration;

public final List<String> dataInsightReports =
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -294,6 +301,23 @@ public void propagateInheritedFieldsToChildren(
}
}

public void propagateGlossaryTags(String entityType, String glossaryFQN, ChangeDescription changeDescription,
IndexMapping indexMapping) {
StringBuilder scriptTxt = new StringBuilder();
Map<String, Object> fieldData = new HashMap<>();
if (changeDescription != null && entityType.equalsIgnoreCase(Entity.GLOSSARY_TERM) ) {
for (FieldChange field : changeDescription.getFieldsAdded()) {
if (propagateFields.contains(field.getName())) {
try {
List<TagLabel> tagLabels = JsonUtils.readObjects(field.getNewValue().toString(), TagLabel.class);
scriptTxt.append(String.format(ADD_GLOSSARY_TAGS, glossaryFQN));
fieldData =
}
}
}
}
}

private Pair<String, Map<String, Object>> getInheritedFieldChanges(ChangeDescription changeDescription) {
StringBuilder scriptTxt = new StringBuilder();
Map<String, Object> fieldData = new HashMap<>();
Expand Down

0 comments on commit dfb1302

Please sign in to comment.