You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@atlas.apache.org by sa...@apache.org on 2019/11/08 18:18:05 UTC

[atlas] branch branch-2.0 updated: ATLAS-3517: Enhance suggestions REST API to have optional 'fieldName' parameter to get suggestions from

This is an automated email from the ASF dual-hosted git repository.

sarath pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/atlas.git


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new 7e3990b  ATLAS-3517: Enhance suggestions REST API to have optional 'fieldName' parameter to get suggestions from
7e3990b is described below

commit 7e3990bf3816ab7f0b79b33b2a72186ae5b63d63
Author: Sarath Subramanian <sa...@apache.org>
AuthorDate: Thu Nov 7 19:27:50 2019 -0800

    ATLAS-3517: Enhance suggestions REST API to have optional 'fieldName' parameter to get suggestions from
    
    (cherry picked from commit 1b5ca4c5346c1b2fad0d276b509a56f78489755d)
---
 .../atlas/repository/graphdb/AtlasGraphIndexClient.java  |  6 ++----
 .../graphdb/janus/AtlasJanusGraphIndexClient.java        | 11 +++++++++--
 .../atlas/model/discovery/AtlasSuggestionsResult.java    | 12 +++++++++++-
 .../apache/atlas/discovery/AtlasDiscoveryService.java    |  3 ++-
 .../apache/atlas/discovery/EntityDiscoveryService.java   |  6 +++---
 .../org/apache/atlas/discovery/SuggestionsProvider.java  |  2 +-
 .../apache/atlas/discovery/SuggestionsProviderImpl.java  | 16 ++++++++++------
 .../java/org/apache/atlas/web/rest/DiscoveryREST.java    |  6 +++---
 8 files changed, 41 insertions(+), 21 deletions(-)

diff --git a/graphdb/api/src/main/java/org/apache/atlas/repository/graphdb/AtlasGraphIndexClient.java b/graphdb/api/src/main/java/org/apache/atlas/repository/graphdb/AtlasGraphIndexClient.java
index 09a88d9..9960d89 100644
--- a/graphdb/api/src/main/java/org/apache/atlas/repository/graphdb/AtlasGraphIndexClient.java
+++ b/graphdb/api/src/main/java/org/apache/atlas/repository/graphdb/AtlasGraphIndexClient.java
@@ -18,12 +18,9 @@
 package org.apache.atlas.repository.graphdb;
 
 import org.apache.atlas.model.discovery.AtlasAggregationEntry;
-import org.apache.atlas.model.discovery.SearchParameters;
-import org.apache.atlas.type.AtlasEntityType;
 
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 /**
  * Represents a graph client work with indices used by Jansgraph.
@@ -39,9 +36,10 @@ public interface AtlasGraphIndexClient {
     /**
      * Returns top 5 suggestions for the given prefix string.
      * @param prefixString the prefix string whose value needs to be retrieved.
+     * @param indexFieldName the indexed field name from which to retrieve suggestions
      * @return top 5 suggestion strings with prefix String
      */
