Commit aa86d62c by Pinal Shah Committed by nixonrodrigues

ATLAS-3848 : Quick Search : Fixed incorrect aggregation metrics

parent 992b8b47
...@@ -34,6 +34,8 @@ import java.util.List; ...@@ -34,6 +34,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static org.apache.atlas.repository.Constants.CUSTOM_ATTRIBUTES_PROPERTY_KEY;
public class AtlasSolrQueryBuilder { public class AtlasSolrQueryBuilder {
private static final Logger LOG = LoggerFactory.getLogger(AtlasSolrQueryBuilder.class); private static final Logger LOG = LoggerFactory.getLogger(AtlasSolrQueryBuilder.class);
...@@ -43,6 +45,8 @@ public class AtlasSolrQueryBuilder { ...@@ -43,6 +45,8 @@ public class AtlasSolrQueryBuilder {
private boolean excludeDeletedEntities; private boolean excludeDeletedEntities;
private boolean includeSubtypes; private boolean includeSubtypes;
private Map<String, String> indexFieldNameCache; private Map<String, String> indexFieldNameCache;
public static final char CUSTOM_ATTR_SEPARATOR = '=';
public static final String CUSTOM_ATTR_SEARCH_FORMAT = "\"\\\"%s\\\":\\\"%s\\\"\"";
public AtlasSolrQueryBuilder() { public AtlasSolrQueryBuilder() {
...@@ -211,6 +215,14 @@ public class AtlasSolrQueryBuilder { ...@@ -211,6 +215,14 @@ public class AtlasSolrQueryBuilder {
attributeValue = attributeValue.trim(); attributeValue = attributeValue.trim();
} }
if (attributeName.equals(CUSTOM_ATTRIBUTES_PROPERTY_KEY) && operator.equals(Operator.CONTAINS)) {
// CustomAttributes stores key value pairs in String format, so ideally it should be 'contains' operator to search for one pair,
// for use-case, E1 having key1=value1 and E2 having key1=value2, searching key1=value1 results both E1,E2
// surrounding inverted commas to attributeValue works
operator = Operator.EQ;
attributeValue = getIndexQueryAttributeValue(attributeValue);
}
AtlasAttribute attribute = entityType.getAttribute(attributeName); AtlasAttribute attribute = entityType.getAttribute(attributeName);
if (attribute == null) { if (attribute == null) {
...@@ -231,6 +243,8 @@ public class AtlasSolrQueryBuilder { ...@@ -231,6 +243,8 @@ public class AtlasSolrQueryBuilder {
throw new AtlasBaseException(msg); throw new AtlasBaseException(msg);
} }
beginCriteria(queryBuilder);
switch (operator) { switch (operator) {
case EQ: case EQ:
withEqual(queryBuilder, indexFieldName, attributeValue); withEqual(queryBuilder, indexFieldName, attributeValue);
...@@ -274,9 +288,27 @@ public class AtlasSolrQueryBuilder { ...@@ -274,9 +288,27 @@ public class AtlasSolrQueryBuilder {
LOG.error(msg); LOG.error(msg);
throw new AtlasBaseException(msg); throw new AtlasBaseException(msg);
} }
endCriteria(queryBuilder);
} }
} }
private String getIndexQueryAttributeValue(String attributeValue) {
if (StringUtils.isNotEmpty(attributeValue)) {
int separatorIdx = attributeValue.indexOf(CUSTOM_ATTR_SEPARATOR);
String key = separatorIdx != -1 ? attributeValue.substring(0, separatorIdx) : null;
String value = key != null ? attributeValue.substring(separatorIdx + 1) : null;
if (key != null && value != null) {
return String.format(CUSTOM_ATTR_SEARCH_FORMAT, key, value);
}
}
return attributeValue;
}
private void beginCriteria(StringBuilder queryBuilder) { private void beginCriteria(StringBuilder queryBuilder) {
queryBuilder.append("( "); queryBuilder.append("( ");
} }
...@@ -294,7 +326,7 @@ public class AtlasSolrQueryBuilder { ...@@ -294,7 +326,7 @@ public class AtlasSolrQueryBuilder {
} }
private void withNotEqual(StringBuilder queryBuilder, String indexFieldName, String attributeValue) { private void withNotEqual(StringBuilder queryBuilder, String indexFieldName, String attributeValue) {
queryBuilder.append("-").append(indexFieldName).append(":").append(attributeValue).append(" "); queryBuilder.append("*:* -").append(indexFieldName).append(":").append(attributeValue).append(" ");
} }
private void withEqual(StringBuilder queryBuilder, String indexFieldName, String attributeValue) { private void withEqual(StringBuilder queryBuilder, String indexFieldName, String attributeValue) {
......
...@@ -114,7 +114,7 @@ public class AtlasSolrQueryBuilderTest { ...@@ -114,7 +114,7 @@ public class AtlasSolrQueryBuilderTest {
processSearchParameters(fileName, underTest); processSearchParameters(fileName, underTest);
Assert.assertEquals(underTest.build(), "+t AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( +name_index:t10 OR +comment_index:*t10* )"); Assert.assertEquals(underTest.build(), "+t AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( ( +name_index:t10 ) OR ( +comment_index:*t10* ) )");
} }
@Test @Test
...@@ -124,7 +124,7 @@ public class AtlasSolrQueryBuilderTest { ...@@ -124,7 +124,7 @@ public class AtlasSolrQueryBuilderTest {
processSearchParameters(fileName, underTest); processSearchParameters(fileName, underTest);
Assert.assertEquals(underTest.build(), "+t AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( +name_index:t10 )"); Assert.assertEquals(underTest.build(), "+t AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( ( +name_index:t10 ) )");
} }
@Test @Test
...@@ -134,7 +134,7 @@ public class AtlasSolrQueryBuilderTest { ...@@ -134,7 +134,7 @@ public class AtlasSolrQueryBuilderTest {
processSearchParameters(fileName, underTest); processSearchParameters(fileName, underTest);
Assert.assertEquals(underTest.build(), "+t AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( +name_index:t10 AND +comment_index:*t10* )"); Assert.assertEquals(underTest.build(), "+t AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( ( +name_index:t10 ) AND ( +comment_index:*t10* ) )");
} }
@Test @Test
...@@ -144,7 +144,7 @@ public class AtlasSolrQueryBuilderTest { ...@@ -144,7 +144,7 @@ public class AtlasSolrQueryBuilderTest {
processSearchParameters(fileName, underTest); processSearchParameters(fileName, underTest);
Assert.assertEquals(underTest.build(), "+t AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( +name_index:t10 )"); Assert.assertEquals(underTest.build(), "+t AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( ( +name_index:t10 ) )");
} }
@Test @Test
...@@ -154,7 +154,7 @@ public class AtlasSolrQueryBuilderTest { ...@@ -154,7 +154,7 @@ public class AtlasSolrQueryBuilderTest {
processSearchParameters(fileName, underTest); processSearchParameters(fileName, underTest);
Assert.assertEquals(underTest.build(), "+t AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND +name_index:t10 "); Assert.assertEquals(underTest.build(), "+t AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( +name_index:t10 )");
} }
@Test @Test
...@@ -164,7 +164,7 @@ public class AtlasSolrQueryBuilderTest { ...@@ -164,7 +164,7 @@ public class AtlasSolrQueryBuilderTest {
processSearchParameters(fileName, underTest); processSearchParameters(fileName, underTest);
Assert.assertEquals(underTest.build(), "+t10 AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( +comment_index:*United States* AND +descrption__index:*nothing* AND +name_index:*t100* )"); Assert.assertEquals(underTest.build(), "+t10 AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( ( +comment_index:*United States* ) AND ( +descrption__index:*nothing* ) AND ( +name_index:*t100* ) )");
} }
@Test @Test
...@@ -174,7 +174,7 @@ public class AtlasSolrQueryBuilderTest { ...@@ -174,7 +174,7 @@ public class AtlasSolrQueryBuilderTest {
processSearchParameters(fileName, underTest); processSearchParameters(fileName, underTest);
Assert.assertEquals(underTest.build(), "+t10 AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( +created__index:{ 100 TO * ] )"); Assert.assertEquals(underTest.build(), "+t10 AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( ( +created__index:{ 100 TO * ] ) )");
} }
@Test @Test
...@@ -184,7 +184,7 @@ public class AtlasSolrQueryBuilderTest { ...@@ -184,7 +184,7 @@ public class AtlasSolrQueryBuilderTest {
processSearchParameters(fileName, underTest); processSearchParameters(fileName, underTest);
Assert.assertEquals(underTest.build(), "+t10 AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( +created__index:[ 100 TO * ] AND +started__index:[ 100 TO * ] )"); Assert.assertEquals(underTest.build(), "+t10 AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( ( +created__index:[ 100 TO * ] ) AND ( +started__index:[ 100 TO * ] ) )");
} }
@Test @Test
...@@ -194,7 +194,7 @@ public class AtlasSolrQueryBuilderTest { ...@@ -194,7 +194,7 @@ public class AtlasSolrQueryBuilderTest {
processSearchParameters(fileName, underTest); processSearchParameters(fileName, underTest);
Assert.assertEquals(underTest.build(), "+t10 AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( +created__index:[ * TO100} )"); Assert.assertEquals(underTest.build(), "+t10 AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( ( +created__index:[ * TO100} ) )");
} }
@Test @Test
...@@ -204,7 +204,7 @@ public class AtlasSolrQueryBuilderTest { ...@@ -204,7 +204,7 @@ public class AtlasSolrQueryBuilderTest {
processSearchParameters(fileName, underTest); processSearchParameters(fileName, underTest);
Assert.assertEquals(underTest.build(), "+t10 AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( +created__index:[ * TO 100 ] AND +started__index:[ * TO 100 ] )"); Assert.assertEquals(underTest.build(), "+t10 AND -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( ( +created__index:[ * TO 100 ] ) AND ( +started__index:[ * TO 100 ] ) )");
} }
@Test @Test
...@@ -214,7 +214,7 @@ public class AtlasSolrQueryBuilderTest { ...@@ -214,7 +214,7 @@ public class AtlasSolrQueryBuilderTest {
processSearchParameters(fileName, underTest); processSearchParameters(fileName, underTest);
Assert.assertEquals(underTest.build(), " -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( +qualifiedName__index:testdb.t1* )"); Assert.assertEquals(underTest.build(), " -__state_index:DELETED AND +__typeName__index:(hive_table ) AND ( ( +qualifiedName__index:testdb.t1* ) )");
} }
......
...@@ -62,6 +62,7 @@ public class AtlasTypeRegistry { ...@@ -62,6 +62,7 @@ public class AtlasTypeRegistry {
commonIndexFieldNameCache = new HashMap<>(); commonIndexFieldNameCache = new HashMap<>();
resolveReferencesForRootTypes(); resolveReferencesForRootTypes();
resolveIndexFieldNamesForRootTypes();
} }
// used only by AtlasTransientTypeRegistry // used only by AtlasTransientTypeRegistry
...@@ -72,6 +73,7 @@ public class AtlasTypeRegistry { ...@@ -72,6 +73,7 @@ public class AtlasTypeRegistry {
commonIndexFieldNameCache = other.commonIndexFieldNameCache; commonIndexFieldNameCache = other.commonIndexFieldNameCache;
resolveReferencesForRootTypes(); resolveReferencesForRootTypes();
resolveIndexFieldNamesForRootTypes();
} }
public Collection<String> getAllTypeNames() { return registryData.allTypes.getAllTypeNames(); } public Collection<String> getAllTypeNames() { return registryData.allTypes.getAllTypeNames(); }
...@@ -275,6 +277,22 @@ public class AtlasTypeRegistry { ...@@ -275,6 +277,22 @@ public class AtlasTypeRegistry {
} }
} }
private void resolveIndexFieldNamesForRootTypes() {
for (AtlasStructType structType : Arrays.asList(AtlasEntityType.ENTITY_ROOT, AtlasClassificationType.CLASSIFICATION_ROOT)) {
for (AtlasAttribute attribute : structType.getAllAttributes().values()) {
String indexFieldName = getIndexFieldName(attribute.getVertexPropertyName());
if (StringUtils.isNotEmpty(indexFieldName)) {
attribute.setIndexFieldName(indexFieldName);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Attribute {} with index name {} is added", attribute.getVertexPropertyName(), attribute.getIndexFieldName());
}
}
}
}
/** /**
* retrieves the index field name for the common field passed in. * retrieves the index field name for the common field passed in.
* @param propertyName the name of the common field. * @param propertyName the name of the common field.
......
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