mongodb_meilisearch 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.changelog_entries/1d952700c037533909615648b5f10582.json +1 -0
- data/.changelog_entries/config.json +1 -0
- data/Gemfile.lock +4 -1
- data/README.md +69 -17
- data/lib/mongodb_meilisearch/version.rb +1 -1
- data/lib/search/class_methods.rb +6 -1
- data/lib/search/client.rb +2 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d42d4314105bccabf3b1cc8dc49c1e7c578cc854d213f8fb73cde81aba26c03
|
4
|
+
data.tar.gz: 0dc187a25352ae68526a389d8e0deed4ba9772ff94a3a6fb2bf001e67f8ccbcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13f4ab53d0794010d49bcbdf325db757ceae92cbcce77c77c7dccc99fc4436312b9d786a79fe62609363a8f83372c025374f73cc967261cda231f9624b0e0672
|
7
|
+
data.tar.gz: ef36cb85710a0bcbe4c47747686aa19d0a7d6748000e4f8ed73e1682a01ec7c4166cffc7e0984faeb7d3b25f9c64288943a828888bcb161d24d2d2268ddf9974
|
@@ -0,0 +1 @@
|
|
1
|
+
{"type":"Documented","tickets":[],"description":"README: improved example code.","tags":[]}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"git_add":true}
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mongodb_meilisearch (1.0.
|
4
|
+
mongodb_meilisearch (1.0.1)
|
5
5
|
meilisearch
|
6
6
|
mongoid (~> 7.0)
|
7
7
|
rails
|
@@ -131,6 +131,8 @@ GEM
|
|
131
131
|
nio4r (2.5.9)
|
132
132
|
nokogiri (1.15.3-arm64-darwin)
|
133
133
|
racc (~> 1.4)
|
134
|
+
nokogiri (1.15.3-x86_64-linux)
|
135
|
+
racc (~> 1.4)
|
134
136
|
parallel (1.23.0)
|
135
137
|
parser (3.2.2.3)
|
136
138
|
ast (~> 2.4.1)
|
@@ -220,6 +222,7 @@ GEM
|
|
220
222
|
|
221
223
|
PLATFORMS
|
222
224
|
arm64-darwin-22
|
225
|
+
x86_64-linux
|
223
226
|
|
224
227
|
DEPENDENCIES
|
225
228
|
debug
|
data/README.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
# <!-- :TOC: -->
|
2
|
+
- [MongodbMeilisearch](#mongodbmeilisearch)
|
3
|
+
- [Installation](#installation)
|
4
|
+
- [Usage](#usage)
|
5
|
+
- [Model Integration](#model-integration)
|
6
|
+
- [Indexes](#indexes)
|
7
|
+
- [Searching](#searching)
|
8
|
+
- [Development](#development)
|
9
|
+
- [License](#license)
|
10
|
+
- [Code of Conduct](#code-of-conduct)
|
11
|
+
|
1
12
|
# MongodbMeilisearch
|
2
13
|
|
3
14
|
A simple gem for integrating [Meilisearch](https://www.meilisearch.com) into Ruby† applications that are backed by [MongoDB](https://www.mongodb.com/).
|
@@ -35,7 +46,10 @@ SEARCH_ENABLED=true
|
|
35
46
|
MEILISEARCH_API_KEY=<your api key here>
|
36
47
|
MEILISEARCH_URL=http://127.0.0.1:7700
|
37
48
|
|
38
|
-
|
49
|
+
```
|
50
|
+
|
51
|
+
Optional configuration
|
52
|
+
```bash
|
39
53
|
MEILISEARCH_TIMEOUT=10
|
40
54
|
MEILISEARCH_MAX_RETRIES=2
|
41
55
|
```
|
@@ -46,8 +60,8 @@ Add the following near the top of your model. Only the `extend` and `include` li
|
|
46
60
|
This assumes your model also includes `Mongoid::Document`
|
47
61
|
|
48
62
|
```ruby
|
49
|
-
extend Search::ClassMethods
|
50
63
|
include Search::InstanceMethods
|
64
|
+
extend Search::ClassMethods
|
51
65
|
```
|
52
66
|
|
53
67
|
If you want Rails to automatically add, update, and delete records from the index, add the following to your model.
|
@@ -65,19 +79,46 @@ You can override these methods if needed, but you're unlikely to want to.
|
|
65
79
|
|
66
80
|
Assuming you've done the above a new index will be created with a name that
|
67
81
|
corresponds to your model's name, only in snake case. All of your models
|
68
|
-
|
82
|
+
fields will be indexed and [filterable](https://www.meilisearch.com/docs/learn/fine_tuning_results/filtering).
|
83
|
+
|
84
|
+
|
85
|
+
### Example Rails Model
|
86
|
+
|
87
|
+
Here's what it looks like when you put it all
|
88
|
+
together in a Rails model with the default behavior.
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
class Person
|
92
|
+
include Mongoid::Document
|
93
|
+
extend Search::ClassMethods
|
94
|
+
|
95
|
+
if Search::Client.instance.enabled?
|
96
|
+
after_create :add_to_search
|
97
|
+
after_update :update_in_search
|
98
|
+
after_destroy :remove_from_search
|
99
|
+
end
|
69
100
|
|
101
|
+
# normal Mongoid attributes
|
102
|
+
field :name, type: String
|
103
|
+
field :description, type: String
|
104
|
+
field :age, type: Integer
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
Note that that _unless you configure it otherwise_ the ids of `belongs_to` objects
|
109
|
+
will not be searchable. This is because they're random strings that no human's ever
|
110
|
+
going to be searching for, and we don't want to waste RAM or storage.
|
70
111
|
|
71
112
|
### Going Beyond The Defaults
|
72
113
|
This module strives for sensible defaults, but you can override them with the
|
73
114
|
following optional constants:
|
74
115
|
|
75
|
-
|
116
|
+
- `PRIMARY_SEARCH_KEY` - a Symbol matching one of your model's attributes
|
76
117
|
that is guaranteed unique. This defaults to `_id`
|
77
|
-
|
118
|
+
- `SEARCH_INDEX_NAME` - a String - useful if you want to have records from
|
78
119
|
multiple classes come back in the same search results. This defaults to the
|
79
120
|
underscored form of the current class name.
|
80
|
-
|
121
|
+
- `SEARCH_OPTIONS` - a hash of key value pairs in JS style
|
81
122
|
- See the [meilisearch search parameter docs](https://www.meilisearch.com/docs/reference/api/search#search-parameters) for details.
|
82
123
|
- example from [meliesearch's `multi_param_spec`](https://github.com/meilisearch/meilisearch-ruby/blob/main/spec/meilisearch/index/search/multi_params_spec.rb)
|
83
124
|
```ruby
|
@@ -89,7 +130,7 @@ following optional constants:
|
|
89
130
|
limit: 2
|
90
131
|
}
|
91
132
|
```
|
92
|
-
|
133
|
+
- `SEARCH_RANKING_RULES` - an array of strings that correspond to meilisearch rules
|
93
134
|
see [meilisearch ranking rules docs](https://www.meilisearch.com/docs/learn/core_concepts/relevancy#ranking-rules)
|
94
135
|
You probably don't want to change this.
|
95
136
|
|
@@ -126,7 +167,7 @@ as `original_document_id`. This is useful if you want to be able to retrieve the
|
|
126
167
|
You probably don't want to index _all_ the fields. For example,
|
127
168
|
unless you intend to allow users to sort by when a record was created,
|
128
169
|
there's no point in recording it's `created_at` in the search index.
|
129
|
-
It'll just waste bandwidth, memory, and disk space.
|
170
|
+
It'll just waste bandwidth, memory, and disk space.
|
130
171
|
|
131
172
|
Define a `SEARCHABLE_ATTRIBUTES` constant with an array of strings to limit things.
|
132
173
|
By default these will _also_ be the fields you can filter on. Note that
|
@@ -142,6 +183,15 @@ document's `BSON::ObjectId`.
|
|
142
183
|
SEARCHABLE_ATTRIBUTES = searchable_attributes - [:created_at]
|
143
184
|
```
|
144
185
|
|
186
|
+
#### Including Foreign Key data
|
187
|
+
If, for example, your `Person` `belongs_to: group`
|
188
|
+
and you wanted that group's id to be searchable you would include `group_id`
|
189
|
+
in the list.
|
190
|
+
|
191
|
+
If you don't specify any `SEARCHABLE_ATTRIBUTES`, the default list will
|
192
|
+
exclude any fields that are `Mongoid::Fields::ForeignKey` objects.
|
193
|
+
|
194
|
+
|
145
195
|
#### Getting Extra Specific
|
146
196
|
If your searchable data needs to by dynamically generated instead of
|
147
197
|
just taken directly from the `Mongoid::Document`'s attributes you can
|
@@ -150,8 +200,8 @@ must return a hash, and that hash must include the following keys:
|
|
150
200
|
- `"id"` - a string that uniquely identifies the record
|
151
201
|
- `"object_class"` the name of the class that this record corresponds to.
|
152
202
|
|
153
|
-
The value of `"object_class"` is usually just `self.class.name`.
|
154
|
-
|
203
|
+
The value of `"object_class"` is usually just `self.class.name`.
|
204
|
+
This is something specific to this gem, and not Meilisearch itself.
|
155
205
|
|
156
206
|
See `InstanceMethods#search_indexable_hash` for an example.
|
157
207
|
|
@@ -200,7 +250,12 @@ is potentially problematic for your users, and thus noted with a bang.
|
|
200
250
|
For example:
|
201
251
|
```ruby
|
202
252
|
MyModel.reindex # runs asyncronously
|
203
|
-
|
253
|
+
|
254
|
+
```
|
255
|
+
|
256
|
+
vs
|
257
|
+
|
258
|
+
```ruby
|
204
259
|
MyModel.reindex! # runs synchronously
|
205
260
|
```
|
206
261
|
|
@@ -303,8 +358,7 @@ E.g. `MyModel.search("search term", include_metadata: false)`
|
|
303
358
|
Search results, ids only, for a class where `CLASS_PREFIXED_SEARCH_IDS=false`.
|
304
359
|
|
305
360
|
```ruby
|
306
|
-
Note.search('foo', ids_only: true)
|
307
|
-
# returns
|
361
|
+
Note.search('foo', ids_only: true) # => returns
|
308
362
|
{
|
309
363
|
"matches" => [
|
310
364
|
"64274a5d906b1d7d02c1fcc7",
|
@@ -328,8 +382,7 @@ Without `ids_only` you get full objects in a `matches` array.
|
|
328
382
|
|
329
383
|
|
330
384
|
```ruby
|
331
|
-
Note.search('foo') # or Note.search('foo', ids_only: false)
|
332
|
-
# returns
|
385
|
+
Note.search('foo') # or Note.search('foo', ids_only: false) # => returns
|
333
386
|
{
|
334
387
|
"matches" => [
|
335
388
|
#<Note _id: 64274a5d906b1d7d02c1fcc7, created_at: 2023-03-15 00:00:00 UTC, updated_at: 2023-03-31 21:02:21.108 UTC, title: "A note from the past", body: "a body", type: "misc", context: "dachary">,
|
@@ -348,8 +401,7 @@ Note.search('foo') # or Note.search('foo', ids_only: false)
|
|
348
401
|
If `Note` records shared an index with `Task` and they both had `CLASS_PREFIXED_SEARCH_ID=true` you'd get a result like this.
|
349
402
|
|
350
403
|
```ruby
|
351
|
-
Note.search('foo')
|
352
|
-
# returns
|
404
|
+
Note.search('foo') #=> returns
|
353
405
|
{
|
354
406
|
"matches" => [
|
355
407
|
#<Note _id: 64274a5d906b1d7d02c1fcc7, created_at: 2023-03-15 00:00:00 UTC, updated_at: 2023-03-31 21:02:21.108 UTC, title: "A note from the past", body: "a body", type: "misc", context: "dachary">,
|
data/lib/search/class_methods.rb
CHANGED
@@ -339,7 +339,12 @@ module Search
|
|
339
339
|
# Please don't override this. Define SEARCHABLE_ATTRIBUTES instead.
|
340
340
|
# @return [Array[Symbol]] an array of attribute names as symbols
|
341
341
|
def default_searchable_attributes
|
342
|
-
|
342
|
+
good_fields = []
|
343
|
+
fields.each { |name, obj|
|
344
|
+
next if name == "_type"
|
345
|
+
good_fields.push(name.to_sym) unless obj.is_a? Mongoid::Fields::ForeignKey
|
346
|
+
}
|
347
|
+
good_fields
|
343
348
|
end
|
344
349
|
|
345
350
|
# This returns the list from SEARCHABLE_ATTRIBUTES if defined,
|
data/lib/search/client.rb
CHANGED
@@ -13,8 +13,8 @@ module Search
|
|
13
13
|
max_retries = ENV.fetch("MEILISEARCH_MAX_RETRIES", 2).to_i
|
14
14
|
if url.present? && api_key.present?
|
15
15
|
@client = MeiliSearch::Client.new(url, api_key,
|
16
|
-
|
17
|
-
|
16
|
+
timeout: timeout,
|
17
|
+
max_retries: max_retries)
|
18
18
|
else
|
19
19
|
Rails.logger.warn("UNABLE TO CONFIGURE SEARCH. Check env vars.")
|
20
20
|
@client = nil
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongodb_meilisearch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- masukomi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -143,6 +143,8 @@ executables: []
|
|
143
143
|
extensions: []
|
144
144
|
extra_rdoc_files: []
|
145
145
|
files:
|
146
|
+
- ".changelog_entries/1d952700c037533909615648b5f10582.json"
|
147
|
+
- ".changelog_entries/config.json"
|
146
148
|
- ".env"
|
147
149
|
- ".idea/.gitignore"
|
148
150
|
- ".idea/modules.xml"
|