Skip to content

Commit 39ada00

Browse files
ericholschersafwanrahman
authored andcommitted
Merge pull request readthedocs#4340 from safwanrahman/docsearch
[Fix readthedocs#4265] Porting frontend docsearch to work with new API
1 parent 2586e15 commit 39ada00

File tree

1 file changed

+35
-36
lines changed
  • readthedocs/core/static-src/core/js/doc-embed

1 file changed

+35
-36
lines changed

readthedocs/core/static-src/core/js/doc-embed/search.js

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,44 @@ function attach_elastic_search_query(data) {
2323

2424
search_url.href = api_host;
2525
search_url.pathname = '/api/v2/docsearch/';
26-
search_url.search = '?q=' + $.urlencode(query) + '&project=' + project +
27-
'&version=' + version + '&language=' + language;
26+
search_url.search = '?query=' + $.urlencode(query) + '&project=' + project +
27+
'&version=' + version + '&language=' + language;
2828

2929
search_def
30-
.then(function (results) {
31-
var hits = results.hits || {};
32-
var hit_list = hits.hits || [];
30+
.then(function (data) {
31+
var hit_list = data.results || [];
32+
var total_count = data.count || 0;
3333

3434
if (hit_list.length) {
35-
for (var n in hit_list) {
36-
var hit = hit_list[n];
37-
var fields = hit.fields || {};
35+
for (var i = 0; i < hit_list.length; i += 1) {
36+
var doc = hit_list[i];
37+
var highlight = doc.highlight;
3838
var list_item = $('<li style="display: none;"></li>');
39-
var item_url = document.createElement('a');
40-
var highlight = hit.highlight;
41-
42-
item_url.href += fields.link +
43-
DOCUMENTATION_OPTIONS.FILE_SUFFIX;
44-
item_url.search = '?highlight=' + $.urlencode(query);
45-
46-
// Result list elements
47-
list_item.append(
48-
$('<a />')
49-
.attr('href', item_url)
50-
.html(fields.title)
51-
);
52-
// fields.project is returned as an array
53-
if (fields.project.indexOf(project) === -1) {
54-
list_item.append(
55-
$('<span>')
56-
.text(" (from project " + fields.project + ")")
57-
);
39+
40+
// Creating the result from elements
41+
var link = doc.link + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
42+
'?highlight=' + $.urlencode(query);
43+
44+
var item = $('<a>', {'href': link});
45+
item.html(doc.title);
46+
list_item.append(item);
47+
48+
// If the document is from subproject, add extra information
49+
if (doc.project !== project) {
50+
var text = " (from project " + doc.project + ")";
51+
var extra = $('<span>', {'text': text});
52+
53+
list_item.append(extra);
5854
}
59-
if (highlight.content.length) {
60-
var content = $('<div class="context">')
61-
.html(xss(highlight.content[0]));
62-
content.find('em').addClass('highlighted');
63-
list_item.append(content);
55+
56+
// Show highlighted texts
57+
if (highlight.content) {
58+
var content_text = xss(highlight.content[0]);
59+
var contents = $('<div class="context">');
60+
61+
contents.html(content_text);
62+
contents.find('em').addClass('highlighted');
63+
list_item.append(contents);
6464
}
6565

6666
Search.output.append(list_item);
@@ -74,7 +74,7 @@ function attach_elastic_search_query(data) {
7474
}
7575
else {
7676
Search.status.text(
77-
_('Search finished, found %s page(s) matching the search query.').replace('%s', hit_list.length)
77+
_('Search finished, found %s page(s) matching the search query.').replace('%s', total_count)
7878
);
7979
}
8080
})
@@ -96,11 +96,10 @@ function attach_elastic_search_query(data) {
9696
withCredentials: true,
9797
},
9898
complete: function (resp, status_code) {
99-
if (typeof (resp.responseJSON) === 'undefined' ||
100-
typeof (resp.responseJSON.results) === 'undefined') {
99+
if (status_code !== 'success' || resp.responseJSON.count === 0) {
101100
return search_def.reject();
102101
}
103-
return search_def.resolve(resp.responseJSON.results);
102+
return search_def.resolve(resp.responseJSON);
104103
}
105104
})
106105
.error(function (resp, status_code, error) {

0 commit comments

Comments
 (0)