Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCI & criteria support #37

Open
wants to merge 58 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
aaf800f
Mongoid 7 compatibility
tomasc May 5, 2018
65c2202
readd danger
tomasc May 5, 2018
4364224
update changelog
tomasc May 5, 2018
6a6fd8c
SCI support
tomasc May 5, 2018
9f57c79
add support for criteria
tomasc May 5, 2018
d4f634c
update README
tomasc May 5, 2018
70c7ea6
typo
tomasc May 5, 2018
8d6afe2
further explain
tomasc May 5, 2018
e6a1a09
cleanup
tomasc May 5, 2018
94a2361
mongoid-compatibility added _or_newer? in 0.5.1
tomasc May 5, 2018
878b210
add database_cleaner
tomasc May 5, 2018
8e818bd
Use database_cleaner.
dblock May 5, 2018
a6eeef9
Mark failing specs pending.
dblock May 6, 2018
82eab98
Mongoid 7 compatibility
tomasc May 5, 2018
8b3ccb5
readd danger
tomasc May 5, 2018
57abbea
update changelog
tomasc May 5, 2018
b59427d
SCI support
tomasc May 5, 2018
df1ccff
add support for criteria
tomasc May 5, 2018
9832f53
update README
tomasc May 5, 2018
a9b7c36
typo
tomasc May 5, 2018
4b21baa
further explain
tomasc May 5, 2018
13bdee3
cleanup
tomasc May 5, 2018
3c4c469
Merge branch 'sci-support' of https://github.com/tomasc/mongoid_fullt…
tomasc May 7, 2018
5dcd0cd
Mongoid 7 compatibility
tomasc May 5, 2018
20fbcdf
readd danger
tomasc May 5, 2018
1ff06cc
update changelog
tomasc May 5, 2018
6c8ba8c
mongoid-compatibility added _or_newer? in 0.5.1
tomasc May 5, 2018
6ff4ce6
add database_cleaner
tomasc May 5, 2018
71d6396
Merge branch 'master' of https://github.com/tomasc/mongoid_fulltext
tomasc May 7, 2018
7132041
make Rubocop happy
tomasc May 7, 2018
b172e64
fix RuboCop too many lines
tomasc May 7, 2018
fafbf1c
Revert "fix RuboCop too many lines"
tomasc May 7, 2018
ff04f9c
bump module length
tomasc May 7, 2018
a27b81e
Update spec_helper.rb
tomasc May 7, 2018
6098c38
Merge branch 'master' of https://github.com/tomasc/mongoid_fulltext
tomasc May 8, 2018
5b87ef2
fix
tomasc May 8, 2018
10314d5
Merge branch 'master' into sci-support
tomasc May 8, 2018
fe9f7a0
rubocop
tomasc May 8, 2018
3d212b0
changelog
tomasc May 8, 2018
7078f9a
Mongoid 7 compatibility
tomasc May 5, 2018
dc21126
update changelog
tomasc May 5, 2018
9baa190
SCI support
tomasc May 5, 2018
0af33c2
add support for criteria
tomasc May 5, 2018
880ef89
update README
tomasc May 5, 2018
7ab7164
typo
tomasc May 5, 2018
d509b5e
further explain
tomasc May 5, 2018
d278868
cleanup
tomasc May 5, 2018
ebc16e2
make Rubocop happy
tomasc May 7, 2018
dcde6b8
fix RuboCop too many lines
tomasc May 7, 2018
af8958c
Revert "fix RuboCop too many lines"
tomasc May 7, 2018
41a2fb4
bump module length
tomasc May 7, 2018
5c0946d
Update spec_helper.rb
tomasc May 7, 2018
95df357
fix
tomasc May 8, 2018
22f5942
rubocop
tomasc May 8, 2018
c28a1b6
Merge branch 'sci-support' of https://github.com/tomasc/mongoid_fullt…
tomasc May 8, 2018
d0a26ad
update changelog
tomasc May 8, 2018
f48d76d
use class attr
tomasc May 8, 2018
9f7e02c
require
tomasc May 8, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-05-08 14:54:25 +0200 using RuboCop version 0.55.0.
# on 2018-05-08 15:36:14 +0200 using RuboCop version 0.55.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -64,7 +64,7 @@ Metrics/MethodLength:
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 223
Max: 300

# Offense count: 4
Metrics/PerceivedComplexity:
Expand Down Expand Up @@ -129,7 +129,7 @@ Style/NumericPredicate:
- 'spec/**/*'
- 'lib/mongoid/full_text_search.rb'

# Offense count: 262
# Offense count: 265
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Expand Down
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
### 0.8.1 (Next)
### 0.8.3 (Next)

