add the following fields to json search results: currentTimeUTC, responseTimeMS, docsInCollection, hits, moreResultsFollow, and docId. Changes structure of json so that now the results array is returned as an array within a dictionary (field name "results") as opposed to being the only object returned

This commit is contained in:
Daniel Steinberg
2014-03-01 11:16:17 -08:00
parent 8208178c79
commit bb5016e88b

@ -944,8 +944,7 @@ bool printSearchResultsHeader ( State0 *st ) {
// print first [ for json
if ( si->m_format == FORMAT_JSON ) {
sb->safePrintf("[\n");
return true;
sb->safePrintf("{\n");
}
// . if not matt wells we do not do ajax
@ -972,12 +971,17 @@ bool printSearchResultsHeader ( State0 *st ) {
long long globalNowMS = localToGlobalTimeMilliseconds(nowMS);
sb->safePrintf("\t<currentTimeUTC>%lu</currentTimeUTC>\n",
(long)(globalNowMS/1000));
} else if ( si->m_format == FORMAT_JSON ) {
long long globalNowMS = localToGlobalTimeMilliseconds(nowMS);
sb->safePrintf("\"currentTimeUTC\":%lu,\n", (long)(globalNowMS/1000));
}
// show response time
if ( si->m_format == FORMAT_XML )
sb->safePrintf("\t<responseTimeMS>%lli</responseTimeMS>\n",
st->m_took);
else if ( si->m_format == FORMAT_JSON )
sb->safePrintf("\"responseTimeMS\":%lli,\n", st->m_took);
// out of memory allocating msg20s?
if ( st->m_errno ) {
@ -1028,9 +1032,13 @@ bool printSearchResultsHeader ( State0 *st ) {
//if ( base ) docsInColl = base->getNumGlobalRecs();
docsInColl = g_hostdb.getNumGlobalRecs ( );
// include number of docs in the collection corpus
if ( si->m_format == FORMAT_XML && docsInColl >= 0LL )
sb->safePrintf ( "\t<docsInCollection>%lli"
"</docsInCollection>\n", docsInColl );
if ( docsInColl >= 0LL ) {
if ( si->m_format == FORMAT_XML)
sb->safePrintf ( "\t<docsInCollection>%lli"
"</docsInCollection>\n", docsInColl );
else if ( si->m_format == FORMAT_JSON)
sb->safePrintf("\"docsInCollection\":%lli,\n", docsInColl);
}
long numResults = msg40->getNumResults();
bool moreFollow = msg40->moreResultsFollow();
@ -1045,6 +1053,15 @@ bool printSearchResultsHeader ( State0 *st ) {
,(long long)totalHits
,(long)moreFollow
);
else if ( si->m_format == FORMAT_JSON ) {
sb->safePrintf("\"hits\":%lli,\n", (long long)totalHits);
sb->safePrintf("\"moreResultsFollow\":%li,\n", (long)moreFollow);
}
if ( si->m_format == FORMAT_JSON ) {
sb->safePrintf("\"results\":[\n");
return true;
}
// . did he get a spelling recommendation?
// . do not use htmlEncode() on this anymore since receiver
@ -1580,6 +1597,7 @@ bool printSearchResultsTail ( State0 *st ) {
if ( si->m_format == FORMAT_JSON ) {
// print ending ] for json
sb->safePrintf("]\n");
sb->safePrintf("}\n");
// all done for json
return true;
}
@ -2097,6 +2115,7 @@ bool printResult ( State0 *st, long ix ) {
sb->safePrintf(",\n");
sb->safeStrcpy ( mr->ptr_content );
// . let's hack the spidertime onto the end
// . so when we sort by that using gbsortby:spiderdate
// we can ensure it is ordered correctly
@ -2106,6 +2125,7 @@ bool printResult ( State0 *st, long ix ) {
*end == '}' ) {
// replace trailing } with spidertime}
sb->incrementLength(-1);
sb->safePrintf(",\"docId\":%lli\n", mr->m_docId);
// crap, we lose resolution storing as a float
// so fix that shit here...
//float f = mr->m_lastSpidered;