Skip to content

Commit

Permalink
Merge pull request #5919 from readthedocs/gsoc-19-indoc-search
Browse files Browse the repository at this point in the history
Merge the GSOC 2019 in-doc search changes
  • Loading branch information
ericholscher committed Jul 16, 2019
2 parents ab64918 + a457fc0 commit f203884
Show file tree
Hide file tree
Showing 34 changed files with 1,391 additions and 726 deletions.
2 changes: 1 addition & 1 deletion docs/development/search.rst
Expand Up @@ -98,7 +98,7 @@ As per requirements of `django-elasticsearch-dsl`_, it is stored in the

The fields and ES Datatypes are specified in the `PageDocument`. The indexable data is taken
from `processed_json` property of `HTMLFile`. This property provides python dictionary with
document data like `title`, `headers`, `content` etc.
document data like `title`, `sections`, `path` etc.


.. _Elasticsearch: https://www.elastic.co/products/elasticsearch
Expand Down
5 changes: 5 additions & 0 deletions media/css/readthedocs-doc-embed.css
Expand Up @@ -286,3 +286,8 @@ div.ethical-footer {
font-size: 14px;
line-height: 20px;
}

/* Margin between the search results */
.rtd_search_hits_spacing {
margin: 10px 0;
}
138 changes: 123 additions & 15 deletions readthedocs/core/static-src/core/js/doc-embed/search.js
Expand Up @@ -4,6 +4,8 @@

var rtddata = require('./rtd-data');
var xss = require('xss/lib/index');
var MAX_RESULT_PER_SECTION = 3;
var MAX_SUBSTRING_LIMIT = 100;


/*
Expand Down Expand Up @@ -35,14 +37,24 @@ function attach_elastic_search_query(data) {
for (var i = 0; i < hit_list.length; i += 1) {
var doc = hit_list[i];
var highlight = doc.highlight;
var inner_hits = doc.inner_hits || [];
var list_item = $('<li style="display: none;"></li>');

var title = doc.title;
// if highlighted title is present,
// use that.
if (highlight) {
if (highlight.title) {
title = xss(highlight.title[0]);
}
}

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

var item = $('<a>', {'href': link});
item.html(doc.title);
item.html(title);
item.find('em').addClass('highlighted');
list_item.append(item);

// If the document is from subproject, add extra information
Expand All @@ -53,19 +65,115 @@ function attach_elastic_search_query(data) {
list_item.append(extra);
}

// Show highlighted texts
if (highlight.content) {
for (var index = 0; index < highlight.content.length; index += 1) {
if (index < 3) {
// Show up to 3 results for search
var content = highlight.content[index];
var content_text = xss(content);
var contents = $('<div class="context">');

contents.html("..." + content_text + "...");
contents.find('em').addClass('highlighted');
list_item.append(contents);
for (var j = 0; j < inner_hits.length; j += 1) {

var contents = $('<div class="context">');

var section_template = '' +
'<div>' +
'<a href="<%= section_subtitle_link %>">' +
'<%= section_subtitle %>' +
'</a>' +
'</div>' +
'<% for (var i = 0; i < section_content.length; ++i) { %>' +
'<div>' +
'<%= section_content[i] %>' +
'</div>' +
'<% } %>';

var domain_template = '' +
'<div>' +
'<a href="<%= domain_subtitle_link %>">' +
'<%= domain_subtitle %>' +
'</a>' +
'</div>' +
'<span>' +
'<%= domain_content %>' +
'</span>';

// if the result is page section
if(inner_hits[j].type === "sections") {

var section = inner_hits[j];
var section_subtitle = section._source.title;
var section_subtitle_link = link + "#" + section._source.id;
var section_content = [section._source.content.substring(0, MAX_SUBSTRING_LIMIT) + " ..."];

if (section.highlight) {
if (section.highlight["sections.title"]) {
section_subtitle = xss(section.highlight["sections.title"][0]);
}

if (section.highlight["sections.content"]) {
var content = section.highlight["sections.content"];
section_content = [];
for (
var k = 0;
k < content.length && k < MAX_RESULT_PER_SECTION;
k += 1
) {
section_content.push("... " + xss(content[k]) + " ...");
}
}
}

contents.append(
$u.template(
section_template,
{
section_subtitle_link: section_subtitle_link,
section_subtitle: section_subtitle,
section_content: section_content
}
)
);
}

// if the result is a sphinx domain object
if (inner_hits[j].type === "domains") {

var domain = inner_hits[j];
var domain_subtitle = domain._source.role_name;
var domain_subtitle_link = link + "#" + domain._source.anchor;
var domain_content = "";
var domain_name = domain._source.name;

if (
typeof domain._source.display_name === "string" &&
domain._source.display_name.length >= 1
) {
domain_subtitle = "(" + domain._source.role_name + ") " + domain._source.display_name;
}

if (domain.highlight) {
if (domain.highlight["domains.name"]) {
// domain_content = type_display -- name
domain_name = xss(domain.highlight["domains.name"][0]);
}
}

// domain_content = type_display -- name -- in doc_display
domain_content = domain._source.type_display + " -- " + domain_name + " -- in " + domain._source.doc_display;

contents.append(
$u.template(
domain_template,
{
domain_subtitle_link: domain_subtitle_link,
domain_subtitle: domain_subtitle,
domain_content: domain_content
}
)
);
}

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

// Create some spacing between the results.
// Also, don't add this spacing in the last hit.
if (j !== inner_hits.length - 1) {
list_item.append($("<div class='rtd_search_hits_spacing'></div>"));
}
}

Expand Down
2 changes: 1 addition & 1 deletion readthedocs/core/static/core/js/readthedocs-doc-embed.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions readthedocs/core/templatetags/core_tags.py
Expand Up @@ -109,6 +109,14 @@ def key(d, key_name):
return d[key_name]


@register.filter
def get_key_or_none(d, key_name):
try:
return d[key_name]
except KeyError:
return None


@register.simple_tag
def readthedocs_version():
return __version__
Expand Down
2 changes: 0 additions & 2 deletions readthedocs/projects/models.py
Expand Up @@ -1236,8 +1236,6 @@ def get_processed_json(self):
file_path,
)
return {
'headers': [],
'content': '',
'path': file_path,
'title': '',
'sections': [],
Expand Down

0 comments on commit f203884

Please sign in to comment.