none
Rest API Search Results is not the same as the results returned by the search page!!

    Question

  • Hello,

    I have A search rest API, when I run it through Share point Designer< i dont Get the Same Number if results as Returned by the Search page on the Share point site, I have tried Using different Source ids , also tried to use the default source id from results source but I always get the same results so i am not sure what I am doing wrong.

    var ct = new SP.ClientContext.get_current();
         var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(ct);
         
         var queryStr = ctx.DataProvider.get_currentQueryState().k;
         keywordQuery.set_queryText(queryStr);
         
        
         keywordQuery.set_enableSorting(true);								    
         keywordQuery.set_sourceId=("xxxxxx-xxxx-xxxx-xxx-xxxxxxx");  
          keywordQuery.set_rowLimit(500);
         
         
         keywordQuery.set_trimDuplicates(false);
          
         var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(ct);
         var results = searchExecutor.executeQuery(keywordQuery);
    
           
           ct.executeQueryAsync(onQuerySuccess, onQueryFail);
           
           function onQuerySuccess() 
              {
              results.m_value.ResultTables[1].ResultRows.forEach(function (row)
              {
               var Aname1 = row.name;
               console.log(row);
                        if (!$isNull(Aname1))
                        
                   {
                   
                   var name= Aname1;
                       console.log(name);
                     
                   }
    
            
              });
    
              
              
              }
              
              
              
               function onQueryFail() 
               {
       
               }


    • Edited by 123Mn123 Friday, November 2, 2018 8:17 PM
    Friday, November 2, 2018 2:54 PM

