couchrest_model_search 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm 1.8.7@couchrest_model_search --create
1
+ rvm default@couchrest_model_search --create
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ v0.0.5
2
+ -- new feature: `escaped_search` (see README)
3
+
1
4
  v0.0.4
2
5
 
3
6
  -- bug fix: don't update the design doc unless you absolutely have to.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- couchrest_model_search (0.0.3)
4
+ couchrest_model_search (0.0.4)
5
5
  couchrest_model (~> 1.0.0)
6
6
 
7
7
  GEM
@@ -60,7 +60,7 @@ GEM
60
60
  rake (>= 0.8.7)
61
61
  thor (~> 0.14.4)
62
62
  rake (0.9.2)
63
- rest-client (1.6.3)
63
+ rest-client (1.6.7)
64
64
  mime-types (>= 1.16)
65
65
  rspec (2.6.0)
66
66
  rspec-core (~> 2.6.0)
@@ -8,17 +8,99 @@ Search integration for `couchrest_model` users that are using couchdb-lucene.
8
8
 
9
9
  ## Usage
10
10
 
11
+ By simply requiring 'couchrest_model_search' in your project, all of your `CouchRest::Model::Base` models will respond to a `search` class method. This method will, by default, perform a full-text search on your model for any query you pass to it.
12
+
13
+ For example, suppose we have defined the following model:
14
+
15
+ class Article < CouchRest::Model::Base
16
+ property :title
17
+ property :author
18
+ property :body
19
+ end
20
+
21
+ And now, let's create some articles:
22
+
23
+ Article.create(
24
+ :title => "CouchDB is fun!",
25
+ :author => "moonmaster9000",
26
+ :body => "CouchDB is the most fun EVAR."
27
+ )
28
+
29
+ Article.create(
30
+ :title => "CouchDB Lucene FTW",
31
+ :author => "moonmaster9000",
32
+ :body => "CouchDB Lucene makes your documents easily searchable!"
33
+ )
34
+
35
+ Article.create(
36
+ :title => "Search your CouchDB documents from Ruby",
37
+ :author => "dorren",
38
+ :body => "Use the couchrest_model_search gem to search your documents from Ruby."
39
+ )
40
+
41
+ If we searched for "CouchDB", we would receive all documents we've created, since they all contained the word "CouchDB" in one of the properties:
42
+
43
+ Article.search("CouchDB").map(&:title)
44
+ #==> ["CouchDB is fun!", "CouchDB Lucene FTW", "Search your CouchDB documents from Ruby"]
45
+
46
+ Of course, searching for "moonmaster9000" would only return the first two documents:
47
+
48
+ Article.search("moonmaster9000").map(&:title)
49
+ #==> ["CouchDB is fun!", "CouchDB Lucene FTW"]
50
+
51
+
52
+ ### Creating more search functions
53
+
54
+ By default, the `search` method will use an underlying fulltext search index function to query your content. You can override the definition of this default "fulltext" search index function via the `search_by` class method:
55
+
11
56
  class Article < CouchRest::Model::Base
57
+ property :title
58
+ property :author
59
+ property :body
60
+
61
+ search_by :fulltext,
62
+ :index => %(
63
+ function(doc){
64
+ //... your code here
65
+ }
66
+ )
12
67
  end
13
68
 
14
- @articles = Article.search "apple" # search by keyword apple
15
-
16
- # you can also customize the search function.
17
- class CustomArticle < CouchRest::Model::Base
18
- search_by :fulltext,
19
- :index => %(
20
- function(doc) {
21
- // ....
22
- }
23
- )
69
+ You can also create other search indexes. For example, suppose we'd like to index only the titles of articles for search:
70
+
71
+
72
+ class Article < CouchRest::Model::Base
73
+ property :title
74
+ property :author
75
+ property :body
76
+
77
+ search_by :title,
78
+ :index => %(
79
+ function(doc){
80
+ var contentToIndex = new Document();
81
+ contentToIndex.add(doc.title);
82
+ return contentToIndex;
83
+ }
84
+ )
24
85
  end
86
+
87
+ Now, to perform a search by title (instead of a fulltext search), simply pass `:by_title` along with your query to the `search` method:
88
+
89
+ Article.search "some query", :by_title
90
+
91
+ ### CouchDB-Lucene Query options
92
+
93
+ You can pass any CouchDB-Lucene query options to your search via an optional third parameter to search:
94
+
95
+ Article.search "apples", :fulltext, :limit => 10
96
+
97
+
98
+ ### Escaping special characters
99
+
100
+ The following characters have special meaning to Lucene when added to a query:
101
+
102
+ + - && || ! ( ) { } [ ] ^ " ~ * ? : \
103
+
104
+ By default, `couchrest_model_search` will not escape your query. If you want it to automatically escape these special characters in your query, then use the `escaped_search` method:
105
+
106
+ Article.escaped_search "2001:March:10"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
@@ -82,6 +82,15 @@ class CouchRest::Model::Base
82
82
  ret = self.database.search(self.to_s, view_fn, query, options)
83
83
  ret['rows'].map {|r| self.new(r['doc'])}
84
84
  end
85
+
86
+ def self.escaped_search(query, view_fn="by_fulltext", options={})
87
+ self.lucene_special_characters.map {|c| query.gsub!(c, %{\\} + c)}
88
+ self.search query, view_fn, options
89
+ end
90
+
91
+ def self.lucene_special_characters
92
+ @lucene_special_characters ||= %w[\ + - && || ! ( ) { } [ ] ^ " ~ * ? :]
93
+ end
85
94
 
86
95
  # example search functions
87
96
  # class Aritlce
@@ -16,6 +16,13 @@ describe "CouchrestModelSearch" do
16
16
  Article.design_doc["fulltext"].should_not be_nil
17
17
  end
18
18
  end
19
+
20
+ describe "##escaped_search" do
21
+ it "should escape the first parameter, then pass all args off to the search method" do
22
+ Article.should_receive(:search).with('hello\:there', "by_fulltext", {}).and_return nil
23
+ Article.escaped_search "hello:there"
24
+ end
25
+ end
19
26
 
20
27
  describe "overwrite design doc" do
21
28
  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: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dorren Chen
@@ -16,8 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-10-18 00:00:00 -04:00
20
- default_executable:
19
+ date: 2010-10-18 00:00:00 Z
21
20
  dependencies:
22
21
  - !ruby/object:Gem::Dependency
23
22
  name: couchrest_model
@@ -88,7 +87,6 @@ files:
88
87
  - lib/couchrest_model_search.rb
89
88
  - spec/couchrest_model_search_spec.rb
90
89
  - spec/spec_helper.rb
91
- has_rdoc: true
92
90
  homepage: http://github.com/dorren/couchrest_model_search
93
91
  licenses: []
94
92
 
@@ -118,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
116
  requirements: []
119
117
 
120
118
  rubyforge_project:
121
- rubygems_version: 1.5.2
119
+ rubygems_version: 1.8.10
122
120
  signing_key:
123
121
  specification_version: 3
124
122
  summary: Add couchdb-lucene search support to CouchRest Model