* [#35](https://github.com/mongoid/mongoid_fulltext/pull/35): Mongoid 7 compatibility - [@tomasc](https://github.com/tomasc).
* [#37](https://github.com/mongoid/mongoid_fulltext/pull/37): Sci & criteria support - [@tomasc](https://github.com/tomasc).
* Your contribution here.

### 0.8.2 (8/5/2018)

* [#35](https://github.com/mongoid/mongoid_fulltext/pull/35): Mongoid 7 compatibility - [@tomasc](https://github.com/tomasc).

### 0.8.0 (1/19/2017)

* [#28](https://github.com/mongoid/mongoid_fulltext/pull/28): Moved to the mongoid organization - [@dblock](https://github.com/dblock).
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,40 @@ the AND of all of the individual results for each of the fields. Finally, if a f
but criteria for that filter aren't passed to `fulltext_search`, the result is as if the filter
had never been defined - you see both models that both pass and fail the filter in the results.

SCI Support
-----------

The search respects SCI. From the spec:

```ruby
class MyDoc
include Mongoid::Document
include Mongoid::FullTextSearch

field :title
fulltext_search_in :title
end

class MyInheritedDoc < MyDoc
end
```

```ruby
MyDoc.fulltext_search(…) # => will return both MyDoc as well as MyInheritedDoc documents
MyInheritedDoc.fulltext_search(…) # => will return only MyInheritedDoc documents
```

Criteria Support
----------------

It is also possible to pre-empt the search with Monogid criteria:

```ruby
MyDoc.where(value: 10).fulltext_search(…)
```

Please note that this will not work in case an index is shared by multiple classes (that are not connected through inheritance), since a criteria applies only to one class.

Indexing Options
----------------

Expand Down
14 changes: 13 additions & 1 deletion lib/mongoid/full_text_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def fulltext_search(query_string, options = {})
coll = collection.database[index_name]
cursors = ngrams.map do |ngram|
query = { 'ngram' => ngram[0] }
query.update(document_type_filters)
query.update(map_query_filters(options))
count = coll.find(query).count
{ ngram: ngram, count: count, query: query }
Expand Down Expand Up @@ -191,7 +192,11 @@ def fulltext_search(query_string, options = {})
end

def instantiate_mapreduce_result(result)
result[:clazz].constantize.find(result[:id])
if criteria.selector.empty?
result[:clazz].constantize.find(result[:id])
else
criteria.where(_id: result[:id]).first
end
end

def instantiate_mapreduce_results(results, options)
Expand Down Expand Up @@ -295,6 +300,13 @@ def update_ngram_index

private

# add filter by type according to SCI classes
def document_type_filters
return {} unless fields['_type'].present?
kls = ([self] + descendants).map(&:to_s)
{ 'class' => { '$in' => kls } }
end

# Take a list of filters to be mapped so they can update the query
# used upon the fulltext search of the ngrams
def map_query_filters(filters)
Expand Down
9 changes: 9 additions & 0 deletions spec/models/my_doc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class MyDoc
include Mongoid::Document
include Mongoid::FullTextSearch

field :title
field :value, type: Integer

fulltext_search_in :title
end
4 changes: 4 additions & 0 deletions spec/models/my_further_inherited_doc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'models/my_inherited_doc.rb'

class MyFurtherInheritedDoc < MyInheritedDoc
end
2 changes: 2 additions & 0 deletions spec/models/my_inherited_doc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class MyInheritedDoc < MyDoc
end
14 changes: 14 additions & 0 deletions spec/mongoid/criteria_search_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

require 'spec_helper'

describe Mongoid::FullTextSearch do
context 'Criteria' do
let!(:my_doc_1) { MyDoc.create!(title: 'My Doc 1') }
let!(:my_doc_2) { MyDoc.create!(title: 'My Doc 2', value: 10) }

let(:result) { MyDoc.where(value: 10).fulltext_search('doc') }

it { expect(result).not_to include my_doc_1 }
it { expect(result).to include my_doc_2 }
end
end
31 changes: 31 additions & 0 deletions spec/mongoid/sci_search_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

require 'spec_helper'

describe Mongoid::FullTextSearch do
context 'SCI' do
let!(:my_doc) { MyDoc.create!(title: 'My Doc') }
let!(:my_inherited_doc) { MyInheritedDoc.create!(title: 'My Inherited Doc') }
let!(:my_further_inherited_doc) { MyFurtherInheritedDoc.create!(title: 'My Inherited Doc') }

context 'root class returns results for subclasses' do
let(:result) { MyDoc.fulltext_search('doc') }
it { expect(result).to include my_doc }
it { expect(result).to include my_inherited_doc }
it { expect(result).to include my_further_inherited_doc }
end

context 'child class does not return superclass' do
let(:result) { MyInheritedDoc.fulltext_search('doc') }
it { expect(result).not_to include my_doc }
it { expect(result).to include my_inherited_doc }
it { expect(result).to include my_further_inherited_doc }
end

context 'child class does not return superclass' do
let(:result) { MyFurtherInheritedDoc.fulltext_search('doc') }
it { expect(result).not_to include my_doc }
it { expect(result).not_to include my_inherited_doc }
it { expect(result).to include my_further_inherited_doc }
end
end
end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
config.connect_to('mongoid_fulltext_test')
end

Mongoid.logger.level = Logger::INFO
Mongo::Logger.logger.level = Logger::INFO if Mongoid::Compatibility::Version.mongoid5_or_newer?

RSpec.configure do |c|
c.before :each do
DatabaseCleaner.clean
Expand Down