Commit bec9370e by apoorvnaik Committed by Madhan Neethiraj

ATLAS-1659: fix to update full-text search string when traits/tags are added/removed from entity

Signed-off-by: 's avatarMadhan Neethiraj <madhan@apache.org> (cherry picked from commit 298fdb9d2e14227676da7448bdb5d54a1a6a1db6)
parent 9e5ef85c
......@@ -54,7 +54,7 @@ public class FullTextMapperV2 {
@Inject
public FullTextMapperV2(AtlasTypeRegistry typeRegistry) {
this.entityGraphRetriever = new EntityGraphRetriever(typeRegistry);
entityGraphRetriever = new EntityGraphRetriever(typeRegistry);
Configuration configuration = null;
......@@ -69,23 +69,42 @@ public class FullTextMapperV2 {
}
}
public String map(String guid) throws AtlasBaseException {
/**
* Map newly associated/defined classifications for the entity with given GUID
* @param guid Entity guid
* @param classifications new classifications added to the entity
* @return Full text string ONLY for the added classifications
* @throws AtlasBaseException
*/
public String getIndexTextForClassifications(String guid, List<AtlasClassification> classifications) throws AtlasBaseException {
String ret = null;
RequestContext context = RequestContext.get();
AtlasEntityWithExtInfo entity = context.getInstanceV2(guid);
AtlasEntityWithExtInfo entityWithExtInfo = getAndCacheEntity(guid);
if (entity == null) {
entity = entityGraphRetriever.toAtlasEntityWithExtInfo(guid);
if (entityWithExtInfo != null) {
StringBuilder sb = new StringBuilder();
if (entity != null) {
context.cache(entity);
if (CollectionUtils.isNotEmpty(classifications)) {
for (AtlasClassification classification : classifications) {
sb.append(classification.getTypeName()).append(FULL_TEXT_DELIMITER);
if (LOG.isDebugEnabled()) {
LOG.debug("Cache miss -> GUID = {}", guid);
mapAttributes(classification.getAttributes(), entityWithExtInfo, sb, new HashSet<String>());
}
}
ret = sb.toString();
}
if (LOG.isDebugEnabled()) {
LOG.debug("FullTextMapperV2.map({}): {}", guid, ret);
}
return ret;
}
public String getIndexTextForEntity(String guid) throws AtlasBaseException {
String ret = null;
AtlasEntityWithExtInfo entity = getAndCacheEntity(guid);
if (entity != null) {
StringBuilder sb = new StringBuilder();
......@@ -179,4 +198,22 @@ public class FullTextMapperV2 {
sb.append(String.valueOf(value)).append(FULL_TEXT_DELIMITER);
}
}
private AtlasEntityWithExtInfo getAndCacheEntity(String guid) throws AtlasBaseException {
RequestContext context = RequestContext.get();
AtlasEntityWithExtInfo entityWithExtInfo = context.getInstanceV2(guid);
if (entityWithExtInfo == null) {
entityWithExtInfo = entityGraphRetriever.toAtlasEntityWithExtInfo(guid);
if (entityWithExtInfo != null) {
context.cache(entityWithExtInfo);
if (LOG.isDebugEnabled()) {
LOG.debug("Cache miss -> GUID = {}", guid);
}
}
}
return entityWithExtInfo;
}
}
......@@ -41,6 +41,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
......@@ -85,6 +86,10 @@ public class AtlasEntityChangeNotifier {
}
public void onClassificationAddedToEntity(String entityId, List<AtlasClassification> classifications) throws AtlasBaseException {
// Only new classifications need to be used for a partial full text string which can be
// appended to the existing fullText
updateFullTextMapping(entityId, classifications);
ITypedReferenceableInstance entity = toITypedReferenceable(entityId);
List<ITypedStruct> traits = toITypedStructs(classifications);
......@@ -102,6 +107,9 @@ public class AtlasEntityChangeNotifier {
}
public void onClassificationDeletedFromEntity(String entityId, List<String> traitNames) throws AtlasBaseException {
// Since the entity has already been modified in the graph, we need to recursively remap the entity
doFullTextMapping(entityId);
ITypedReferenceableInstance entity = toITypedReferenceable(entityId);
if (entity == null || CollectionUtils.isEmpty(traitNames)) {
......@@ -202,7 +210,7 @@ public class AtlasEntityChangeNotifier {
}
try {
String fullText = fullTextMapperV2.map(guid);
String fullText = fullTextMapperV2.getIndexTextForEntity(guid);
GraphHelper.setProperty(atlasVertex, Constants.ENTITY_TEXT_PROPERTY_KEY, fullText);
} catch (AtlasBaseException e) {
......@@ -210,4 +218,36 @@ public class AtlasEntityChangeNotifier {
}
}
}
private void updateFullTextMapping(String entityId, List<AtlasClassification> classifications) {
try {
if(!AtlasRepositoryConfiguration.isFullTextSearchEnabled()) {
return;
}
} catch (AtlasException e) {
LOG.warn("Unable to determine if FullText is disabled. Proceeding with FullText mapping");
}
if (StringUtils.isEmpty(entityId) || CollectionUtils.isEmpty(classifications)) {
return;
}
AtlasVertex atlasVertex = AtlasGraphUtilsV1.findByGuid(entityId);
try {
String classificationFullText = fullTextMapperV2.getIndexTextForClassifications(entityId, classifications);
String existingFullText = (String) GraphHelper.getProperty(atlasVertex, Constants.ENTITY_TEXT_PROPERTY_KEY);
String newFullText = existingFullText + " " + classificationFullText;
GraphHelper.setProperty(atlasVertex, Constants.ENTITY_TEXT_PROPERTY_KEY, newFullText);
} catch (AtlasBaseException e) {
LOG.error("FullText mapping failed for Vertex[ guid = {} ]", entityId, e);
}
}
private void doFullTextMapping(String guid) {
AtlasEntityHeader entityHeader = new AtlasEntityHeader();
entityHeader.setGuid(guid);
doFullTextMapping(Collections.singletonList(entityHeader));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment