All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openmetadata.service.search.indexes.TopicIndex Maven / Gradle / Ivy

There is a newer version: 1.5.11
Show newest version
package org.openmetadata.service.search.indexes;

import static org.openmetadata.service.search.EntityBuilderConstant.ES_MESSAGE_SCHEMA_FIELD;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import org.openmetadata.schema.entity.data.Topic;
import org.openmetadata.schema.type.Field;
import org.openmetadata.schema.type.TagLabel;
import org.openmetadata.service.Entity;
import org.openmetadata.service.search.ParseTags;
import org.openmetadata.service.search.models.FlattenSchemaField;
import org.openmetadata.service.search.models.SearchSuggest;
import org.openmetadata.service.util.FullyQualifiedName;

public class TopicIndex implements SearchIndex {
  final Set excludeTopicFields = Set.of("sampleData");
  final Topic topic;

  public TopicIndex(Topic topic) {
    this.topic = topic;
  }

  @Override
  public List getSuggest() {
    List suggest = new ArrayList<>();
    suggest.add(SearchSuggest.builder().input(topic.getFullyQualifiedName()).weight(5).build());
    suggest.add(SearchSuggest.builder().input(topic.getName()).weight(10).build());
    return suggest;
  }

  @Override
  public Object getEntity() {
    return topic;
  }

  @Override
  public Set getExcludedFields() {
    return excludeTopicFields;
  }

  public Map buildSearchIndexDocInternal(Map doc) {
    List fieldSuggest = new ArrayList<>();
    List serviceSuggest = new ArrayList<>();
    Set> tagsWithChildren = new HashSet<>();
    List fieldsWithChildrenName = new ArrayList<>();
    serviceSuggest.add(
        SearchSuggest.builder().input(topic.getService().getName()).weight(5).build());

    if (topic.getMessageSchema() != null
        && topic.getMessageSchema().getSchemaFields() != null
        && !topic.getMessageSchema().getSchemaFields().isEmpty()) {
      List flattenFields = new ArrayList<>();
      parseSchemaFields(topic.getMessageSchema().getSchemaFields(), flattenFields, null);

      for (FlattenSchemaField field : flattenFields) {
        fieldSuggest.add(SearchSuggest.builder().input(field.getName()).weight(5).build());
        fieldsWithChildrenName.add(field.getName());
        if (field.getTags() != null) {
          tagsWithChildren.add(field.getTags());
        }
      }
      doc.put("fieldNames", fieldsWithChildrenName);
    }

    ParseTags parseTags = new ParseTags(Entity.getEntityTags(Entity.TOPIC, topic));
    tagsWithChildren.add(parseTags.getTags());
    List flattenedTagList =
        tagsWithChildren.stream()
            .flatMap(List::stream)
            .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
    Map commonAttributes = getCommonAttributesMap(topic, Entity.TOPIC);
    doc.putAll(commonAttributes);
    doc.put(
        "displayName", topic.getDisplayName() != null ? topic.getDisplayName() : topic.getName());
    doc.put("tags", flattenedTagList);
    doc.put("tier", parseTags.getTierTag());
    doc.put("field_suggest", fieldSuggest);
    doc.put("service_suggest", serviceSuggest);
    doc.put("serviceType", topic.getServiceType());
    doc.put("lineage", SearchIndex.getLineageData(topic.getEntityReference()));
    doc.put("messageSchema", topic.getMessageSchema() != null ? topic.getMessageSchema() : null);
    doc.put("service", getEntityWithDisplayName(topic.getService()));
    return doc;
  }

  private void parseSchemaFields(
      List fields, List flattenSchemaFields, String parentSchemaField) {
    Optional optParentField =
        Optional.ofNullable(parentSchemaField).filter(Predicate.not(String::isEmpty));
    List tags = new ArrayList<>();
    for (Field field : fields) {
      String fieldName = field.getName();
      if (optParentField.isPresent()) {
        fieldName = FullyQualifiedName.add(optParentField.get(), fieldName);
      }
      if (field.getTags() != null) {
        tags = field.getTags();
      }

      FlattenSchemaField flattenSchemaField =
          FlattenSchemaField.builder().name(fieldName).description(field.getDescription()).build();

      if (!tags.isEmpty()) {
        flattenSchemaField.setTags(tags);
      }
      flattenSchemaFields.add(flattenSchemaField);
      if (field.getChildren() != null) {
        parseSchemaFields(field.getChildren(), flattenSchemaFields, field.getName());
      }
    }
  }

  public static Map getFields() {
    Map fields = SearchIndex.getDefaultFields();
    fields.put(ES_MESSAGE_SCHEMA_FIELD, 7.0f);
    fields.put("messageSchema.schemaFields.name.keyword", 5.0f);
    fields.put("messageSchema.schemaFields.description", 1.0f);
    fields.put("messageSchema.schemaFields.children.name", 7.0f);
    fields.put("messageSchema.schemaFields.children.keyword", 5.0f);
    return fields;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy