SOLVED

SharePoint search query > Json gives al lot of unnecessary data

Highlighted
Regular Contributor

I have to (jquery) rest searches. The first one:

jquery.ajax({
       url: `${this.props.siteurl}/_api/web/lists/getbytitle('EmployeeList')/items`,
       type: "GET",
       headers:{'Accept': 'application/json; odata=verbose;'},
       success: function(resultData) {...... bla bla

This one gives a nice clean/small json results. I get my field using "resultData.d.results"

 

My second query is this:

      jquery.ajax({
        url: "https://Blabla.sharepoint.com/sites/ilss/_api/search/query?querytext='ContentType:test_matters'&selectproperties='RefinableString10%2cCreatedBy%2cCreated'&rowlimit=500'",
        type: "GET",
        headers:{'Accept': 'application/json; odata=verbose;'},                     
        success: function(resultData) {..... blabla
               

This one gives me a huge json result with too much data. I have to get my data using this:

"resultData.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results[i].Cells.results[2].Value"
 
This gives me some problems. Ain't it possible to have the output from the second query in the same format as the first one?
 
Thanks, Mike
2 Replies
Highlighted
Best Response confirmed by Mike Jansen (Regular Contributor)
Solution

Hey Mike,

 

In your first REST request, you're getting data from the list. The second one is a search API call.

The search data is as it is. It's absolutely normal thing. Nothing to do with it. But resulted data can be reduced to more friendly format on the client side with something like:

 

var endpoint = _spPageContextInfo.webServerRelativeUrl + "/_api/search/query?querytext='*'";
$.ajax({
    url: endpoint,
    type: "GET",
    headers: {
        'Accept': 'application/json; odata=verbose;'
    },
    success: function(response) {
        var results = response.d.query.PrimaryQueryResult
            .RelevantResults.Table.Rows.results
            .map(function(row) {
                return row.Cells.results.reduce(function(res, cell) {
                    res[cell.Key] = cell.Value;
                    return res;
                }, {});
            });
        console.log(results);
    }
});

Two additional pieces of advice:

- There are `nometadata` and `minimalmetadata` OData modes which can reduce response package;

  - `nometadata` and `minimalmetadata` are available in SP 2016 and SPO (2013 requires some installation on server side);

  - The output format is different for different modes, this should be taken into consideration.

- Helper libraries like PnP JS Core can work with search too, it's easier to code with such libs, also, no jQuery needed for ajax calls in this cases.

 

Cheers,

Andrew

Highlighted