diff --git a/readthedocs/core/static-src/core/js/doc-embed/search.js b/readthedocs/core/static-src/core/js/doc-embed/search.js index c11a9714361..fa95ee49191 100644 --- a/readthedocs/core/static-src/core/js/doc-embed/search.js +++ b/readthedocs/core/static-src/core/js/doc-embed/search.js @@ -23,44 +23,44 @@ function attach_elastic_search_query(data) { search_url.href = api_host; search_url.pathname = '/api/v2/docsearch/'; - search_url.search = '?q=' + $.urlencode(query) + '&project=' + project + - '&version=' + version + '&language=' + language; + search_url.search = '?query=' + $.urlencode(query) + '&project=' + project + + '&version=' + version + '&language=' + language; search_def - .then(function (results) { - var hits = results.hits || {}; - var hit_list = hits.hits || []; + .then(function (data) { + var hit_list = data.results || []; + var total_count = data.count || 0; if (hit_list.length) { - for (var n in hit_list) { - var hit = hit_list[n]; - var fields = hit.fields || {}; + for (var i = 0; i < hit_list.length; i += 1) { + var doc = hit_list[i]; + var highlight = doc.highlight; var list_item = $('
  • '); - var item_url = document.createElement('a'); - var highlight = hit.highlight; - - item_url.href += fields.link + - DOCUMENTATION_OPTIONS.FILE_SUFFIX; - item_url.search = '?highlight=' + $.urlencode(query); - - // Result list elements - list_item.append( - $('') - .attr('href', item_url) - .html(fields.title) - ); - // fields.project is returned as an array - if (fields.project.indexOf(project) === -1) { - list_item.append( - $('') - .text(" (from project " + fields.project + ")") - ); + + // Creating the result from elements + var link = doc.link + DOCUMENTATION_OPTIONS.FILE_SUFFIX + + '?highlight=' + $.urlencode(query); + + var item = $('', {'href': link}); + item.html(doc.title); + list_item.append(item); + + // If the document is from subproject, add extra information + if (doc.project !== project) { + var text = " (from project " + doc.project + ")"; + var extra = $('', {'text': text}); + + list_item.append(extra); } - if (highlight.content.length) { - var content = $('
    ') - .html(xss(highlight.content[0])); - content.find('em').addClass('highlighted'); - list_item.append(content); + + // Show highlighted texts + if (highlight.content) { + var content_text = xss(highlight.content[0]); + var contents = $('
    '); + + contents.html(content_text); + contents.find('em').addClass('highlighted'); + list_item.append(contents); } Search.output.append(list_item); @@ -74,7 +74,7 @@ function attach_elastic_search_query(data) { } else { Search.status.text( - _('Search finished, found %s page(s) matching the search query.').replace('%s', hit_list.length) + _('Search finished, found %s page(s) matching the search query.').replace('%s', total_count) ); } }) @@ -96,11 +96,10 @@ function attach_elastic_search_query(data) { withCredentials: true, }, complete: function (resp, status_code) { - if (typeof (resp.responseJSON) === 'undefined' || - typeof (resp.responseJSON.results) === 'undefined') { + if (status_code !== 'success' || resp.responseJSON.count === 0) { return search_def.reject(); } - return search_def.resolve(resp.responseJSON.results); + return search_def.resolve(resp.responseJSON); } }) .error(function (resp, status_code, error) {