Commit e75473a3 by Madhan Neethiraj

ATLAS-2008: basic-search gremlin query updated to use bind varibles, to avoid ClassCastException

parent 9b72de98
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
package org.apache.atlas.discovery; package org.apache.atlas.discovery;
import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.discovery.SearchParameters;
import org.apache.atlas.model.discovery.SearchParameters.FilterCriteria; import org.apache.atlas.model.discovery.SearchParameters.FilterCriteria;
import org.apache.atlas.model.instance.AtlasEntity; import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.repository.Constants; import org.apache.atlas.repository.Constants;
...@@ -33,7 +32,6 @@ import org.apache.atlas.type.AtlasClassificationType; ...@@ -33,7 +32,6 @@ import org.apache.atlas.type.AtlasClassificationType;
import org.apache.atlas.util.AtlasGremlinQueryProvider; import org.apache.atlas.util.AtlasGremlinQueryProvider;
import org.apache.atlas.utils.AtlasPerfTracer; import org.apache.atlas.utils.AtlasPerfTracer;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -65,30 +63,30 @@ public class ClassificationSearchProcessor extends SearchProcessor { ...@@ -65,30 +63,30 @@ public class ClassificationSearchProcessor extends SearchProcessor {
final FilterCriteria filterCriteria = context.getSearchParameters().getTagFilters(); final FilterCriteria filterCriteria = context.getSearchParameters().getTagFilters();
final Set<String> typeAndSubTypes = classificationType.getTypeAndAllSubTypes(); final Set<String> typeAndSubTypes = classificationType.getTypeAndAllSubTypes();
final String typeAndSubTypesQryStr = classificationType.getTypeAndAllSubTypesQryStr(); final String typeAndSubTypesQryStr = classificationType.getTypeAndAllSubTypesQryStr();
final Set<String> solrAttributes = new HashSet<>(); final Set<String> indexAttributes = new HashSet<>();
final Set<String> gremlinAttributes = new HashSet<>(); final Set<String> graphAttributes = new HashSet<>();
final Set<String> allAttributes = new HashSet<>(); final Set<String> allAttributes = new HashSet<>();
processSearchAttributes(classificationType, filterCriteria, solrAttributes, gremlinAttributes, allAttributes); processSearchAttributes(classificationType, filterCriteria, indexAttributes, graphAttributes, allAttributes);
// for classification search, if any attribute can't be handled by Solr - switch to all Gremlin // for classification search, if any attribute can't be handled by index query - switch to all filter by Graph query
boolean useSolrSearch = typeAndSubTypesQryStr.length() <= MAX_QUERY_STR_LENGTH_TAGS && CollectionUtils.isEmpty(gremlinAttributes) && canApplySolrFilter(classificationType, filterCriteria, false); boolean useIndexSearch = typeAndSubTypesQryStr.length() <= MAX_QUERY_STR_LENGTH_TAGS && CollectionUtils.isEmpty(graphAttributes) && canApplyIndexFilter(classificationType, filterCriteria, false);
AtlasGraph graph = context.getGraph(); AtlasGraph graph = context.getGraph();
if (useSolrSearch) { if (useIndexSearch) {
StringBuilder solrQuery = new StringBuilder(); StringBuilder indexQuery = new StringBuilder();
constructTypeTestQuery(solrQuery, typeAndSubTypesQryStr); constructTypeTestQuery(indexQuery, typeAndSubTypesQryStr);
constructFilterQuery(solrQuery, classificationType, filterCriteria, solrAttributes); constructFilterQuery(indexQuery, classificationType, filterCriteria, indexAttributes);
String solrQueryString = STRAY_AND_PATTERN.matcher(solrQuery).replaceAll(")"); String indexQueryString = STRAY_AND_PATTERN.matcher(indexQuery).replaceAll(")");
solrQueryString = STRAY_OR_PATTERN.matcher(solrQueryString).replaceAll(")"); indexQueryString = STRAY_OR_PATTERN.matcher(indexQueryString).replaceAll(")");
solrQueryString = STRAY_ELIPSIS_PATTERN.matcher(solrQueryString).replaceAll(""); indexQueryString = STRAY_ELIPSIS_PATTERN.matcher(indexQueryString).replaceAll("");
indexQuery = graph.indexQuery(Constants.VERTEX_INDEX, solrQueryString); this.indexQuery = graph.indexQuery(Constants.VERTEX_INDEX, indexQueryString);
} else { } else {
indexQuery = null; indexQuery = null;
} }
...@@ -101,28 +99,29 @@ public class ClassificationSearchProcessor extends SearchProcessor { ...@@ -101,28 +99,29 @@ public class ClassificationSearchProcessor extends SearchProcessor {
// Now filter on the tag attributes // Now filter on the tag attributes
AtlasGremlinQueryProvider queryProvider = AtlasGremlinQueryProvider.INSTANCE; AtlasGremlinQueryProvider queryProvider = AtlasGremlinQueryProvider.INSTANCE;
gremlinQueryBindings = new HashMap<>();
StringBuilder gremlinQuery = new StringBuilder(); StringBuilder gremlinQuery = new StringBuilder();
gremlinQuery.append("g.V().has('__guid', T.in, guids)"); gremlinQuery.append("g.V().has('__guid', T.in, guids)");
gremlinQuery.append(queryProvider.getQuery(AtlasGremlinQueryProvider.AtlasGremlinQuery.BASIC_SEARCH_CLASSIFICATION_FILTER)); gremlinQuery.append(queryProvider.getQuery(AtlasGremlinQueryProvider.AtlasGremlinQuery.BASIC_SEARCH_CLASSIFICATION_FILTER));
gremlinQuery.append(".as('e').out()"); gremlinQuery.append(".as('e').out()");
gremlinQuery.append(queryProvider.getQuery(AtlasGremlinQueryProvider.AtlasGremlinQuery.BASIC_SEARCH_TYPE_FILTER)); gremlinQuery.append(queryProvider.getQuery(AtlasGremlinQueryProvider.AtlasGremlinQuery.BASIC_SEARCH_TYPE_FILTER));
constructGremlinFilterQuery(gremlinQuery, context.getClassificationType(), context.getSearchParameters().getTagFilters()); constructGremlinFilterQuery(gremlinQuery, gremlinQueryBindings, context.getClassificationType(), context.getSearchParameters().getTagFilters());
// After filtering on tags go back to e and output the list of entity vertices // After filtering on tags go back to e and output the list of entity vertices
gremlinQuery.append(".back('e').toList()"); gremlinQuery.append(".back('e').toList()");
gremlinTagFilterQuery = gremlinQuery.toString();
gremlinQueryBindings = new HashMap<>();
gremlinQueryBindings.put("traitNames", typeAndSubTypes); gremlinQueryBindings.put("traitNames", typeAndSubTypes);
gremlinQueryBindings.put("typeNames", typeAndSubTypes); // classification typeName gremlinQueryBindings.put("typeNames", typeAndSubTypes); // classification typeName
gremlinTagFilterQuery = gremlinQuery.toString();
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
LOG.debug("gremlinTagFilterQuery={}", gremlinTagFilterQuery); LOG.debug("gremlinTagFilterQuery={}", gremlinTagFilterQuery);
} }
} else { } else {
gremlinTagFilterQuery = null; gremlinTagFilterQuery = null;
gremlinQueryBindings = null; gremlinQueryBindings = null;
} }
} }
...@@ -170,7 +169,7 @@ public class ClassificationSearchProcessor extends SearchProcessor { ...@@ -170,7 +169,7 @@ public class ClassificationSearchProcessor extends SearchProcessor {
if (indexQuery != null) { if (indexQuery != null) {
Iterator<AtlasIndexQuery.Result> queryResult = indexQuery.vertices(qryOffset, limit); Iterator<AtlasIndexQuery.Result> queryResult = indexQuery.vertices(qryOffset, limit);
if (!queryResult.hasNext()) { // no more results from solr - end of search if (!queryResult.hasNext()) { // no more results from index query - end of search
break; break;
} }
...@@ -259,7 +258,7 @@ public class ClassificationSearchProcessor extends SearchProcessor { ...@@ -259,7 +258,7 @@ public class ClassificationSearchProcessor extends SearchProcessor {
} }
} catch (AtlasBaseException | ScriptException e) { } catch (AtlasBaseException | ScriptException e) {
LOG.warn(e.getMessage()); LOG.warn(e.getMessage(), e);
} }
} }
......
...@@ -50,50 +50,50 @@ public class EntitySearchProcessor extends SearchProcessor { ...@@ -50,50 +50,50 @@ public class EntitySearchProcessor extends SearchProcessor {
final FilterCriteria filterCriteria = context.getSearchParameters().getEntityFilters(); final FilterCriteria filterCriteria = context.getSearchParameters().getEntityFilters();
final Set<String> typeAndSubTypes = entityType.getTypeAndAllSubTypes(); final Set<String> typeAndSubTypes = entityType.getTypeAndAllSubTypes();
final String typeAndSubTypesQryStr = entityType.getTypeAndAllSubTypesQryStr(); final String typeAndSubTypesQryStr = entityType.getTypeAndAllSubTypesQryStr();
final Set<String> solrAttributes = new HashSet<>(); final Set<String> indexAttributes = new HashSet<>();
final Set<String> gremlinAttributes = new HashSet<>(); final Set<String> graphAttributes = new HashSet<>();
final Set<String> allAttributes = new HashSet<>(); final Set<String> allAttributes = new HashSet<>();
final AtlasClassificationType classificationType = context.getClassificationType(); final AtlasClassificationType classificationType = context.getClassificationType();
final boolean filterClassification = classificationType != null && !context.needClassificationProcessor(); final boolean filterClassification = classificationType != null && !context.needClassificationProcessor();
processSearchAttributes(entityType, filterCriteria, solrAttributes, gremlinAttributes, allAttributes); processSearchAttributes(entityType, filterCriteria, indexAttributes, graphAttributes, allAttributes);
final boolean typeSearchBySolr = !filterClassification && typeAndSubTypesQryStr.length() <= MAX_QUERY_STR_LENGTH_TYPES; final boolean typeSearchByIndex = !filterClassification && typeAndSubTypesQryStr.length() <= MAX_QUERY_STR_LENGTH_TYPES;
final boolean attrSearchBySolr = !filterClassification && CollectionUtils.isNotEmpty(solrAttributes) && canApplySolrFilter(entityType, filterCriteria, false); final boolean attrSearchByIndex = !filterClassification && CollectionUtils.isNotEmpty(indexAttributes) && canApplyIndexFilter(entityType, filterCriteria, false);
StringBuilder solrQuery = new StringBuilder(); StringBuilder indexQuery = new StringBuilder();
if (typeSearchBySolr) { if (typeSearchByIndex) {
constructTypeTestQuery(solrQuery, typeAndSubTypesQryStr); constructTypeTestQuery(indexQuery, typeAndSubTypesQryStr);
} }
if (attrSearchBySolr) { if (attrSearchByIndex) {
constructFilterQuery(solrQuery, entityType, filterCriteria, solrAttributes); constructFilterQuery(indexQuery, entityType, filterCriteria, indexAttributes);
} else { } else {
gremlinAttributes.addAll(solrAttributes); graphAttributes.addAll(indexAttributes);
} }
if (solrQuery.length() > 0) { if (indexQuery.length() > 0) {
if (context.getSearchParameters().getExcludeDeletedEntities()) { if (context.getSearchParameters().getExcludeDeletedEntities()) {
constructStateTestQuery(solrQuery); constructStateTestQuery(indexQuery);
} }
String solrQueryString = STRAY_AND_PATTERN.matcher(solrQuery).replaceAll(")"); String indexQueryString = STRAY_AND_PATTERN.matcher(indexQuery).replaceAll(")");
solrQueryString = STRAY_OR_PATTERN.matcher(solrQueryString).replaceAll(")"); indexQueryString = STRAY_OR_PATTERN.matcher(indexQueryString).replaceAll(")");
solrQueryString = STRAY_ELIPSIS_PATTERN.matcher(solrQueryString).replaceAll(""); indexQueryString = STRAY_ELIPSIS_PATTERN.matcher(indexQueryString).replaceAll("");
indexQuery = context.getGraph().indexQuery(Constants.VERTEX_INDEX, solrQueryString); this.indexQuery = context.getGraph().indexQuery(Constants.VERTEX_INDEX, indexQueryString);
} else { } else {
indexQuery = null; this.indexQuery = null;
} }
if (CollectionUtils.isNotEmpty(gremlinAttributes) || !typeSearchBySolr) { if (CollectionUtils.isNotEmpty(graphAttributes) || !typeSearchByIndex) {
AtlasGraphQuery query = context.getGraph().query(); AtlasGraphQuery query = context.getGraph().query();
if (!typeSearchBySolr) { if (!typeSearchByIndex) {
query.in(Constants.TYPE_NAME_PROPERTY_KEY, typeAndSubTypes); query.in(Constants.TYPE_NAME_PROPERTY_KEY, typeAndSubTypes);
} }
...@@ -101,9 +101,9 @@ public class EntitySearchProcessor extends SearchProcessor { ...@@ -101,9 +101,9 @@ public class EntitySearchProcessor extends SearchProcessor {
query.in(Constants.TRAIT_NAMES_PROPERTY_KEY, classificationType.getTypeAndAllSubTypes()); query.in(Constants.TRAIT_NAMES_PROPERTY_KEY, classificationType.getTypeAndAllSubTypes());
} }
graphQuery = toGraphFilterQuery(entityType, filterCriteria, gremlinAttributes, query); graphQuery = toGraphFilterQuery(entityType, filterCriteria, graphAttributes, query);
if (context.getSearchParameters().getExcludeDeletedEntities() && indexQuery == null) { if (context.getSearchParameters().getExcludeDeletedEntities() && this.indexQuery == null) {
graphQuery.has(Constants.STATE_PROPERTY_KEY, "ACTIVE"); graphQuery.has(Constants.STATE_PROPERTY_KEY, "ACTIVE");
} }
} else { } else {
...@@ -161,7 +161,7 @@ public class EntitySearchProcessor extends SearchProcessor { ...@@ -161,7 +161,7 @@ public class EntitySearchProcessor extends SearchProcessor {
if (indexQuery != null) { if (indexQuery != null) {
Iterator<AtlasIndexQuery.Result> idxQueryResult = indexQuery.vertices(qryOffset, limit); Iterator<AtlasIndexQuery.Result> idxQueryResult = indexQuery.vertices(qryOffset, limit);
if (!idxQueryResult.hasNext()) { // no more results from solr - end of search if (!idxQueryResult.hasNext()) { // no more results from index query - end of search
break; break;
} }
......
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