All replies

  • This could be due to security trimming 

    https://sharepoint.stackexchange.com/questions/244037/why-do-i-get-different-results-from-a-rest-api-result-and-searching-on-sharepoin


    Please remember to click Mark as Answer on the answer if it helps you

    Friday, November 2, 2018 6:16 PM
  • hello thanks for the replay,

    My function is Running on the same SITE, IN the  COMMON.JS file AND it gets called when the search button is clicked, I even noticed some fields are missing as well. I am Not Sure If this is a permission Issue.

    Also Doing this through the Sharepoint Designer, My Aim is to Get all of the  the Results then export it to a CSV, SO far I have Been able to Export whatever in the CTX.ListData table but it is limited to 50 Rows at a time, Not Sure How else I could have access to all the results without running a separate Search API



    • Edited by 123Mn123 Wednesday, November 7, 2018 4:19 PM
    Friday, November 2, 2018 6:32 PM
  • Hi,

    Your query will return duplicates as you set the value to false, the search setting remove duplicates by default.


    To download your data to CSV, you could then create an array and then iterate all rows and store in the array and then export.

    https://halistechnology.com/2015/05/28/use-javascript-to-export-your-data-as-csv/

    <!doctype html>  
    <html>  
    <head></head>  
    <body>
    
    <a href='#' onclick='downloadCSV({ filename: "stock-data.csv" });'>Download CSV</a>
    
    <script type="text/javascript">  
        var stockData = [
            {
                Symbol: "AAPL",
                Company: "Apple Inc.",
                Price: "132.54"
            },
            {
                Symbol: "INTC",
                Company: "Intel Corporation",
                Price: "33.45"
            },
            {
                Symbol: "GOOG",
                Company: "Google Inc",
                Price: "554.52"
            },
        ];
    
        function convertArrayOfObjectsToCSV(args) {
            var result, ctr, keys, columnDelimiter, lineDelimiter, data;
    
            data = args.data || null;
            if (data == null || !data.length) {
                return null;
            }
    
            columnDelimiter = args.columnDelimiter || ',';
            lineDelimiter = args.lineDelimiter || '\n';
    
            keys = Object.keys(data[0]);
    
            result = '';
            result += keys.join(columnDelimiter);
            result += lineDelimiter;
    
            data.forEach(function(item) {
                ctr = 0;
                keys.forEach(function(key) {
                    if (ctr > 0) result += columnDelimiter;
    
                    result += item[key];
                    ctr++;
                });
                result += lineDelimiter;
            });
    
            return result;
        }
    
        function downloadCSV(args) {
            var data, filename, link;
    
            var csv = convertArrayOfObjectsToCSV({
                data: stockData
            });
            if (csv == null) return;
    
            filename = args.filename || 'export.csv';
    
            if (!csv.match(/^data:text\/csv/i)) {
                csv = 'data:text/csv;charset=utf-8,' + csv;
            }
            data = encodeURI(csv);
    
            link = document.createElement('a');
            link.setAttribute('href', data);
            link.setAttribute('download', filename);
            link.click();
        }
    </script>  
    </body>  
    </html>  

    Best Regards,

    Lee

    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, November 5, 2018 2:14 AM
  • Hello,

    Exporting the data is not a problem, Getting the data is the Issue.

    Thanks

    Tuesday, November 6, 2018 1:59 PM
  • Hi,

    Try to remove duplicates and then check.

    keywordQuery.set_trimDuplicates(true);

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, November 7, 2018 1:29 AM
  • Hi Lee,

    Thank you for the reply.

    I have tested my Code with Both setting the trim Duplicates to true and False, But it is not really changing the

    Outcome of the results.

    I am not Sure Why, My Goal is to Get all of the data returned and then Export it to a CSV File.

    I was Able to get the results displayed on the page through the CTX.ListData , but that is 50 rows at a time.

    But Not successful in Getting the whole  set of results to be exported.

    any advice will be appreciated.

    Thanks

    Wednesday, November 7, 2018 2:16 PM
  • Hi,

    I tested in my local display template, the result row will return more than 50 rows.

    My test code:

    AddPostRenderCallback(ctx, function () {  
    			var ct = new SP.ClientContext.get_current();          
                var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(ct);
                var queryStr = ctx.DataProvider.get_currentQueryState().k;
                keywordQuery.set_queryText(queryStr);
                keywordQuery.set_enableSorting(true);
                keywordQuery.set_sourceId = ("8413cd39-2156-4e00-b54d-11efd9abdb89");
                keywordQuery.set_rowLimit(500);
    
                keywordQuery.set_trimDuplicates(false);
                var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(ct);
                var results = searchExecutor.executeQuery(keywordQuery);
                ct.executeQueryAsync(onQuerySuccess, onQueryFail);
                function onQuerySuccess() {
                    debugger;
                    results.m_value.ResultTables[1].ResultRows.forEach(function (row) {
                        var Aname1 = row.name;
                        console.log(row);
                        if (!$isNull(Aname1)) {
                            var name = Aname1;
                            console.log(name);
                        }
                    });
                }
    
                function onQueryFail() {
    
                }
            })


    If you need further assistance, it would be better share more details for your solution.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, November 9, 2018 9:33 AM
  • Hi Lee,

    thank You so much for your Help, so When i use the debugger I can lot of rows actually come back  but most of them have data that

    I also there is  table 2 as below

    results.m_value.ResultTables[2].ResultRows.forEach(function (row) 

    data is residing there as well and when i try to write the Rows to console i can write it for some reason, i can write the wros from table 1 as in the original code.

    any ideas why?

    The Number of tables returned varies so it can be 1,2 or 3 tables returned and i can loop through the Rows in the tables

    for example Table 2 has about 496 Rows because it is capped at 500 Rows, How to rerun the query for the next 500 Rows until I reach the max rows returned by the query. I am Not Quite sure how to do that. i hope I am Clear enough.








    • Edited by 123Mn123 Friday, November 9, 2018 10:18 PM changes
    Friday, November 9, 2018 2:26 PM
  • Hi,

    I wrapped a demo recurring function to retrieve total rows for a result table(row limit as 50), the script depends on jQuery library so you need reference jQuery library also.

    var getSearchResults = function (PageNumber, PageLimit, ctx, ct) {
                var def = new $.Deferred();
                var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(ct);
                var queryStr = ctx.DataProvider.get_currentQueryState().k;
                keywordQuery.set_queryText(queryStr);
                keywordQuery.set_enableSorting(true);
                keywordQuery.set_sourceId = ("8413cd39-2156-4e00-b54d-11efd9abdb89");
                keywordQuery.set_rowLimit(PageLimit);
                keywordQuery.set_startRow(PageNumber * PageLimit);
    
                keywordQuery.set_trimDuplicates(false);
                var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(ct);
                var results = searchExecutor.executeQuery(keywordQuery);
                ct.executeQueryAsync(onQuerySuccess, onQueryFail);
                function onQuerySuccess() {
                    if (results.m_value.ResultTables[0].ResultRows.length > 0) {
                        def.resolve(results.m_value.ResultTables[0]);
                    }
                }
    
                function onQueryFail() {
                    def.reject("-1");
                }
                return def.promise();
            }
            AddPostRenderCallback(ctx, function () {
                var ct = new SP.ClientContext.get_current();
                var i = 0;
                var PageLimit = 50;
                var results;
                getSearchResults(i, PageLimit, ctx, ct).done(function (result) {//first page
                    results = result.ResultRows;
                    for (var j = 1; j < parseInt(result.TotalRows / PageLimit) + 1 ; j++) {//next pages
                        getSearchResults(j, PageLimit, ctx, ct).done(function (data) {
                            results=results.concat(data.ResultRows);
                            if (results.length == result.TotalRows) {
                                debugger;
                                results.forEach(function (row) {
                                    console.log(row);
                                });
                            }
                        });
                    }
    
                });
            })

    Don’t insert the script in AddPostRenderCallback as my demo, I did this for test purpose.


    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, November 12, 2018 6:35 AM
  • Hi Lee,

    thank you for the reply. the challenge is to query the next 500 rows until the Results.RowLength is reached.maybe storing all the results in an array or datatable.

    to clarify more when the search is run the search function might return multiple tables. i was successful looping through these tables to get the results up to 500 rows that are in one of the table. i was also successful at returning the total number of Rows but the challenge is as i explained is to query again and again until numbers of Rows Returned Reaches total numbers of Rows.

    Any ideas.

    thank you so much

    Monday, November 12, 2018 10:30 PM
  • Hi,

    As my sample demo, use keywordQuery.set_startRow(PageNumber * PageLimit) to set start row for your query, my test demo set PageLimit to 50 as I have no enough test data, for your case, set (PageNumber*500) so it will get next N*500 rows.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, November 13, 2018 1:06 AM
  • Hi Lee,

    Sorry for the late reply.

    I have tried using the function Above and modified it to my need it is not returning any rows.

    the reason I put the fr loop is because the search function could return more than one table and wanted to loop through them.I have added some images and as you see a search could return up to 5000 rows but I need to filter through them.

    Is there a way to use your DEMO to load the CTX.ListData with the results 50 at a time and loop until we get all the results, then as we loop store the data in an array.

    any help is appreciated. not sure what is wrong.

    var getSearchResults = function (PageNumber, PageLimit, ctx, ct) 
    {
    
        var def = new $.Deferred();
        var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(ct);
        var queryStr = ctx.DataProvider.get_currentQueryState().k;
        keywordQuery.set_queryText(queryStr);
        keywordQuery.set_enableSorting(true);
        keywordQuery.set_sourceId = ("25896d76-7b8c-4bca-9062-24c12931678c");
        keywordQuery.set_rowLimit(PageLimit);
        keywordQuery.set_startRow(PageNumber * PageLimit);
    
        keywordQuery.set_trimDuplicates(false);
         console.log("I got here");
        var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(ct);
        var results = searchExecutor.executeQuery(keywordQuery);
        ct.executeQueryAsync(onQuerySuccess, onQueryFail);
        function onQuerySuccess() {
            for (var t = 0; t < results.m_value.ResultTables.length; t++) {
    
                if (results.m_value.ResultTables[t].ResultRows.Length > 0) {
                    def.resolve(results.m_value.ResultTables[t]);
                   
                }
            }
        }
    
        function onQueryFail() {
            def.reject("-1");
        }
        return def.promise();
    }
    
    var getSearchResults1 = (ctx, function () {
        var ct = new SP.ClientContext.get_current();
        var i = 0;
        var PageLimit = 500;
        var results;
        getSearchResults(i, PageLimit, ctx, ct).done(function (result) {  //first page
            results = result.ResultRows;
            for (var j = 1; j < parseInt(result.TotalRows / PageLimit) + 1; j++) {//next pages
                getSearchResults(j, PageLimit, ctx, ct).done(function (data) {
                    results = results.concat(data.ResultRows);
                    if (results.length == result.TotalRows) {
                        debugger;
                        results.forEach(function (row) {
                            console.log(row);
                        });
                        
                    }
                    
                });
               
    
            }
            
    
        });
        
    })


    • Edited by 123Mn123 Wednesday, November 14, 2018 10:15 PM
    Wednesday, November 14, 2018 10:06 PM
  • Hi,

    May I know how you defined your query to return multiple datatables? So I could try to test in my local.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Thursday, November 15, 2018 5:48 AM
  • Hi, It does return multiple tables depending on the search. and one of the tables has the results and  is capped at 500 Rows.

    the query might find 4000(less or more) rows but one of the returned tables has only 500 rows as explained, the challenge is getting the other Rows 500 at a time.

    I was wondering how SharePoint Developers accomplished that.

    Sorry forgot to answer your question

    for(var i=0; i< results.m_value.ResultTables.length; i++)
              {
              results.m_value.ResultTables[i].ResultRows.forEach(function (row)
       
          
              {
            var Aname1 = row.item;
               
                     
                      if (!$isNull(Aname1))
                        
                   {
                   
                       var item= Aname1;
                       
                    // console.log(item);
                    
                     // console.log(row);

                     
                   }
            });
    }

    The answer is in the code above.

    Thanks





    • Edited by 123Mn123 Friday, November 16, 2018 2:51 PM
    Thursday, November 15, 2018 1:54 PM