-    List<String> getSuggestions(String prefixString);
+    List<String> getSuggestions(String prefixString, String indexFieldName);
 
     /**
      *  The implementers should apply the search weights for the passed in properties.
diff --git a/graphdb/janus/src/main/java/org/apache/atlas/repository/graphdb/janus/AtlasJanusGraphIndexClient.java b/graphdb/janus/src/main/java/org/apache/atlas/repository/graphdb/janus/AtlasJanusGraphIndexClient.java
index 278ec5d..ba65f3d 100644
--- a/graphdb/janus/src/main/java/org/apache/atlas/repository/graphdb/janus/AtlasJanusGraphIndexClient.java
+++ b/graphdb/janus/src/main/java/org/apache/atlas/repository/graphdb/janus/AtlasJanusGraphIndexClient.java
@@ -28,6 +28,7 @@ import org.apache.atlas.type.AtlasStructType.AtlasAttribute;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.collections.MapUtils;
 import org.apache.commons.configuration.Configuration;
+import org.apache.commons.lang.StringUtils;
 import org.apache.solr.client.solrj.SolrClient;
 import org.apache.solr.client.solrj.SolrQuery;
 import org.apache.solr.client.solrj.SolrRequest;
@@ -55,6 +56,8 @@ public class AtlasJanusGraphIndexClient implements AtlasGraphIndexClient {
     private static final FreqComparator FREQ_COMPARATOR          = new FreqComparator();
     private static final int            DEFAULT_SUGGESTION_COUNT = 5;
     private static final int            MIN_FACET_COUNT_REQUIRED = 1;
+    private static final String         TERMS_PREFIX             = "terms.prefix";
+    private static final String         TERMS_FIELD              = "terms.fl";
 
     private final Configuration configuration;
 
@@ -269,7 +272,7 @@ public class AtlasJanusGraphIndexClient implements AtlasGraphIndexClient {
     }
 
     @Override
-    public List<String> getSuggestions(String prefixString) {
+    public List<String> getSuggestions(String prefixString, String indexFieldName) {
         SolrClient solrClient = null;
 
         try {
@@ -284,9 +287,13 @@ public class AtlasJanusGraphIndexClient implements AtlasGraphIndexClient {
             SolrQuery solrQuery = new SolrQuery();
 
             solrQuery.setRequestHandler(Constants.TERMS_REQUEST_HANDLER)
-                     .setParam("terms.prefix", prefixString)
+                     .setParam(TERMS_PREFIX, prefixString)
                      .setParam(CommonParams.OMIT_HEADER, true);
 
+            if (StringUtils.isNotEmpty(indexFieldName)) {
+                solrQuery.setParam(TERMS_FIELD, indexFieldName);
+            }
+
             QueryResponse queryResponse = solrClient.query(VERTEX_INDEX, solrQuery);
             TermsResponse termsResponse = queryResponse == null? null: queryResponse.getTermsResponse();
 
diff --git a/intg/src/main/java/org/apache/atlas/model/discovery/AtlasSuggestionsResult.java b/intg/src/main/java/org/apache/atlas/model/discovery/AtlasSuggestionsResult.java
index 054ea2e..af574b8 100644
--- a/intg/src/main/java/org/apache/atlas/model/discovery/AtlasSuggestionsResult.java
+++ b/intg/src/main/java/org/apache/atlas/model/discovery/AtlasSuggestionsResult.java
@@ -33,9 +33,11 @@ import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.PUBLIC_
 public class AtlasSuggestionsResult {
     private List<String> suggestions;
     private String       prefixString;
+    private String       fieldName;
 
-    public AtlasSuggestionsResult(String prefixString) {
+    public AtlasSuggestionsResult(String prefixString, String fieldName) {
         this.prefixString = prefixString;
+        this.fieldName    = fieldName;
     }
 
     public List<String> getSuggestions() {
@@ -53,4 +55,12 @@ public class AtlasSuggestionsResult {
     public void setPrefixString(String prefixString) {
         this.prefixString = prefixString;
     }
+
+    public String getFieldName() {
+        return fieldName;
+    }
+
+    public void setFieldName(String fieldName) {
+        this.fieldName = fieldName;
+    }
 }
diff --git a/repository/src/main/java/org/apache/atlas/discovery/AtlasDiscoveryService.java b/repository/src/main/java/org/apache/atlas/discovery/AtlasDiscoveryService.java
index 3feae27..e64c315 100644
--- a/repository/src/main/java/org/apache/atlas/discovery/AtlasDiscoveryService.java
+++ b/repository/src/main/java/org/apache/atlas/discovery/AtlasDiscoveryService.java
@@ -151,7 +151,8 @@ public interface AtlasDiscoveryService {
     /**
      * Should return top 5 suggestion strings for the given prefix.
      * @param prefixString the prefix string
+     * @param fieldName field from which to retrieve suggestions
      * @return top 5 suggestion strings for the given prefix.
      */
-    AtlasSuggestionsResult getSuggestions(String prefixString);
+    AtlasSuggestionsResult getSuggestions(String prefixString, String fieldName);
 }
diff --git a/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java b/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java
index 23fd7e9..25042c1 100644
--- a/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java
+++ b/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java
@@ -109,7 +109,7 @@ public class EntityDiscoveryService implements AtlasDiscoveryService {
         this.maxTagsLengthInIdxQuery  = ApplicationProperties.get().getInt(Constants.INDEX_SEARCH_TAGS_MAX_QUERY_STR_LENGTH, 512);
         this.indexSearchPrefix        = AtlasGraphUtilsV2.getIndexSearchPrefix();
         this.userProfileService       = userProfileService;
-        this.suggestionsProvider      = new SuggestionsProviderImpl(graph);
+        this.suggestionsProvider      = new SuggestionsProviderImpl(graph, typeRegistry);
     }
 
     @Override
