mongomapper-search 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Fernando Meyer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = mongomapper-solr
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Fernando Meyer. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ namespace :gem do
5
+ begin
6
+ require 'jeweler'
7
+ @jeweler_tasks = Jeweler::Tasks.new do |gem|
8
+ gem.name = "mongomapper-search"
9
+ gem.summary = %Q{Easily integreate mongo mapper with enterprise search like solr}
10
+ gem.description = %Q{Easily integreate mongo mapper with enterprise search like solr}
11
+ gem.email = "fmcamargo@gmail.com"
12
+ gem.homepage = "http://github.com/fmeyer/mongomapper-search"
13
+ gem.authors = ["Fernando Meyer"]
14
+ gem.add_development_dependency "rspec", ">= 1.2.9"
15
+
16
+ gem.add_dependency "rsolr", "0.9.6"
17
+ gem.add_dependency "mongo_mapper", "0.5.8"
18
+ gem.add_dependency "will_paginate", "2.3.11"
19
+
20
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
21
+ end
22
+
23
+ rescue LoadError
24
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
25
+ end
26
+ end
27
+
28
+ begin
29
+ require 'spec/rake/spectask'
30
+ Spec::Rake::SpecTask.new do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ spec.rcov_dir = 'doc/coverage'
35
+ spec.rcov_opts = %w{--text-summary --failure-threshold 100 --exclude spec/*,gems/*,/usr/lib/ruby/* }
36
+ end
37
+ rescue LoadError
38
+ task :spec do
39
+ abort "rSpec is not available. In order to run specs, you must: sudo gem install rspec"
40
+ end
41
+ end
42
+
43
+ task :default => :spec
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "mongomapper-solr #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'mongo_mapper'
4
+ require 'rsolr'
5
+ require 'will_paginate/collection'
6
+
7
+ module Search
8
+ mattr_accessor :connection
9
+ end
10
+
11
+ require 'solr/search'
@@ -0,0 +1,47 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Search::SolrDocument
4
+ module ClassMethods
5
+ def search(query, page = 1, opts = {})
6
+ WillPaginate::Collection.create(page, 20) do |pager|
7
+ result,count = search_engine_query(query, opts)
8
+ pager.replace(result)
9
+ pager.total_entries = count
10
+ end
11
+ end
12
+
13
+ protected
14
+ def search_engine_query(query, opts = {})
15
+ opts.reverse_merge!(:limit => 20, :offset => 0)
16
+ resp = Search.connection.select(:q => query, :rows => opts[:limit], :start => opts[:offset], :qt => :dismax, :fl => '*,score')['response']
17
+ return resp['docs'].map{|doc| find(doc['id'])}, resp['numFound']
18
+ end
19
+ end
20
+
21
+ module InstanceMethods
22
+ def to_index
23
+ attrs = {'id' => self._id}
24
+ self.metaclass.keys.each_pair do |name, key|
25
+ attrs.merge!(name => self[name]) if key.options[:fulltext]
26
+ end
27
+ attrs
28
+ end
29
+
30
+ protected
31
+ def save_on_search_engine
32
+ Search.connection.add(to_index)
33
+ Search.connection.commit
34
+ end
35
+
36
+ def delete_from_search_engine
37
+ Search.connection.delete_by_id(self._id)
38
+ end
39
+ end
40
+
41
+ def self.included(receiver)
42
+ receiver.extend ClassMethods
43
+ receiver.send :include, InstanceMethods
44
+ receiver.after_save :save_on_search_engine
45
+ receiver.before_destroy :delete_from_search_engine
46
+ end
47
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Search::SolrDocument do
4
+ class Dummy
5
+ include MongoMapper::Document
6
+ include Search::SolrDocument
7
+
8
+ key :field_1, String
9
+ key :field_2, String, :fulltext => true
10
+ key :field_3, String, :fulltext => true
11
+ end
12
+
13
+ before(:each) do
14
+ @dummy = Dummy.new(:id => '123', :field_1 => 'abc', :field_2 => 'def', :field_3 => 'ghi')
15
+ Search.connection = mock('solr')
16
+ Search.connection.stub!(:add)
17
+ Search.connection.stub!(:commit)
18
+
19
+ Dummy.connection Mongo::Connection.new
20
+ Dummy.stub!(:find).with(anything()).and_return(@dummy)
21
+ end
22
+
23
+ it "should create a hash with attributes to index" do
24
+ @dummy.to_index.should have(3).fields
25
+ @dummy.to_index.should have_key('id')
26
+ @dummy.to_index.should have_key('field_2')
27
+ @dummy.to_index.should have_key('field_3')
28
+ end
29
+
30
+ it "should index and commit on save" do
31
+ Search.connection.should_receive(:add).with(@dummy.to_index)
32
+ Search.connection.should_receive(:commit)
33
+ @dummy.save
34
+ end
35
+
36
+ it "should remove from index then delete" do
37
+ Search.connection.should_receive(:delete_by_id).with(@dummy._id)
38
+ @dummy.destroy
39
+ end
40
+
41
+ it "should return instances given a text to fulltext search" do
42
+ Search.connection.
43
+ should_receive(:select).
44
+ with(hash_including(:q => 'abc')).
45
+ and_return('response' => {'docs' => [{'id' => 'a'}], 'numFound' => 1})
46
+
47
+ dummies = Dummy.search('abc')
48
+ end
49
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --format specdoc
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'mongomapper-search'
7
+
8
+ Spec::Runner.configure do |config|
9
+ MongoMapper.connection = Mongo::Connection.new("127.0.0.1")
10
+ MongoMapper.database = "dummydb"
11
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongomapper-search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fernando Meyer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-07 00:00:00 -02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rsolr
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.6
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: mongo_mapper
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.5.8
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: will_paginate
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.3.11
54
+ version:
55
+ description: Easily integreate mongo mapper with enterprise search like solr
56
+ email: fmcamargo@gmail.com
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.rdoc
64
+ files:
65
+ - .document
66
+ - .gitignore
67
+ - LICENSE
68
+ - README.rdoc
69
+ - Rakefile
70
+ - VERSION
71
+ - lib/mongomapper-search.rb
72
+ - lib/solr/search.rb
73
+ - spec/solr/search_spec.rb
74
+ - spec/spec.opts
75
+ - spec/spec_helper.rb
76
+ has_rdoc: true
77
+ homepage: http://github.com/fmeyer/mongomapper-search
78
+ licenses: []
79
+
80
+ post_install_message:
81
+ rdoc_options:
82
+ - --charset=UTF-8
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ version:
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: "0"
96
+ version:
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.3.5
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Easily integreate mongo mapper with enterprise search like solr
104
+ test_files:
105
+ - spec/solr/search_spec.rb
106
+ - spec/spec_helper.rb