tangled
alpha
login
or
join now
mackuba.eu
/
lycan
35
fork
atom
Don't forget to lycansubscribe
35
fork
atom
overview
issues
1
pulls
pipelines
added support for phrases in quotes
mackuba.eu
6 months ago
6885141b
eb520282
+16
-5
3 changed files
expand all
collapse all
unified
split
app
models
searchable.rb
query_parser.rb
server.rb
+1
-1
app/models/searchable.rb
···
26
scope :matching_terms, ->(terms) {
27
where(
28
(["(posts.text ~* ?)"] * (terms.length)).join(" AND "),
29
-
*(terms.map { |x| "\\y#{x}\\y" })
30
)
31
}
32
···
26
scope :matching_terms, ->(terms) {
27
where(
28
(["(posts.text ~* ?)"] * (terms.length)).join(" AND "),
29
+
*(terms.map { |x| "\\y" + x.gsub(/\s+/, "\\s+") + "\\y" })
30
)
31
}
32
+14
-3
app/query_parser.rb
···
1
class QueryParser
2
-
def parse_terms(query)
3
-
query = query.strip.gsub('%', "\\%")
4
-
query.split(/ +/)
0
0
0
0
0
0
0
0
0
0
0
5
end
6
end
···
1
class QueryParser
2
+
attr_reader :terms
3
+
4
+
def initialize(query)
5
+
@terms = []
6
+
query = query.strip
7
+
8
+
while match = query.match(/".+?"/)
9
+
range = match.begin(0)...match.end(0)
10
+
phrase = query[range][1..-2].strip
11
+
query[range] = ' '
12
+
@terms << phrase
13
+
end
14
+
15
+
@terms += query.split(/ +/)
16
end
17
end
+1
-1
app/server.rb
···
55
return json_error('MissingParameter', 'Missing "collection" parameter')
56
end
57
58
-
terms = QueryParser.new.parse_terms(params[:query])
59
60
collection = case params[:collection]
61
when 'likes' then user.likes
···
55
return json_error('MissingParameter', 'Missing "collection" parameter')
56
end
57
58
+
terms = QueryParser.new(params[:query]).terms
59
60
collection = case params[:collection]
61
when 'likes' then user.likes