Skip to content

Commit

Permalink
Merge pull request #4340 from safwanrahman/docsearch
Browse files Browse the repository at this point in the history
[Fix #4265] Porting frontend docsearch to work with new API
  • Loading branch information
ericholscher committed Jul 16, 2018
2 parents d6638b9 + 46d72f9 commit 512138a
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions readthedocs/core/static-src/core/js/doc-embed/search.js
Expand Up @@ -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 = $('<li style="display: none;"></li>');
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(
$('<a />')
.attr('href', item_url)
.html(fields.title)
);
// fields.project is returned as an array
if (fields.project.indexOf(project) === -1) {
list_item.append(
$('<span>')
.text(" (from project " + fields.project + ")")
);

// Creating the result from elements
var link = doc.link + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
'?highlight=' + $.urlencode(query);

var item = $('<a>', {'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 = $('<span>', {'text': text});

list_item.append(extra);
}
if (highlight.content.length) {
var content = $('<div class="context">')
.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 = $('<div class="context">');

contents.html(content_text);
contents.find('em').addClass('highlighted');
list_item.append(contents);
}

Search.output.append(list_item);
Expand All @@ -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)
);
}
})
Expand All @@ -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) {
Expand Down

0 comments on commit 512138a

Please sign in to comment.