Skip to content

Commit

Permalink
Improvements to search
Browse files Browse the repository at this point in the history
1. Do not remove stop words, so or/where/from are search searchable

2. When indexing the title, split IDs on dots and slashes

3. Reduce the size of the search sidebar

Closes #996.
  • Loading branch information
José Valim committed Apr 11, 2019
1 parent 981bc3d commit 37e2e78
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 15 deletions.
15 changes: 12 additions & 3 deletions assets/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,23 @@ function getProjectMeta () {
return document.head.querySelector('meta[name=project][content]').content
}

function titleExtractor(document) {
var title = document['title']

if(document['type'] != 'extras') {
title = title + ' ' + title.replace(/\.|\//g, ' ')
}

return title
}

function createIndex () {
return lunr(function () {
this.ref('ref')
this.field('title')
this.field('module')
this.field('type')
this.field('title', {extractor: titleExtractor})
this.field('doc')
this.metadataWhitelist = ['position']
this.pipeline.remove(lunr.stopWordFilter)

searchNodes.forEach(function (doc) {
this.add(doc)
Expand Down
4 changes: 3 additions & 1 deletion assets/js/templates/search-results.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<p>Invalid search: {{errorMessage}}.</p>
{{/isArray}}

<p>Here are some search tips:</p>
<p>Here are some tips when performing a full-text search:</p>

<dl>
<li>Multiple words (such as <code>foo bar</code>) are searched as <code>OR</code></li>
Expand All @@ -28,4 +28,6 @@
<li>Use <code>WORD^NUMBER</code> (such as <code>foo^2</code>) to boost the given word</li>
<li>Use <code>WORD~NUMBER</code> (such as <code>foo~2</code>) to do a search with edit distance on word</li>
</dl>

<p>To quickly go to a module, type, or function, use the autocompletion feature in the sidebar search.</p>
{{/isNonEmptyArray}}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions lib/ex_doc/formatter/html/search_items.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule ExDoc.Formatter.HTML.SearchItems do
@header_body_regex ~r/(?<header>.*)<\/h2>(?<body>.*)/s
defp extra(%{id: id, title: title, content: content}) do
[intro | sections] = Regex.split(@h2_split_regex, content)
intro_json_item = encode("#{id}.html", title, id, :extras, intro)
intro_json_item = encode("#{id}.html", title, :extras, intro)

section_json_items =
sections
Expand All @@ -27,14 +27,13 @@ defmodule ExDoc.Formatter.HTML.SearchItems do
encode(
"#{id}.html##{HTML.text_to_id(header)}",
"#{title} - #{header}",
id,
:extras,
body
)
end

defp module_node(node = %ExDoc.ModuleNode{id: id, type: type, rendered_doc: doc}) do
module = encode("#{id}.html", id, id, type, doc)
module = encode("#{id}.html", id, type, doc)
functions = Enum.map(node.docs, &node_child(&1, id))
types = Enum.map(node.typespecs, &node_child(&1, id))
[module] ++ functions ++ types
Expand All @@ -44,20 +43,17 @@ defmodule ExDoc.Formatter.HTML.SearchItems do
encode(
"#{module}.html##{HTML.link_id(id, type)}",
"#{module}.#{id}",
module,
type,
doc
)
end

defp encode(ref, title, module, type, doc) do
defp encode(ref, title, type, doc) do
[
"{\"ref\":",
[?", ref, ?"],
",\"title\":",
[?", title, ?"],
",\"module\":",
[?", module, ?"],
",\"type\":",
[?", Atom.to_string(type), ?"],
",\"doc\":",
Expand Down

0 comments on commit 37e2e78

Please sign in to comment.