+ Start a Discussion
Tyler Harris 8Tyler Harris 8 

Custom Search Solution Speed Question

Hello,

I'm currently trying to find the best way to approach a request we have internally. I have inherited  custom Visualforce Community that has a custom search that is conducting a large SOSL search and rendering the results in VF and into a AngularJS controller. We have found that the JS remote call is very slow in rendering the results on page. So I have a few questions on how to optimize:

1) Would an API call be faster in fetching the results? Is there anything I can set in Salesforce to make this faster?

2) In general, are Lightning Components/Apps faster to render data on page? Would it make sense to refactor into a lightning component?

Apex
@RemoteAction
    global static List<SObject> getAllRecords (String language, String searchString, Boolean isSaved) {
        System.debug('>>> searchString = ' + searchString);
        List<SObject> records = new List<SObject>();
        String contentRecordTypeId = GlobalPRM_ManagementService.getRecordTypeId(GlobalPRM_ConstantDeclarations.CONTENT_RECORD_TYPE_RESOURCE, 'ContentVersion');
        
        String savedResourceIds = '';
        if (isSaved) {
            savedResourceIds = getSavedResourceIds();
             //Updated by JoJo 05/10/2016, If viewing saved only, but without saved resource, return empty records.             
               
             if (savedResourceIds == '' ) {
                 return records;
             }
        } 
        
        if (searchString == '') {
            String filter1 = '';
            if (savedResourceIds != '' ) {
                filter1 += ' AND Id IN ' + savedResourceIds;
            }
              System.debug('>>> filter1 = ' + filter1);
            records.addAll(GlobalPRM_ManagementService.getArticlesByFieldSet('Resource_Library__kav', 'GlobalPRM_ResourceLibraryArticle_FieldSe', language, filter1));
            String fieldNames = 'Title, Description, TagCsv, Brand__c, Type__c, Products__c, Solutions__c, Role__c, CreatedDate, LastModifiedDate, FileType';
            for(GlobalPRM_VisibilitySettings__c rec: GlobalPRM_VisibilitySettings__c.getAll().valueS()){
              fieldNames+=','+rec.source__c;
            }
            String filter2 = GlobalPRM_ManagementService.buildFilterForContent(language, 'RecordTypeId = \'' + contentRecordTypeId + '\'');
            if (savedResourceIds != '') {
                filter2 += ' AND Id IN ' + savedResourceIds;
            }
            System.debug('>>> filter2 = ' + filter2);
            records.addAll(GlobalPRM_ManagementService.queryListByFieldNames(fieldNames, 'ContentVersion', filter2));
        } else {
            String queryString = 'FIND \'' + searchString + '\' IN ALL FIELDS RETURNING ';
            String filter1 = GlobalPRM_ManagementService.buildFilterForArticle(language, '', 'Resource_Library__kav');
            String filter2 = GlobalPRM_ManagementService.buildFilterForContent(language, 'RecordTypeId = \'' + contentRecordTypeId + '\'');
            
            // 8/18/2016 Jade Global: This has been added to include customSetting Fields in Query and Visibility__c has been Removed from Query
            String visibilityFields = GlobalPRM_ManagementService.retQueryStringForCustomSettingFields('GlobalPRM_VisibilitySettings__c');
            
            if (savedResourceIds != '' ) {
                filter1 += ' AND Id IN ' + savedResourceIds;
                filter2 += ' AND Id IN ' + savedResourceIds;
            }
            queryString += 'Resource_Library__kav ('+visibilityFields+', Thumbnail__c, Heading__c, FirstPublishedDate, LastModifiedDate, Description__c, Products__c, Solutions__c, Brand__c, Type__c, Role__c, Tags__c, KnowledgeArticleId' + 
                           filter1 + '),' + 
                           ' ContentVersion ('+visibilityFields+' ,Title, Description, TagCsv, Brand__c, Type__c, Products__c, Solutions__c, Role__c, CreatedDate, LastModifiedDate, FileType' + 
                           filter2 + ')';
            System.debug('>>> queryString = ' + queryString);
            
            List<List<sObject>> results = search.query(queryString);
            for (Integer k = 0; k < results.size(); k++) {
                records.addAll(results[k]);
            }
        } 

       return GlobalPRM_ManagementService.filterByVisibilityPOC(records,null);
    }
    
    global static String getSavedResourceIds () {
        String savedResourceIds = '('; 
        
        String fieldNames = 'Id, Partner_User__c, Resource_ID__c';
        String filter = 'WHERE Partner_User__c = \'' + UserInfo.getUserId() + '\'';
        Set<String> articleNumberSet = new Set<String>();
        Set<String> contentDocumentSet = new Set<String>();

         //Updated by JoJo 05/10/2016, get ArticleNumber or ContentDocumentId by Resource_ID__c  
        for (Saved_Resource__c sr : (List<Saved_Resource__c>)GlobalPRM_ManagementService.queryListByFieldNames(fieldNames, 'Saved_Resource__c', filter)) {
             String resourceId = sr.Resource_ID__c;
            if(resourceId.length()==18){
                if(resourceId.startsWithIgnoreCase('069')){
                 //sr.Resource_ID__c  => Content Document Id          
                contentDocumentSet.add(sr.Resource_ID__c); 
                }else{
                    savedResourceIds += '\'' + sr.Resource_ID__c + '\', ';
                }
            }else{
                 //sr.Resource_ID__c  => ArticleNumber          
               articleNumberSet.add(sr.Resource_ID__c);
            }
        }
        //Get the knowledge articl id which publishStatus is online by ArticleNumber           
        KnowledgeArticleVersion[] kavList =[Select Id from KnowledgeArticleVersion where PublishStatus='online' and language='en_US' and ArticleNumber in :articleNumberSet];
        for (KnowledgeArticleVersion kav :kavList) {
         
           savedResourceIds += '\'' + kav.id + '\', ';
        }
        
       //Get the content version id which is the LatestPublishedVersionId  by content document id           
       ContentDocument[] cdList= [Select LatestPublishedVersionId From ContentDocument where id in :contentDocumentSet];
       for (ContentDocument cd :cdList) {
         
           savedResourceIds += '\'' + cd.LatestPublishedVersionId + '\', ';
        }
        
        if (savedResourceIds.length() == 1) {
            savedResourceIds = '';
        } else {
            savedResourceIds = savedResourceIds.substring(0, savedResourceIds.length() - 2);
            savedResourceIds += ')';
        }
        
        System.debug('>>>savedResourceIds' + savedResourceIds);
        return savedResourceIds;
    }

 
Tyler Harris 8Tyler Harris 8
Also, is there any way I could take my current custom Apex search and 'back end' it to Salesforce Standard search? Salesforce's standard search is great and I'm not sure why we needed a custom one. Please let me know.

Thank you.