Skip to content

Commit 823fa1f

Browse files
committed
- Improve search so that it is an OR search by default, and allows quoted phrases for exact match
1 parent 34bdb4d commit 823fa1f

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

app/views/search.slim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
.main class=(config.sidebar ? 'with-sidebar' : 'without-sidebar')
44
form.search-form action="/_search" method="get"
5-
input.search-field type="text" name="q" value="#{params[:q]}" placeholder="Search" autofocus=true
5+
input.search-field type="text" name="q" value="#{params[:q]}" placeholder="Search (use quotes for exact match)" autofocus=true
66
input type="submit" value="Search" style='display:none'
77

88
hr

lib/madness/search.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'shellwords'
2+
13
module Madness
24
class Search
35
include ServerHelper
@@ -13,14 +15,19 @@ def index
1315

1416
def search(query)
1517
query = query.downcase
18+
words = Shellwords.split query
19+
word_count = words.count
1620
result = {}
21+
return result if words.empty?
1722

1823
index.each do |file, content|
1924
file = file.remove("#{@path}/")[0...-3]
2025
url = file_url file
2126
label = file_label file
22-
next unless content.include? query
23-
result[label] = url
27+
found = 0
28+
words.each { |word| found += 1 if content.include? word }
29+
next unless found == word_count
30+
result[label] = url
2431
end
2532

2633
result

spec/madness/search_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
results = subject.search('file').keys
3838
expect(results).to eq ["With Sorting / File 1", "With Sorting / File 2", "I Do Not Belong"]
3939
end
40+
41+
context "with a quoted query" do
42+
it "returns only pages that include the exact phrase" do
43+
expect(subject.search('the jedi').count).to be > 1
44+
expect(subject.search('"the jedi"').count).to eq 1
45+
end
46+
end
4047
end
4148

4249
end

0 commit comments

Comments
 (0)