searchkick 4.6.3 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -1
- data/README.md +108 -55
- data/lib/searchkick/bulk_reindex_job.rb +12 -8
- data/lib/searchkick/controller_runtime.rb +40 -0
- data/lib/searchkick/index.rb +121 -54
- data/lib/searchkick/index_cache.rb +30 -0
- data/lib/searchkick/index_options.rb +16 -66
- data/lib/searchkick/indexer.rb +15 -8
- data/lib/searchkick/log_subscriber.rb +57 -0
- data/lib/searchkick/middleware.rb +1 -1
- data/lib/searchkick/model.rb +44 -47
- data/lib/searchkick/process_batch_job.rb +9 -25
- data/lib/searchkick/process_queue_job.rb +3 -2
- data/lib/searchkick/query.rb +36 -52
- data/lib/searchkick/record_data.rb +0 -1
- data/lib/searchkick/record_indexer.rb +135 -51
- data/lib/searchkick/reindex_queue.rb +25 -2
- data/lib/searchkick/reindex_v2_job.rb +10 -34
- data/lib/searchkick/relation.rb +36 -0
- data/lib/searchkick/relation_indexer.rb +150 -0
- data/lib/searchkick/results.rb +22 -23
- data/lib/searchkick/version.rb +1 -1
- data/lib/searchkick.rb +157 -82
- data/lib/tasks/searchkick.rake +6 -3
- metadata +11 -28
- data/lib/searchkick/bulk_indexer.rb +0 -173
- data/lib/searchkick/logging.rb +0 -246
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc223f746f73c44e5c6f56b5e351cb31ae5a655069d92f5bfd2a0b2e531afae8
|
4
|
+
data.tar.gz: 1969964f34a5adaf3ed746230fbef57673454f7304d890466e2814d27eb2cb47
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17259c3e071d69c3852584aba480b523211b167f1249951f32d1bd2f761fc07322144283f0ccabac4878c0a2ff2f4c9dd8def2db2beac004c449839052e503ea
|
7
|
+
data.tar.gz: 1109a53d99eb30f7e76cab8df00505c82af5e898c4e3f1821773932b8f6917e68f6bfedfa952b8e4acb25387607edf10856cc994021943671086af4d751cc728
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,32 @@
|
|
1
|
+
## 5.0.0 (2022-02-21)
|
2
|
+
|
3
|
+
- Searches now use lazy loading (similar to Active Record)
|
4
|
+
- Added `unscope` option to better support working with default scopes
|
5
|
+
- Added support for `:async` and `:queue` modes for `reindex` on relation
|
6
|
+
- Added basic protection from unfiltered parameters to `where` option
|
7
|
+
- Added `models` option to `similar` method
|
8
|
+
- Changed async full reindex to fetch ids instead of using ranges for numeric primary keys with Active Record
|
9
|
+
- Changed `searchkick_index_options` to return symbol keys (instead of mix of strings and symbols)
|
10
|
+
- Changed non-anchored regular expressions to match expected results (previously warned)
|
11
|
+
- Changed record reindex to return `true` to match model and relation reindex
|
12
|
+
- Updated async reindex job to call `search_import` for nested associations
|
13
|
+
- Fixed removing records when `should_index?` is `false` when `reindex` called on relation
|
14
|
+
- Fixed issue with `merge_mappings` for fields that use `searchkick` options
|
15
|
+
- Raise error when `search` called on relations
|
16
|
+
- Raise `ArgumentError` (instead of warning) for invalid regular expression modifiers
|
17
|
+
- Raise `ArgumentError` instead of `RuntimeError` for unknown operators
|
18
|
+
- Removed mapping of `id` to `_id` with `order` option
|
19
|
+
- Removed `wordnet` option (no longer worked)
|
20
|
+
- Removed dependency on `elasticsearch` gem (can use `elasticsearch` or `opensearch-ruby`)
|
21
|
+
- Dropped support for Elasticsearch 6
|
22
|
+
- Dropped support for Ruby < 2.6 and Active Record < 5.2
|
23
|
+
- Dropped support for NoBrainer and Cequel
|
24
|
+
- Dropped support for `faraday_middleware-aws-signers-v4` (use `faraday_middleware-aws-sigv4` instead)
|
25
|
+
|
1
26
|
## 4.6.3 (2021-11-19)
|
2
27
|
|
3
28
|
- Added support for reloadable synonyms for OpenSearch
|
4
|
-
- Added experimental support for `opensearch` gem
|
29
|
+
- Added experimental support for `opensearch-ruby` gem
|
5
30
|
- Removed `elasticsearch-xpack` dependency for reloadable synonyms
|
6
31
|
|
7
32
|
## 4.6.2 (2021-11-15)
|
data/README.md
CHANGED
@@ -20,7 +20,7 @@ Plus:
|
|
20
20
|
- autocomplete
|
21
21
|
- “Did you mean” suggestions
|
22
22
|
- supports many languages
|
23
|
-
- works with Active Record
|
23
|
+
- works with Active Record and Mongoid
|
24
24
|
|
25
25
|
Check out [Searchjoy](https://github.com/ankane/searchjoy) for analytics and [Autosuggest](https://github.com/ankane/autosuggest) for query suggestions
|
26
26
|
|
@@ -39,26 +39,34 @@ Check out [Searchjoy](https://github.com/ankane/searchjoy) for analytics and [Au
|
|
39
39
|
- [Testing](#testing)
|
40
40
|
- [Deployment](#deployment)
|
41
41
|
- [Performance](#performance)
|
42
|
-
- [
|
42
|
+
- [Advanced Search](#advanced)
|
43
43
|
- [Reference](#reference)
|
44
44
|
- [Contributing](#contributing)
|
45
45
|
|
46
|
+
Searchkick 5.0 was recently released! See [how to upgrade](#upgrading)
|
47
|
+
|
46
48
|
## Getting Started
|
47
49
|
|
48
50
|
Install [Elasticsearch](https://www.elastic.co/downloads/elasticsearch) or [OpenSearch](https://opensearch.org/downloads.html). For Homebrew, use:
|
49
51
|
|
50
52
|
```sh
|
51
|
-
brew install elasticsearch
|
52
|
-
brew services start elasticsearch
|
53
|
+
brew install elasticsearch
|
54
|
+
brew services start elasticsearch
|
55
|
+
# or
|
56
|
+
brew install opensearch
|
57
|
+
brew services start opensearch
|
53
58
|
```
|
54
59
|
|
55
|
-
Add
|
60
|
+
Add these lines to your application’s Gemfile:
|
56
61
|
|
57
62
|
```ruby
|
58
|
-
gem
|
63
|
+
gem "searchkick"
|
64
|
+
|
65
|
+
gem "elasticsearch" # select one
|
66
|
+
gem "opensearch-ruby" # select one
|
59
67
|
```
|
60
68
|
|
61
|
-
The latest version works with Elasticsearch
|
69
|
+
The latest version works with Elasticsearch 7 and 8 and OpenSearch 1. For Elasticsearch 6, use version 4.6.3 and [this readme](https://github.com/ankane/searchkick/blob/v4.6.3/README.md).
|
62
70
|
|
63
71
|
Add searchkick to models you want to search.
|
64
72
|
|
@@ -83,7 +91,7 @@ products.each do |product|
|
|
83
91
|
end
|
84
92
|
```
|
85
93
|
|
86
|
-
Searchkick supports the complete [Elasticsearch Search API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html). As your search becomes more advanced, we recommend you use the [
|
94
|
+
Searchkick supports the complete [Elasticsearch Search API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html) and [OpenSearch Search API](https://opensearch.org/docs/latest/opensearch/rest-api/search/). As your search becomes more advanced, we recommend you use the [search server DSL](#advanced) for maximum flexibility.
|
87
95
|
|
88
96
|
## Querying
|
89
97
|
|
@@ -144,7 +152,7 @@ select: [:name]
|
|
144
152
|
|
145
153
|
### Results
|
146
154
|
|
147
|
-
Searches return a `Searchkick::
|
155
|
+
Searches return a `Searchkick::Relation` object. This responds like an array to most methods.
|
148
156
|
|
149
157
|
```ruby
|
150
158
|
results = Product.search("milk")
|
@@ -153,7 +161,7 @@ results.any?
|
|
153
161
|
results.each { |result| ... }
|
154
162
|
```
|
155
163
|
|
156
|
-
By default, ids are fetched from
|
164
|
+
By default, ids are fetched from the search server and records are fetched from your database. To fetch everything from the search server, use:
|
157
165
|
|
158
166
|
```ruby
|
159
167
|
Product.search("apples", load: false)
|
@@ -171,13 +179,13 @@ Get the time the search took (in milliseconds)
|
|
171
179
|
results.took
|
172
180
|
```
|
173
181
|
|
174
|
-
Get the full response from
|
182
|
+
Get the full response from the search server
|
175
183
|
|
176
184
|
```ruby
|
177
185
|
results.response
|
178
186
|
```
|
179
187
|
|
180
|
-
**Note:** By default, Elasticsearch [
|
188
|
+
**Note:** By default, Elasticsearch and OpenSearch [limit paging](#deep-paging) to the first 10,000 results for performance. This applies to the total count as well.
|
181
189
|
|
182
190
|
### Boosting
|
183
191
|
|
@@ -375,9 +383,9 @@ search_synonyms: ["lightbulb => halogenlamp"]
|
|
375
383
|
|
376
384
|
The above approach works well when your synonym list is static, but in practice, this is often not the case. When you analyze search conversions, you often want to add new synonyms without a full reindex.
|
377
385
|
|
378
|
-
#### Elasticsearch 7.3+
|
386
|
+
#### Elasticsearch 7.3+ and OpenSearch
|
379
387
|
|
380
|
-
For Elasticsearch 7.3+
|
388
|
+
For Elasticsearch 7.3+ and OpenSearch, we recommend placing synonyms in a file on the search server (in the `config` directory). This allows you to reload synonyms without reindexing.
|
381
389
|
|
382
390
|
```txt
|
383
391
|
pop, soda
|
@@ -485,7 +493,7 @@ Search :ice_cream::cake: and get `ice cream cake`!
|
|
485
493
|
Add this line to your application’s Gemfile:
|
486
494
|
|
487
495
|
```ruby
|
488
|
-
gem
|
496
|
+
gem "gemoji-parser"
|
489
497
|
```
|
490
498
|
|
491
499
|
And use:
|
@@ -520,12 +528,10 @@ class Product < ApplicationRecord
|
|
520
528
|
end
|
521
529
|
```
|
522
530
|
|
523
|
-
By default, all records are indexed. To control which records are indexed, use the `should_index?` method
|
531
|
+
By default, all records are indexed. To control which records are indexed, use the `should_index?` method.
|
524
532
|
|
525
533
|
```ruby
|
526
534
|
class Product < ApplicationRecord
|
527
|
-
scope :search_import, -> { where(active: true) }
|
528
|
-
|
529
535
|
def should_index?
|
530
536
|
active # only index active records
|
531
537
|
end
|
@@ -618,6 +624,28 @@ class Image < ApplicationRecord
|
|
618
624
|
end
|
619
625
|
```
|
620
626
|
|
627
|
+
### Default Scopes
|
628
|
+
|
629
|
+
If you have a default scope that filters records, use the `should_index?` method to exclude them from indexing:
|
630
|
+
|
631
|
+
```ruby
|
632
|
+
class Product < ApplicationRecord
|
633
|
+
default_scope { where(deleted_at: nil) }
|
634
|
+
|
635
|
+
def should_index?
|
636
|
+
deleted_at.nil?
|
637
|
+
end
|
638
|
+
end
|
639
|
+
```
|
640
|
+
|
641
|
+
If you want to index and search filtered records, set:
|
642
|
+
|
643
|
+
```ruby
|
644
|
+
class Product < ApplicationRecord
|
645
|
+
searchkick unscope: true
|
646
|
+
end
|
647
|
+
```
|
648
|
+
|
621
649
|
## Intelligent Search
|
622
650
|
|
623
651
|
The best starting point to improve your search **by far** is to track searches and conversions. [Searchjoy](https://github.com/ankane/searchjoy) makes it easy.
|
@@ -1043,13 +1071,13 @@ Product.search("soap", debug: true)
|
|
1043
1071
|
|
1044
1072
|
This prints useful info to `stdout`.
|
1045
1073
|
|
1046
|
-
See how
|
1074
|
+
See how the search server scores your queries with:
|
1047
1075
|
|
1048
1076
|
```ruby
|
1049
1077
|
Product.search("soap", explain: true).response
|
1050
1078
|
```
|
1051
1079
|
|
1052
|
-
See how
|
1080
|
+
See how the search server tokenizes your queries with:
|
1053
1081
|
|
1054
1082
|
```ruby
|
1055
1083
|
Product.search_index.tokens("Dish Washer Soap", analyzer: "searchkick_index")
|
@@ -1223,7 +1251,7 @@ And [setup-opensearch](https://github.com/ankane/setup-opensearch) for an easy w
|
|
1223
1251
|
|
1224
1252
|
## Deployment
|
1225
1253
|
|
1226
|
-
Searchkick uses `ENV["ELASTICSEARCH_URL"]` for
|
1254
|
+
For the search server, Searchkick uses `ENV["ELASTICSEARCH_URL"]` for Elasticsearch and `ENV["OPENSEARCH_URL"]` for OpenSearch. This defaults to `http://localhost:9200`.
|
1227
1255
|
|
1228
1256
|
- [Elastic Cloud](#elastic-cloud)
|
1229
1257
|
- [Heroku](#heroku)
|
@@ -1248,13 +1276,20 @@ rake searchkick:reindex:all
|
|
1248
1276
|
|
1249
1277
|
Choose an add-on: [Bonsai](https://elements.heroku.com/addons/bonsai), [SearchBox](https://elements.heroku.com/addons/searchbox), or [Elastic Cloud](https://elements.heroku.com/addons/foundelasticsearch).
|
1250
1278
|
|
1251
|
-
For Bonsai:
|
1279
|
+
For Elasticsearch on Bonsai:
|
1252
1280
|
|
1253
1281
|
```sh
|
1254
|
-
heroku addons:create bonsai
|
1282
|
+
heroku addons:create bonsai
|
1255
1283
|
heroku config:set ELASTICSEARCH_URL=`heroku config:get BONSAI_URL`
|
1256
1284
|
```
|
1257
1285
|
|
1286
|
+
For OpenSearch on Bonsai:
|
1287
|
+
|
1288
|
+
```sh
|
1289
|
+
heroku addons:create bonsai --engine=opensearch
|
1290
|
+
heroku config:set OPENSEARCH_URL=`heroku config:get BONSAI_URL`
|
1291
|
+
```
|
1292
|
+
|
1258
1293
|
For SearchBox:
|
1259
1294
|
|
1260
1295
|
```sh
|
@@ -1289,16 +1324,16 @@ heroku run rake searchkick:reindex:all
|
|
1289
1324
|
|
1290
1325
|
### Amazon OpenSearch Service
|
1291
1326
|
|
1292
|
-
Create an initializer `config/initializers/
|
1327
|
+
Create an initializer `config/initializers/opensearch.rb` with:
|
1293
1328
|
|
1294
1329
|
```ruby
|
1295
|
-
ENV["
|
1330
|
+
ENV["OPENSEARCH_URL"] = "https://es-domain-1234.us-east-1.es.amazonaws.com:443"
|
1296
1331
|
```
|
1297
1332
|
|
1298
1333
|
To use signed requests, include in your Gemfile:
|
1299
1334
|
|
1300
1335
|
```ruby
|
1301
|
-
gem
|
1336
|
+
gem "faraday_middleware-aws-sigv4"
|
1302
1337
|
```
|
1303
1338
|
|
1304
1339
|
and add to your initializer:
|
@@ -1319,10 +1354,12 @@ rake searchkick:reindex:all
|
|
1319
1354
|
|
1320
1355
|
### Self-Hosted and Other
|
1321
1356
|
|
1322
|
-
Create an initializer
|
1357
|
+
Create an initializer with:
|
1323
1358
|
|
1324
1359
|
```ruby
|
1325
1360
|
ENV["ELASTICSEARCH_URL"] = "https://user:password@host:port"
|
1361
|
+
# or
|
1362
|
+
ENV["OPENSEARCH_URL"] = "https://user:password@host:port"
|
1326
1363
|
```
|
1327
1364
|
|
1328
1365
|
Then deploy and reindex:
|
@@ -1333,9 +1370,9 @@ rake searchkick:reindex:all
|
|
1333
1370
|
|
1334
1371
|
### Data Protection
|
1335
1372
|
|
1336
|
-
We recommend encrypting data at rest and in transit (even inside your own network). This is especially important if you send [personal data](https://en.wikipedia.org/wiki/Personally_identifiable_information) of your users to
|
1373
|
+
We recommend encrypting data at rest and in transit (even inside your own network). This is especially important if you send [personal data](https://en.wikipedia.org/wiki/Personally_identifiable_information) of your users to the search server.
|
1337
1374
|
|
1338
|
-
Bonsai, Elastic Cloud, and Amazon
|
1375
|
+
Bonsai, Elastic Cloud, and Amazon OpenSearch Service all support encryption at rest and HTTPS.
|
1339
1376
|
|
1340
1377
|
### Automatic Failover
|
1341
1378
|
|
@@ -1368,7 +1405,7 @@ See [Production Rails](https://github.com/ankane/production_rails) for other goo
|
|
1368
1405
|
Significantly increase performance with faster JSON generation. Add [Oj](https://github.com/ohler55/oj) to your Gemfile.
|
1369
1406
|
|
1370
1407
|
```ruby
|
1371
|
-
gem
|
1408
|
+
gem "oj"
|
1372
1409
|
```
|
1373
1410
|
|
1374
1411
|
This speeds up all JSON generation and parsing in your application (automatically!)
|
@@ -1378,7 +1415,7 @@ This speeds up all JSON generation and parsing in your application (automaticall
|
|
1378
1415
|
Significantly increase performance with persistent HTTP connections. Add [Typhoeus](https://github.com/typhoeus/typhoeus) to your Gemfile and it’ll automatically be used.
|
1379
1416
|
|
1380
1417
|
```ruby
|
1381
|
-
gem
|
1418
|
+
gem "typhoeus"
|
1382
1419
|
```
|
1383
1420
|
|
1384
1421
|
To reduce log noise, create an initializer with:
|
@@ -1447,7 +1484,7 @@ Product.reindex(async: {wait: true})
|
|
1447
1484
|
You can use [ActiveJob::TrafficControl](https://github.com/nickelser/activejob-traffic_control) to control concurrency. Install the gem:
|
1448
1485
|
|
1449
1486
|
```ruby
|
1450
|
-
gem
|
1487
|
+
gem "activejob-traffic_control", ">= 0.1.3"
|
1451
1488
|
```
|
1452
1489
|
|
1453
1490
|
And create an initializer with:
|
@@ -1510,7 +1547,7 @@ For more tips, check out [Keeping Elasticsearch in Sync](https://www.elastic.co/
|
|
1510
1547
|
|
1511
1548
|
### Routing
|
1512
1549
|
|
1513
|
-
Searchkick supports [
|
1550
|
+
Searchkick supports [routing](https://www.elastic.co/blog/customizing-your-document-routing), which can significantly speed up searches.
|
1514
1551
|
|
1515
1552
|
```ruby
|
1516
1553
|
class Business < ApplicationRecord
|
@@ -1618,7 +1655,7 @@ ReindexConversionsJob.perform_later("Product")
|
|
1618
1655
|
|
1619
1656
|
## Advanced
|
1620
1657
|
|
1621
|
-
Searchkick makes it easy to use the Elasticsearch DSL on its own.
|
1658
|
+
Searchkick makes it easy to use the Elasticsearch or OpenSearch DSL on its own.
|
1622
1659
|
|
1623
1660
|
### Advanced Mapping
|
1624
1661
|
|
@@ -1672,7 +1709,7 @@ products =
|
|
1672
1709
|
end
|
1673
1710
|
```
|
1674
1711
|
|
1675
|
-
###
|
1712
|
+
### Client
|
1676
1713
|
|
1677
1714
|
Searchkick is built on top of the [elasticsearch](https://github.com/elastic/elasticsearch-ruby) gem. To access the client directly, use:
|
1678
1715
|
|
@@ -1685,8 +1722,8 @@ Searchkick.client
|
|
1685
1722
|
To batch search requests for performance, use:
|
1686
1723
|
|
1687
1724
|
```ruby
|
1688
|
-
products = Product.search("snacks"
|
1689
|
-
coupons = Coupon.search("snacks"
|
1725
|
+
products = Product.search("snacks")
|
1726
|
+
coupons = Coupon.search("snacks")
|
1690
1727
|
Searchkick.multi_search([products, coupons])
|
1691
1728
|
```
|
1692
1729
|
|
@@ -1737,7 +1774,7 @@ products.clear_scroll
|
|
1737
1774
|
|
1738
1775
|
## Deep Paging
|
1739
1776
|
|
1740
|
-
By default, Elasticsearch
|
1777
|
+
By default, Elasticsearch and OpenSearch limit paging to the first 10,000 results. [Here’s why](https://www.elastic.co/guide/en/elasticsearch/guide/current/pagination.html). We don’t recommend changing this, but if you really need all results, you can use:
|
1741
1778
|
|
1742
1779
|
```ruby
|
1743
1780
|
class Product < ApplicationRecord
|
@@ -1745,7 +1782,7 @@ class Product < ApplicationRecord
|
|
1745
1782
|
end
|
1746
1783
|
```
|
1747
1784
|
|
1748
|
-
If you just need an accurate total count
|
1785
|
+
If you just need an accurate total count, you can instead use:
|
1749
1786
|
|
1750
1787
|
```ruby
|
1751
1788
|
Product.search("pears", body_options: {track_total_hits: true})
|
@@ -1960,13 +1997,6 @@ class Product < ApplicationRecord
|
|
1960
1997
|
end
|
1961
1998
|
```
|
1962
1999
|
|
1963
|
-
Lazy searching
|
1964
|
-
|
1965
|
-
```ruby
|
1966
|
-
products = Product.search("carrots", execute: false)
|
1967
|
-
products.each { ... } # search not executed until here
|
1968
|
-
```
|
1969
|
-
|
1970
2000
|
Add [request parameters](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html#search-search-api-query-params) like `search_type`
|
1971
2001
|
|
1972
2002
|
```ruby
|
@@ -2004,22 +2034,17 @@ Turn on misspellings after a certain number of characters
|
|
2004
2034
|
Product.search "api", misspellings: {prefix_length: 2} # api, apt, no ahi
|
2005
2035
|
```
|
2006
2036
|
|
2007
|
-
**Note:** With this option, if the query length is the same as `prefix_length`, misspellings are turned off
|
2037
|
+
**Note:** With this option, if the query length is the same as `prefix_length`, misspellings are turned off with Elasticsearch 7 and OpenSearch
|
2008
2038
|
|
2009
2039
|
```ruby
|
2010
2040
|
Product.search "ah", misspellings: {prefix_length: 2} # ah, no aha
|
2011
2041
|
```
|
2012
2042
|
|
2013
|
-
##
|
2014
|
-
|
2015
|
-
1. Install Searchkick 4
|
2016
|
-
2. Upgrade your Elasticsearch cluster
|
2017
|
-
|
2018
|
-
## Elasticsearch Gotchas
|
2043
|
+
## Gotchas
|
2019
2044
|
|
2020
2045
|
### Consistency
|
2021
2046
|
|
2022
|
-
Elasticsearch
|
2047
|
+
Elasticsearch and OpenSearch are eventually consistent, meaning it can take up to a second for a change to reflect in search. You can use the `refresh` method to have it show up immediately.
|
2023
2048
|
|
2024
2049
|
```ruby
|
2025
2050
|
product.save!
|
@@ -2028,7 +2053,7 @@ Product.search_index.refresh
|
|
2028
2053
|
|
2029
2054
|
### Inconsistent Scores
|
2030
2055
|
|
2031
|
-
Due to the distributed nature of Elasticsearch, you can get incorrect results when the number of documents in the index is low. You can [read more about it here](https://www.elastic.co/blog/understanding-query-then-fetch-vs-dfs-query-then-fetch). To fix this, do:
|
2056
|
+
Due to the distributed nature of Elasticsearch and OpenSearch, you can get incorrect results when the number of documents in the index is low. You can [read more about it here](https://www.elastic.co/blog/understanding-query-then-fetch-vs-dfs-query-then-fetch). To fix this, do:
|
2032
2057
|
|
2033
2058
|
```ruby
|
2034
2059
|
class Product < ApplicationRecord
|
@@ -2038,6 +2063,34 @@ end
|
|
2038
2063
|
|
2039
2064
|
For convenience, this is set by default in the test environment.
|
2040
2065
|
|
2066
|
+
## Upgrading
|
2067
|
+
|
2068
|
+
### 5.0
|
2069
|
+
|
2070
|
+
Searchkick 5 supports both the `elasticsearch` and `opensearch-ruby` gems. Add the one you want to use to your Gemfile:
|
2071
|
+
|
2072
|
+
```ruby
|
2073
|
+
gem "elasticsearch"
|
2074
|
+
# or
|
2075
|
+
gem "opensearch-ruby"
|
2076
|
+
```
|
2077
|
+
|
2078
|
+
If using the deprecated `faraday_middleware-aws-signers-v4` gem, switch to `faraday_middleware-aws-sigv4`.
|
2079
|
+
|
2080
|
+
Also, searches now use lazy loading:
|
2081
|
+
|
2082
|
+
```ruby
|
2083
|
+
# search not executed
|
2084
|
+
Product.search("milk")
|
2085
|
+
|
2086
|
+
# search executed
|
2087
|
+
Product.search("milk").to_a
|
2088
|
+
```
|
2089
|
+
|
2090
|
+
And there’s a [new option](#default-scopes) for models with default scopes.
|
2091
|
+
|
2092
|
+
Check out the [changelog](https://github.com/ankane/searchkick/blob/master/CHANGELOG.md) for the full list of changes.
|
2093
|
+
|
2041
2094
|
## History
|
2042
2095
|
|
2043
2096
|
View the [changelog](https://github.com/ankane/searchkick/blob/master/CHANGELOG.md).
|
@@ -2,16 +2,20 @@ module Searchkick
|
|
2
2
|
class BulkReindexJob < ActiveJob::Base
|
3
3
|
queue_as { Searchkick.queue_name }
|
4
4
|
|
5
|
+
# TODO remove min_id and max_id in Searchkick 6
|
5
6
|
def perform(class_name:, record_ids: nil, index_name: nil, method_name: nil, batch_id: nil, min_id: nil, max_id: nil)
|
6
|
-
|
7
|
-
index =
|
7
|
+
model = Searchkick.load_model(class_name)
|
8
|
+
index = model.searchkick_index(name: index_name)
|
9
|
+
|
10
|
+
# legacy
|
8
11
|
record_ids ||= min_id..max_id
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
)
|
12
|
+
|
13
|
+
relation = Searchkick.scope(model)
|
14
|
+
relation = Searchkick.load_records(relation, record_ids)
|
15
|
+
relation = relation.search_import if relation.respond_to?(:search_import)
|
16
|
+
|
17
|
+
RecordIndexer.new(index).reindex(relation, mode: :inline, method_name: method_name, full: false)
|
18
|
+
RelationIndexer.new(index).batch_completed(batch_id) if batch_id
|
15
19
|
end
|
16
20
|
end
|
17
21
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# based on https://gist.github.com/mnutt/566725
|
2
|
+
module Searchkick
|
3
|
+
module ControllerRuntime
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
protected
|
7
|
+
|
8
|
+
attr_internal :searchkick_runtime
|
9
|
+
|
10
|
+
def process_action(action, *args)
|
11
|
+
# We also need to reset the runtime before each action
|
12
|
+
# because of queries in middleware or in cases we are streaming
|
13
|
+
# and it won't be cleaned up by the method below.
|
14
|
+
Searchkick::LogSubscriber.reset_runtime
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
def cleanup_view_runtime
|
19
|
+
searchkick_rt_before_render = Searchkick::LogSubscriber.reset_runtime
|
20
|
+
runtime = super
|
21
|
+
searchkick_rt_after_render = Searchkick::LogSubscriber.reset_runtime
|
22
|
+
self.searchkick_runtime = searchkick_rt_before_render + searchkick_rt_after_render
|
23
|
+
runtime - searchkick_rt_after_render
|
24
|
+
end
|
25
|
+
|
26
|
+
def append_info_to_payload(payload)
|
27
|
+
super
|
28
|
+
payload[:searchkick_runtime] = (searchkick_runtime || 0) + Searchkick::LogSubscriber.reset_runtime
|
29
|
+
end
|
30
|
+
|
31
|
+
module ClassMethods
|
32
|
+
def log_process_action(payload)
|
33
|
+
messages = super
|
34
|
+
runtime = payload[:searchkick_runtime]
|
35
|
+
messages << ("Searchkick: %.1fms" % runtime.to_f) if runtime.to_f > 0
|
36
|
+
messages
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|