clear_search 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +118 -0
- data/Rakefile +8 -0
- data/lib/clear_search/configuration.rb +11 -0
- data/lib/clear_search/models/index.rb +59 -0
- data/lib/clear_search/models/occurrence.rb +50 -0
- data/lib/clear_search/models/term.rb +33 -0
- data/lib/clear_search/railtie.rb +11 -0
- data/lib/clear_search/search.rb +77 -0
- data/lib/clear_search/searchable.rb +89 -0
- data/lib/clear_search/version.rb +5 -0
- data/lib/clear_search.rb +30 -0
- data/lib/generators/clear_search/install/install_generator.rb +36 -0
- data/lib/generators/clear_search/install/templates/install_clear_search.rb.erb +34 -0
- data/sig/clear_search.rbs +4 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1e84e9de488d035f3873aba86874c31f54ff5b896643e42c7bab1537047a8933
|
4
|
+
data.tar.gz: 661ca23b22989e24ddc285c12fbcf024cda22a0ed18ebdd1880dbc80715eb952
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 433ecea31397582cdc642d7fc290bfa76b6826a8055f98e5610cba45832d3076df54f4238fb7291e1e1adc946250e3004d0e8eddbd0859c405a77971c17d701d
|
7
|
+
data.tar.gz: 537105fc417a8099fba486f090512a5a926dbe20bcded2ea8ca3fa6c17a3cb262e3592ffa2b24ad4a64e14495dce2e3c2e67f03e8096d3ec3fd7d06ea3c9c3f3
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Andrea Fomera
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
# ClearSearch
|
2
|
+
|
3
|
+
ClearSearch is a powerful and flexible search engine for Ruby on Rails applications that supports multiple databases (SQLite, PostgreSQL, and MySQL).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'clear_search'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
$ bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Generate and run the migrations:
|
20
|
+
|
21
|
+
```bash
|
22
|
+
$ rails generate clear_search:install
|
23
|
+
$ rails db:migrate
|
24
|
+
```
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
### Basic Setup
|
29
|
+
|
30
|
+
Add the `searchable` block to your model and specify which attributes you want to index:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
class Post < ApplicationRecord
|
34
|
+
belongs_to :account
|
35
|
+
|
36
|
+
searchable do
|
37
|
+
index :title
|
38
|
+
index :content, with: %i[text prefix exact]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
The `with` option specifies which analyzers to use for the field. Available analyzers are:
|
44
|
+
|
45
|
+
- `:standard` (default) - Splits text into words
|
46
|
+
- `:text` - Similar to standard but optimized for full-text search
|
47
|
+
- `:prefix` - Enables prefix matching (e.g., "ann" matches "announcement")
|
48
|
+
- `:exact` - Exact string matching
|
49
|
+
|
50
|
+
### Searching
|
51
|
+
|
52
|
+
#### Basic Search
|
53
|
+
|
54
|
+
Search through all indexed fields:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
Post.search "announcement"
|
58
|
+
```
|
59
|
+
|
60
|
+
#### Exact Search
|
61
|
+
|
62
|
+
Use `%s{"term"}` syntax for exact matching:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
Post.search %s{"announcement"}
|
66
|
+
```
|
67
|
+
|
68
|
+
#### Field-Specific Search
|
69
|
+
|
70
|
+
Search specific fields:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
Post.search title: "announcement", content: "solid search"
|
74
|
+
```
|
75
|
+
|
76
|
+
You can also use exact matching for specific fields:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
Post.search title: %s{"announcement"}
|
80
|
+
```
|
81
|
+
|
82
|
+
### Chaining
|
83
|
+
|
84
|
+
Search results are ActiveRecord relations, so you can chain them with other scopes:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
Post.search("announcement").where(published: true).order(created_at: :desc)
|
88
|
+
```
|
89
|
+
|
90
|
+
### Indexing
|
91
|
+
|
92
|
+
Records are automatically indexed when they are created or updated. However, if you need to reindex existing records, you can use:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
# Reindex a single record
|
96
|
+
post.reindex_search
|
97
|
+
|
98
|
+
# Reindex all records for a model
|
99
|
+
Post.reindex_all
|
100
|
+
```
|
101
|
+
|
102
|
+
This is particularly useful when:
|
103
|
+
|
104
|
+
- Adding search to an existing model with data
|
105
|
+
- Modifying the search configuration
|
106
|
+
- Recovering from any indexing issues
|
107
|
+
|
108
|
+
## Development
|
109
|
+
|
110
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
|
111
|
+
|
112
|
+
## Contributing
|
113
|
+
|
114
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/afomera/clear_search.
|
115
|
+
|
116
|
+
## License
|
117
|
+
|
118
|
+
The gem is available as open source under the terms of the MIT License.
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ClearSearch
|
4
|
+
class Index < ActiveRecord::Base
|
5
|
+
self.table_name = "clear_search_indices"
|
6
|
+
|
7
|
+
has_many :terms, class_name: "ClearSearch::Term", dependent: :destroy
|
8
|
+
has_many :occurrences, class_name: "ClearSearch::Occurrence", dependent: :destroy
|
9
|
+
|
10
|
+
validates :record_type, :field, presence: true
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def update_for(record, field, content, options)
|
14
|
+
index = find_or_create_by!(
|
15
|
+
record_type: record.class.name,
|
16
|
+
field: field.to_s
|
17
|
+
)
|
18
|
+
|
19
|
+
index.update_terms(record, content, options)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def update_terms(record, content, options)
|
24
|
+
return if content.blank?
|
25
|
+
|
26
|
+
transaction do
|
27
|
+
# Clear existing occurrences for this record
|
28
|
+
occurrences.where(record_id: record.id, record_type: record.class.name).destroy_all
|
29
|
+
|
30
|
+
options[:analyzers].each do |analyzer|
|
31
|
+
terms_for_analyzer(content, analyzer).reject(&:blank?).each do |term_text|
|
32
|
+
term = terms.find_or_create_by!(text: term_text, analyzer: analyzer.to_s)
|
33
|
+
term.occurrences.create!(
|
34
|
+
record: record,
|
35
|
+
index: self,
|
36
|
+
position: 0 # For future positional search support
|
37
|
+
)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def terms_for_analyzer(content, analyzer)
|
46
|
+
case analyzer
|
47
|
+
when :text
|
48
|
+
content.to_s.downcase.split(/\W+/)
|
49
|
+
when :prefix
|
50
|
+
words = content.to_s.downcase.split(/\W+/)
|
51
|
+
words.flat_map { |word| (1..word.length).map { |i| word[0...i] } }
|
52
|
+
when :exact
|
53
|
+
[content.to_s.downcase]
|
54
|
+
else # standard
|
55
|
+
content.to_s.downcase.split(/\W+/)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ClearSearch
|
4
|
+
class Occurrence < ActiveRecord::Base
|
5
|
+
self.table_name = "clear_search_occurrences"
|
6
|
+
|
7
|
+
belongs_to :term, class_name: "ClearSearch::Term"
|
8
|
+
belongs_to :index, class_name: "ClearSearch::Index"
|
9
|
+
belongs_to :record, polymorphic: true
|
10
|
+
|
11
|
+
validates :term_id, :index_id, :record_id, :record_type, presence: true
|
12
|
+
validates :position, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
13
|
+
|
14
|
+
# Ensure uniqueness of term occurrences per record and field
|
15
|
+
validates :term_id, uniqueness: { scope: [:record_id, :record_type, :index_id] }
|
16
|
+
|
17
|
+
scope :for_record_type, ->(type) { where(record_type: type.to_s) }
|
18
|
+
|
19
|
+
def self.search_results(terms_relation)
|
20
|
+
base_query = joins(:term)
|
21
|
+
.where(term: terms_relation)
|
22
|
+
.for_record_type(terms_relation.first&.index&.record_type)
|
23
|
+
|
24
|
+
case connection.adapter_name.downcase
|
25
|
+
when 'postgresql'
|
26
|
+
subquery = base_query
|
27
|
+
.select("#{table_name}.record_id")
|
28
|
+
.select("ROW_NUMBER() OVER (PARTITION BY #{table_name}.record_id ORDER BY #{table_name}.position) as row_num")
|
29
|
+
|
30
|
+
from("(#{subquery.to_sql}) AS #{table_name}")
|
31
|
+
.where("row_num = 1")
|
32
|
+
.select(:record_id)
|
33
|
+
when 'mysql', 'mysql2'
|
34
|
+
subquery = base_query
|
35
|
+
.select("#{table_name}.record_id")
|
36
|
+
.select("ROW_NUMBER() OVER (PARTITION BY #{table_name}.record_id ORDER BY #{table_name}.position) as row_num")
|
37
|
+
|
38
|
+
from("(#{subquery.to_sql}) AS #{table_name}")
|
39
|
+
.where("row_num = 1")
|
40
|
+
.select(:record_id)
|
41
|
+
else
|
42
|
+
# Fallback to GROUP BY for SQLite and other databases
|
43
|
+
base_query
|
44
|
+
.group(:record_id)
|
45
|
+
.select(:record_id)
|
46
|
+
.order("#{table_name}.position")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ClearSearch
|
4
|
+
class Term < ActiveRecord::Base
|
5
|
+
self.table_name = "clear_search_terms"
|
6
|
+
|
7
|
+
belongs_to :index, class_name: "ClearSearch::Index"
|
8
|
+
has_many :occurrences, class_name: "ClearSearch::Occurrence", dependent: :destroy
|
9
|
+
|
10
|
+
validates :text, :analyzer, presence: true
|
11
|
+
validates :text, presence: true
|
12
|
+
validates :text, uniqueness: { scope: [:index_id, :analyzer] }, if: -> { text.present? }
|
13
|
+
|
14
|
+
scope :matching, ->(query) {
|
15
|
+
normalized_query = query.to_s.downcase
|
16
|
+
where(text: normalized_query)
|
17
|
+
}
|
18
|
+
|
19
|
+
scope :prefix_matching, ->(query) {
|
20
|
+
normalized_query = query.to_s.downcase
|
21
|
+
where("text LIKE ?", "#{normalized_query}%")
|
22
|
+
}
|
23
|
+
|
24
|
+
def self.search(query, analyzer: :standard)
|
25
|
+
case analyzer
|
26
|
+
when :prefix
|
27
|
+
prefix_matching(query)
|
28
|
+
else
|
29
|
+
matching(query)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ClearSearch
|
4
|
+
class Search
|
5
|
+
attr_reader :model_class, :query, :conditions
|
6
|
+
|
7
|
+
def initialize(model_class, query)
|
8
|
+
@model_class = model_class
|
9
|
+
parse_query(query)
|
10
|
+
end
|
11
|
+
|
12
|
+
def results
|
13
|
+
return model_class.none if query.blank? && conditions.empty?
|
14
|
+
|
15
|
+
terms_relation = build_terms_relation
|
16
|
+
occurrences = Occurrence.search_results(terms_relation)
|
17
|
+
.for_record_type(model_class.name)
|
18
|
+
|
19
|
+
model_class.where(id: occurrences.select(:record_id))
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def parse_query(input)
|
25
|
+
case input
|
26
|
+
when String
|
27
|
+
if input.start_with?('%s{') && input.end_with?('}')
|
28
|
+
# Exact search with %s{"term"}
|
29
|
+
@query = input[3..-2].tr('"', '') # Remove %s{" and "}
|
30
|
+
@analyzer = :exact
|
31
|
+
else
|
32
|
+
@query = input
|
33
|
+
@analyzer = :standard
|
34
|
+
end
|
35
|
+
@conditions = {}
|
36
|
+
when Hash
|
37
|
+
@query = nil
|
38
|
+
@conditions = input.transform_values do |value|
|
39
|
+
if value.is_a?(String) && value.start_with?('%s{') && value.end_with?('}')
|
40
|
+
{ text: value[3..-2].tr('"', ''), analyzer: :exact }
|
41
|
+
else
|
42
|
+
{ text: value, analyzer: :standard }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
else
|
46
|
+
@query = nil
|
47
|
+
@conditions = {}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def build_terms_relation
|
52
|
+
if query.present?
|
53
|
+
build_global_search_terms
|
54
|
+
else
|
55
|
+
build_field_specific_terms
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def build_global_search_terms
|
60
|
+
indices = Index.where(record_type: model_class.name)
|
61
|
+
Term.where(index: indices).search(query, analyzer: @analyzer)
|
62
|
+
end
|
63
|
+
|
64
|
+
def build_field_specific_terms
|
65
|
+
return Term.none if conditions.empty?
|
66
|
+
|
67
|
+
terms = conditions.map do |field, condition|
|
68
|
+
index = Index.find_by(record_type: model_class.name, field: field)
|
69
|
+
next Term.none unless index
|
70
|
+
|
71
|
+
Term.where(index: index).search(condition[:text], analyzer: condition[:analyzer])
|
72
|
+
end
|
73
|
+
|
74
|
+
terms.reduce(:or)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ClearSearch
|
4
|
+
module Searchable
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
class_methods do
|
8
|
+
def searchable(&block)
|
9
|
+
class_eval do
|
10
|
+
@search_config ||= SearchConfiguration.new
|
11
|
+
@search_config.instance_eval(&block)
|
12
|
+
end
|
13
|
+
|
14
|
+
include InstanceMethods
|
15
|
+
end
|
16
|
+
|
17
|
+
def search(query)
|
18
|
+
Search.new(self, query).results
|
19
|
+
end
|
20
|
+
|
21
|
+
def search_config
|
22
|
+
@search_config
|
23
|
+
end
|
24
|
+
|
25
|
+
def reindex_all
|
26
|
+
find_each do |record|
|
27
|
+
record.reindex_search
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module InstanceMethods
|
33
|
+
extend ActiveSupport::Concern
|
34
|
+
|
35
|
+
included do
|
36
|
+
after_commit :update_search_indices, on: [:create, :update]
|
37
|
+
after_commit :remove_from_search_indices, on: [:destroy]
|
38
|
+
end
|
39
|
+
|
40
|
+
def reindex_search
|
41
|
+
update_search_indices
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def remove_from_search_indices
|
47
|
+
self.class.search_config.indexed_fields.each do |field, _options|
|
48
|
+
indices = Index.where(record_type: self.class.name, field: field)
|
49
|
+
Term.where(index: indices).joins(:occurrences)
|
50
|
+
.where(occurrences: { record_id: id, record_type: self.class.name })
|
51
|
+
.destroy_all
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def update_search_indices
|
56
|
+
self.class.search_config.indexed_fields.each do |field, options|
|
57
|
+
content = extract_field_content(field)
|
58
|
+
Index.update_for(self, field, content, options) if content.present?
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def extract_field_content(field)
|
63
|
+
value = send(field)
|
64
|
+
return nil if value.nil?
|
65
|
+
|
66
|
+
if defined?(ActionText::RichText) && value.is_a?(ActionText::RichText)
|
67
|
+
return value.body.to_plain_text if value.body.present?
|
68
|
+
return nil
|
69
|
+
end
|
70
|
+
|
71
|
+
value.to_s
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class SearchConfiguration
|
76
|
+
attr_reader :indexed_fields
|
77
|
+
|
78
|
+
def initialize
|
79
|
+
@indexed_fields = {}
|
80
|
+
end
|
81
|
+
|
82
|
+
def index(field, options = {})
|
83
|
+
options[:analyzers] = [:standard] if options[:with].nil?
|
84
|
+
options[:analyzers] = Array(options[:with]) if options[:with]
|
85
|
+
@indexed_fields[field] = options
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/lib/clear_search.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "clear_search/version"
|
4
|
+
require "active_record"
|
5
|
+
require "active_support/concern"
|
6
|
+
require "rails/railtie"
|
7
|
+
require_relative "clear_search/railtie"
|
8
|
+
|
9
|
+
module ClearSearch
|
10
|
+
class Error < StandardError; end
|
11
|
+
|
12
|
+
autoload :Searchable, "clear_search/searchable"
|
13
|
+
autoload :Index, "clear_search/models/index"
|
14
|
+
autoload :Term, "clear_search/models/term"
|
15
|
+
autoload :Occurrence, "clear_search/models/occurrence"
|
16
|
+
autoload :Configuration, "clear_search/configuration"
|
17
|
+
autoload :Search, "clear_search/search"
|
18
|
+
|
19
|
+
class << self
|
20
|
+
def configuration
|
21
|
+
@configuration ||= Configuration.new
|
22
|
+
end
|
23
|
+
|
24
|
+
def configure
|
25
|
+
yield(configuration)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Removed the direct ActiveSupport.on_load call as it's now handled by the Railtie
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators"
|
4
|
+
require "rails/generators/active_record"
|
5
|
+
|
6
|
+
module ClearSearch
|
7
|
+
module Generators
|
8
|
+
class InstallGenerator < Rails::Generators::Base
|
9
|
+
include Rails::Generators::Migration
|
10
|
+
|
11
|
+
source_root File.expand_path("templates", __dir__)
|
12
|
+
|
13
|
+
def self.next_migration_number(dirname)
|
14
|
+
ActiveRecord::Generators::Base.next_migration_number(dirname)
|
15
|
+
end
|
16
|
+
|
17
|
+
def create_migration_file
|
18
|
+
migration_template(
|
19
|
+
"install_clear_search.rb.erb",
|
20
|
+
"db/migrate/install_clear_search.rb",
|
21
|
+
migration_version: migration_version
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def migration_version
|
28
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if needs_version?
|
29
|
+
end
|
30
|
+
|
31
|
+
def needs_version?
|
32
|
+
Rails::VERSION::MAJOR >= 5
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class InstallClearSearch < ActiveRecord::Migration<%= migration_version %>
|
4
|
+
def change
|
5
|
+
create_table :clear_search_indices do |t|
|
6
|
+
t.string :record_type, null: false
|
7
|
+
t.string :field, null: false
|
8
|
+
t.timestamps
|
9
|
+
|
10
|
+
t.index [:record_type, :field], unique: true
|
11
|
+
end
|
12
|
+
|
13
|
+
create_table :clear_search_terms do |t|
|
14
|
+
t.references :index, null: false, foreign_key: { to_table: :clear_search_indices }
|
15
|
+
t.string :text, null: false
|
16
|
+
t.string :analyzer, null: false, default: 'standard'
|
17
|
+
t.timestamps
|
18
|
+
|
19
|
+
t.index [:index_id, :text, :analyzer], unique: true
|
20
|
+
t.index :text
|
21
|
+
end
|
22
|
+
|
23
|
+
create_table :clear_search_occurrences do |t|
|
24
|
+
t.references :term, null: false, foreign_key: { to_table: :clear_search_terms }
|
25
|
+
t.references :index, null: false, foreign_key: { to_table: :clear_search_indices }
|
26
|
+
t.references :record, polymorphic: true, null: false
|
27
|
+
t.integer :position, null: false, default: 0
|
28
|
+
t.timestamps
|
29
|
+
|
30
|
+
t.index [:term_id, :record_type, :record_id, :index_id], unique: true, name: 'idx_clear_search_occurrences_uniqueness'
|
31
|
+
t.index [:record_type, :record_id], name: 'idx_clear_search_occurrences_record'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: clear_search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrea Fomera
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-02-07 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: rails
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '6.1'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '6.1'
|
26
|
+
description: ClearSearch provides a simple and flexible way to add full-text search
|
27
|
+
capabilities to your Rails models with support for multiple databases.
|
28
|
+
email:
|
29
|
+
- afomera@hey.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- CHANGELOG.md
|
35
|
+
- CODE_OF_CONDUCT.md
|
36
|
+
- LICENSE.txt
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- lib/clear_search.rb
|
40
|
+
- lib/clear_search/configuration.rb
|
41
|
+
- lib/clear_search/models/index.rb
|
42
|
+
- lib/clear_search/models/occurrence.rb
|
43
|
+
- lib/clear_search/models/term.rb
|
44
|
+
- lib/clear_search/railtie.rb
|
45
|
+
- lib/clear_search/search.rb
|
46
|
+
- lib/clear_search/searchable.rb
|
47
|
+
- lib/clear_search/version.rb
|
48
|
+
- lib/generators/clear_search/install/install_generator.rb
|
49
|
+
- lib/generators/clear_search/install/templates/install_clear_search.rb.erb
|
50
|
+
- sig/clear_search.rbs
|
51
|
+
homepage: https://github.com/afomera/clear_search
|
52
|
+
licenses:
|
53
|
+
- MIT
|
54
|
+
metadata:
|
55
|
+
homepage_uri: https://github.com/afomera/clear_search
|
56
|
+
source_code_uri: https://github.com/afomera/clear_search
|
57
|
+
changelog_uri: https://github.com/afomera/clear_search/blob/main/CHANGELOG.md
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 3.1.0
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubygems_version: 3.6.2
|
73
|
+
specification_version: 4
|
74
|
+
summary: A powerful search engine for Ruby on Rails applications
|
75
|
+
test_files: []
|