@@ -448,8 +448,8 @@ public class EntityDiscoveryService implements AtlasDiscoveryService {
 
     @Override
     @GraphTransaction
-    public AtlasSuggestionsResult getSuggestions(String prefixString) {
-        return suggestionsProvider.getSuggestions(prefixString);
+    public AtlasSuggestionsResult getSuggestions(String prefixString, String fieldName) {
+        return suggestionsProvider.getSuggestions(prefixString, fieldName);
     }
 
     @Override
diff --git a/repository/src/main/java/org/apache/atlas/discovery/SuggestionsProvider.java b/repository/src/main/java/org/apache/atlas/discovery/SuggestionsProvider.java
index 4ee1b13..6cf2252 100644
--- a/repository/src/main/java/org/apache/atlas/discovery/SuggestionsProvider.java
+++ b/repository/src/main/java/org/apache/atlas/discovery/SuggestionsProvider.java
@@ -20,5 +20,5 @@ package org.apache.atlas.discovery;
 import org.apache.atlas.model.discovery.AtlasSuggestionsResult;
 
 public interface SuggestionsProvider {
-    AtlasSuggestionsResult getSuggestions(String prefixString);
+    AtlasSuggestionsResult getSuggestions(String prefixString, String indexFieldName);
 }
diff --git a/repository/src/main/java/org/apache/atlas/discovery/SuggestionsProviderImpl.java b/repository/src/main/java/org/apache/atlas/discovery/SuggestionsProviderImpl.java
index c00a650..d7824ae 100644
--- a/repository/src/main/java/org/apache/atlas/discovery/SuggestionsProviderImpl.java
+++ b/repository/src/main/java/org/apache/atlas/discovery/SuggestionsProviderImpl.java
@@ -21,6 +21,7 @@ import org.apache.atlas.AtlasException;
 import org.apache.atlas.model.discovery.AtlasSuggestionsResult;
 import org.apache.atlas.repository.graphdb.AtlasGraph;
 import org.apache.atlas.repository.graphdb.AtlasGraphIndexClient;
+import org.apache.atlas.type.AtlasTypeRegistry;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -30,20 +31,23 @@ import java.util.Collections;
 public class SuggestionsProviderImpl implements SuggestionsProvider {
     private static final Logger LOG = LoggerFactory.getLogger(SuggestionsProviderImpl.class);
 
-    private final AtlasGraph graph;
+    private final AtlasGraph        graph;
+    private final AtlasTypeRegistry typeRegistry;
 
-    public SuggestionsProviderImpl(AtlasGraph graph) {
-        this.graph = graph;
+    public SuggestionsProviderImpl(AtlasGraph graph, AtlasTypeRegistry typeRegistry) {
+        this.graph        = graph;
+        this.typeRegistry = typeRegistry;
     }
 
     @Override
-    public AtlasSuggestionsResult getSuggestions(String prefixString) {
-        AtlasSuggestionsResult result = new AtlasSuggestionsResult(prefixString);
+    public AtlasSuggestionsResult getSuggestions(String prefixString, String fieldName) {
+        AtlasSuggestionsResult result = new AtlasSuggestionsResult(prefixString, fieldName);
 
         try {
             AtlasGraphIndexClient graphIndexClient = graph.getGraphIndexClient();
+            String                indexFieldName   = (fieldName == null) ? null : typeRegistry.getIndexFieldName(fieldName);
 
-            result.setSuggestions(graphIndexClient.getSuggestions(prefixString));
+            result.setSuggestions(graphIndexClient.getSuggestions(prefixString, indexFieldName));
         } catch (AtlasException e) {
             LOG.error("Error encountered in performing quick suggestions. Will return no suggestions.", e);
 
diff --git a/webapp/src/main/java/org/apache/atlas/web/rest/DiscoveryREST.java b/webapp/src/main/java/org/apache/atlas/web/rest/DiscoveryREST.java
index 908ae70..825cda3 100644
--- a/webapp/src/main/java/org/apache/atlas/web/rest/DiscoveryREST.java
+++ b/webapp/src/main/java/org/apache/atlas/web/rest/DiscoveryREST.java
@@ -650,15 +650,15 @@ public class DiscoveryREST {
 
     @Path("suggestions")
     @GET
-    public AtlasSuggestionsResult getSuggestions(@QueryParam("prefixString") String prefixString) throws AtlasBaseException {
+    public AtlasSuggestionsResult getSuggestions(@QueryParam("prefixString") String prefixString, @QueryParam("fieldName") String fieldName) {
         AtlasPerfTracer perf = null;
 
         try {
             if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
-                perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "DiscoveryREST.getSuggestions(" + prefixString + ")");
+                perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "DiscoveryREST.getSuggestions(" + prefixString + "," + fieldName + ")");
             }
 
-            return discoveryService.getSuggestions(prefixString);
+            return discoveryService.getSuggestions(prefixString, fieldName);
         } finally {
             AtlasPerfTracer.log(perf);
         }