couchrest_model_search 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +3 -0
- data/Gemfile.lock +1 -1
- data/VERSION +1 -1
- data/lib/couchrest_model_search.rb +32 -11
- data/spec/couchrest_model_search_spec.rb +8 -1
- metadata +3 -3
data/CHANGELOG
CHANGED
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.6
|
@@ -66,7 +66,25 @@ module CouchRest
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
+
module CouchRest
|
70
|
+
module Search
|
71
|
+
module Escape
|
72
|
+
def escape_special_characters(query)
|
73
|
+
new_query = query.dup
|
74
|
+
lucene_special_characters.map {|c| new_query.gsub!(c, %{\\} + c)}
|
75
|
+
new_query
|
76
|
+
end
|
77
|
+
|
78
|
+
def lucene_special_characters
|
79
|
+
@lucene_special_characters ||= %w[\ + - && || ! ( ) { } [ ] ^ " ~ * ? :]
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
69
85
|
class CouchRest::Database
|
86
|
+
include CouchRest::Search::Escape
|
87
|
+
|
70
88
|
def search(klass, view_fn, query, options={})
|
71
89
|
url = CouchRest.paramify_url("#{@root}/_fti/_design/#{klass}/#{view_fn}", options.merge(:q => query))
|
72
90
|
ActiveSupport::Notifications.instrument("search.lucene",
|
@@ -74,22 +92,25 @@ class CouchRest::Database
|
|
74
92
|
CouchRest.get url
|
75
93
|
end
|
76
94
|
end
|
95
|
+
|
96
|
+
def escaped_search(klass, view_fn, query, options={})
|
97
|
+
search klass, view_fn, escape_special_characters(query), options
|
98
|
+
end
|
77
99
|
end
|
78
100
|
|
79
101
|
class CouchRest::Model::Base
|
80
|
-
|
81
|
-
|
82
|
-
ret = self.database.search(self.to_s, view_fn, query, options)
|
83
|
-
ret['rows'].map {|r| self.new(r['doc'])}
|
84
|
-
end
|
102
|
+
class << self
|
103
|
+
include CouchRest::Search::Escape
|
85
104
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
105
|
+
def search(query, view_fn="by_fulltext", options={})
|
106
|
+
options[:include_docs] = true
|
107
|
+
ret = self.database.search(self.to_s, view_fn, query, options)
|
108
|
+
ret['rows'].map {|r| self.new(r['doc'])}
|
109
|
+
end
|
90
110
|
|
91
|
-
|
92
|
-
|
111
|
+
def escaped_search(query, view_fn="by_fulltext", options={})
|
112
|
+
self.search escape_special_characters(query), view_fn, options
|
113
|
+
end
|
93
114
|
end
|
94
115
|
|
95
116
|
# example search functions
|
@@ -17,12 +17,19 @@ describe "CouchrestModelSearch" do
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
describe "##escaped_search" do
|
20
|
+
describe "CouchRest::Model::Base##escaped_search" do
|
21
21
|
it "should escape the first parameter, then pass all args off to the search method" do
|
22
22
|
Article.should_receive(:search).with('hello\:there', "by_fulltext", {}).and_return nil
|
23
23
|
Article.escaped_search "hello:there"
|
24
24
|
end
|
25
25
|
end
|
26
|
+
|
27
|
+
describe "CouchRest::Database##escaped_search" do
|
28
|
+
it "should escape the third parameter, then pass all args off to the search method" do
|
29
|
+
Article.database.should_receive(:search).with(Article, "by_fulltext", 'hello\:there', {}).and_return nil
|
30
|
+
Article.database.escaped_search Article, "by_fulltext", "hello:there"
|
31
|
+
end
|
32
|
+
end
|
26
33
|
|
27
34
|
describe "overwrite design doc" do
|
28
35
|
class CustomArticle < CouchRest::Model::Base
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: couchrest_model_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dorren Chen
|