typesensual 0.5.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +32 -14
- data/README.md +11 -8
- data/lib/tasks/typesensual.rake +8 -0
- data/lib/typesensual/collection.rb +26 -11
- data/lib/typesensual/index.rb +2 -3
- data/lib/typesensual/rake_helper.rb +34 -1
- data/lib/typesensual/search/facet.rb +27 -0
- data/lib/typesensual/search/results.rb +13 -2
- data/lib/typesensual/search.rb +136 -10
- data/lib/typesensual/version.rb +1 -1
- data/lib/typesensual.rb +3 -0
- metadata +5 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 29851530a5198bb6d1c6d22c50f59f21517f6a2fdb2f986a54788065310f7dd4
|
|
4
|
+
data.tar.gz: f20503d802197c17bc4577ca359018f3196f7cdf3c48523d4f2ad472c534bb78
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a7cc4b88c96e264b04f87ac36639662297c3c2befd55ba7300d5f48444ab69b067b9fb306667a8ec4ff2cbe29ef1b9c0b1c1027841abf126a51c5765ca29dc49
|
|
7
|
+
data.tar.gz: 0bd0ae98230789ade85e09b487183e1d0297dbd8eac12e44e1765e2a600646f7c41b4a87b5477347a01a3cc00dbdd51faa785510f47f63a205b848364b336ef0
|
data/CHANGELOG.md
CHANGED
|
@@ -2,4 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [1.1.0] - 2026-11-28
|
|
6
|
+
|
|
7
|
+
- Follow the alias when it moves: searches and document writes made through `Index.collection` are
|
|
8
|
+
now sent to the alias instead of to whichever collection it pointed at when the process first
|
|
9
|
+
used the index. Previously a running process stayed on the old collection after a reindex
|
|
10
|
+
elsewhere, and failed once that collection was deleted
|
|
11
|
+
- Add `Collection#lookup_name`, the name or alias a collection is reached by. `Collection#delete!`
|
|
12
|
+
still deletes the collection that was loaded, and `Collection#reload` loads whatever the alias
|
|
13
|
+
points to now
|
|
14
|
+
- Test against typesense-ruby 2.x through 5.x and Typesense 27 through 30
|
|
15
|
+
- Require `uri`, `time` and ActiveSupport's `blank` extensions explicitly instead of relying on
|
|
16
|
+
them being loaded by other gems
|
|
17
|
+
|
|
18
|
+
## [1.0.0] - 2024-10-06
|
|
19
|
+
|
|
5
20
|
- Initial release
|
data/Gemfile
CHANGED
|
@@ -5,6 +5,9 @@ source 'https://rubygems.org'
|
|
|
5
5
|
# Specify your gem's dependencies in typesensual.gemspec
|
|
6
6
|
gemspec
|
|
7
7
|
|
|
8
|
+
# Lets CI test against a specific typesense-ruby version
|
|
9
|
+
gem 'typesense', ENV.fetch('TYPESENSE_GEM') if ENV.key?('TYPESENSE_GEM')
|
|
10
|
+
|
|
8
11
|
gem 'rake'
|
|
9
12
|
gem 'rspec'
|
|
10
13
|
gem 'rubocop'
|
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
typesensual (
|
|
4
|
+
typesensual (1.1.0)
|
|
5
5
|
activesupport (>= 6.1.5)
|
|
6
6
|
paint (>= 2.0.0)
|
|
7
7
|
typesense (>= 0.13.0)
|
|
@@ -15,18 +15,33 @@ GEM
|
|
|
15
15
|
minitest (>= 5.1)
|
|
16
16
|
tzinfo (~> 2.0)
|
|
17
17
|
ast (2.4.2)
|
|
18
|
+
base64 (0.3.0)
|
|
18
19
|
commonmarker (0.23.10)
|
|
19
|
-
concurrent-ruby (1.2.
|
|
20
|
+
concurrent-ruby (1.2.3)
|
|
21
|
+
connection_pool (3.0.3)
|
|
20
22
|
diff-lcs (1.5.0)
|
|
21
23
|
docile (1.4.0)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
faraday (2.15.1)
|
|
25
|
+
faraday-net_http (>= 2.0, < 3.6)
|
|
26
|
+
json
|
|
27
|
+
logger
|
|
28
|
+
faraday-net_http (3.5.0)
|
|
29
|
+
net-http (~> 0.5)
|
|
30
|
+
faraday-net_http_persistent (2.3.1)
|
|
31
|
+
faraday (~> 2.5)
|
|
32
|
+
net-http-persistent (>= 4.0.4, < 5)
|
|
25
33
|
i18n (1.14.1)
|
|
26
34
|
concurrent-ruby (~> 1.0)
|
|
27
|
-
json (2.
|
|
28
|
-
|
|
29
|
-
|
|
35
|
+
json (2.21.2)
|
|
36
|
+
logger (1.7.0)
|
|
37
|
+
minitest (5.22.3)
|
|
38
|
+
net-http (0.10.0)
|
|
39
|
+
net-protocol (>= 0.3.0)
|
|
40
|
+
uri (>= 0.11.1)
|
|
41
|
+
net-http-persistent (4.0.8)
|
|
42
|
+
connection_pool (>= 2.2.4, < 4)
|
|
43
|
+
net-protocol (0.4.0)
|
|
44
|
+
timeout
|
|
30
45
|
paint (2.3.0)
|
|
31
46
|
parallel (1.23.0)
|
|
32
47
|
parser (3.2.2.1)
|
|
@@ -79,14 +94,17 @@ GEM
|
|
|
79
94
|
simplecov_json_formatter (~> 0.1)
|
|
80
95
|
simplecov-html (0.12.3)
|
|
81
96
|
simplecov_json_formatter (0.1.4)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
97
|
+
timeout (0.6.1)
|
|
98
|
+
typesense (5.1.0)
|
|
99
|
+
base64 (~> 0.2)
|
|
100
|
+
faraday (~> 2.8)
|
|
101
|
+
faraday-net_http_persistent (~> 2.0)
|
|
102
|
+
json (>= 2.9, < 4)
|
|
103
|
+
net-http-persistent (~> 4.0)
|
|
87
104
|
tzinfo (2.0.6)
|
|
88
105
|
concurrent-ruby (~> 1.0)
|
|
89
106
|
unicode-display_width (2.4.2)
|
|
107
|
+
uri (1.1.2)
|
|
90
108
|
webrick (1.7.0)
|
|
91
109
|
yard (0.9.34)
|
|
92
110
|
|
|
@@ -110,4 +128,4 @@ DEPENDENCIES
|
|
|
110
128
|
yard
|
|
111
129
|
|
|
112
130
|
BUNDLED WITH
|
|
113
|
-
2.
|
|
131
|
+
2.6.7
|
data/README.md
CHANGED
|
@@ -117,13 +117,8 @@ Once you have defined your index, you can load data into it and update the alias
|
|
|
117
117
|
indexed data. Typesensual provides rake tasks for this purpose if you use ActiveRecord:
|
|
118
118
|
|
|
119
119
|
```console
|
|
120
|
-
$ bundle exec rake typesensual:
|
|
121
|
-
==>
|
|
122
|
-
|
|
123
|
-
$ bundle exec rake typesensual:update_alias[MoviesIndex,1690076097]
|
|
124
|
-
==> Alias for MoviesIndex
|
|
125
|
-
Old: None (N/A)
|
|
126
|
-
New: 1690076097 (2023-05-07 18:01:37)
|
|
120
|
+
$ bundle exec rake typesensual:reindex[MoviesIndex,Movie]
|
|
121
|
+
==> Reindexing Movie into MoviesIndex (Version 1690076097)
|
|
127
122
|
```
|
|
128
123
|
|
|
129
124
|
Otherwise you can do similar to the following:
|
|
@@ -134,6 +129,9 @@ collection.index_many(Movie.ids, collection: collection)
|
|
|
134
129
|
MoviesIndex.update_alias(collection)
|
|
135
130
|
```
|
|
136
131
|
|
|
132
|
+
Searches and writes made through the index go by way of its alias, so processes which are already
|
|
133
|
+
running pick up the new collection as soon as the alias moves. There's no need to restart them.
|
|
134
|
+
|
|
137
135
|
### Searching your index
|
|
138
136
|
|
|
139
137
|
Now that you have data in your index, you can search it! Typesensual provides a simple interface for
|
|
@@ -146,11 +144,16 @@ query.per(10).page(2).load
|
|
|
146
144
|
|
|
147
145
|
The full interface for this is documented in the `Search`, `Results`, and `Hit` classes.
|
|
148
146
|
|
|
147
|
+
## Compatibility
|
|
148
|
+
|
|
149
|
+
Typesensual is tested against typesense-ruby 2.x through 5.x and Typesense 27 through 30, paired
|
|
150
|
+
according to [typesense-ruby's compatibility table](https://github.com/typesense/typesense-ruby#compatibility).
|
|
151
|
+
|
|
149
152
|
## Development
|
|
150
153
|
|
|
151
154
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
152
155
|
|
|
153
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb
|
|
156
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb` and the changelog, then push a `v`-prefixed tag for it (or run the Release workflow by hand, which creates the tag). The [Release workflow](.github/workflows/release.yml) pushes the `.gem` file to [rubygems.org](https://rubygems.org) through [trusted publishing](https://guides.rubygems.org/trusted-publishing/).
|
|
154
157
|
|
|
155
158
|
## Contributing
|
|
156
159
|
|
data/lib/tasks/typesensual.rake
CHANGED
|
@@ -24,6 +24,14 @@ namespace :typesensual do
|
|
|
24
24
|
)
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
+
desc 'Index all records from a model into a new version then update the alias of the index'
|
|
28
|
+
task :reindex, %i[index model] => :environment do |_, args|
|
|
29
|
+
Typesensual::RakeHelper.reindex(
|
|
30
|
+
index: args[:index],
|
|
31
|
+
model: args[:model]
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
|
|
27
35
|
desc 'Delete a version of an index'
|
|
28
36
|
task :drop_version, %i[index version] => :environment do |_, args|
|
|
29
37
|
Typesensual::RakeHelper.drop_version(
|
|
@@ -32,26 +32,40 @@ class Typesensual
|
|
|
32
32
|
# * `num_documents` [Integer] the number of documents in the collection
|
|
33
33
|
# * `symbols_to_index` [String] the symbols to index
|
|
34
34
|
# * `token_separators` [String] the token separators
|
|
35
|
+
#
|
|
35
36
|
# @overload initialize(name)
|
|
36
37
|
# Initialize a new collection, loading info from Typesense
|
|
37
38
|
#
|
|
38
|
-
# @param name [String] the name of the collection
|
|
39
|
+
# @param name [String] the name of the collection, or of an alias pointing to it
|
|
39
40
|
# @raise [Typesense::Error::ObjectNotFound] if the collection doesn't exist
|
|
40
41
|
def initialize(collection_or_name)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
if collection_or_name.is_a?(Hash)
|
|
43
|
+
@collection = collection_or_name.deep_stringify_keys
|
|
44
|
+
else
|
|
45
|
+
@lookup_name = collection_or_name
|
|
46
|
+
@collection = client.collections[collection_or_name].retrieve
|
|
47
|
+
end
|
|
46
48
|
end
|
|
47
49
|
|
|
48
|
-
# Reload the underlying collection data from Typesense
|
|
50
|
+
# Reload the underlying collection data from Typesense. If the collection was looked up through
|
|
51
|
+
# an alias which has since been pointed elsewhere, this loads the collection it points to now.
|
|
49
52
|
# @return [self]
|
|
50
53
|
def reload
|
|
51
|
-
@collection = client.collections[
|
|
54
|
+
@collection = client.collections[lookup_name].retrieve
|
|
55
|
+
@created_at = @parsed_name = nil
|
|
52
56
|
self
|
|
53
57
|
end
|
|
54
58
|
|
|
59
|
+
# The name this collection is reached by: the name or alias it was looked up with, or its own
|
|
60
|
+
# name if it was not looked up. Searches and document writes are sent here rather than to
|
|
61
|
+
# {#name}, so a collection which was looked up through an alias follows that alias when it is
|
|
62
|
+
# pointed at another collection, such as by a reindex in another process. The rest of what the
|
|
63
|
+
# collection reports about itself is as of when it was loaded; see {#reload}.
|
|
64
|
+
# @return [String] the name or alias used to reach the collection
|
|
65
|
+
def lookup_name
|
|
66
|
+
@lookup_name || name
|
|
67
|
+
end
|
|
68
|
+
|
|
55
69
|
# The time the collection was created, as tracked by Typesense
|
|
56
70
|
# @return [Time] the time the collection was created
|
|
57
71
|
def created_at
|
|
@@ -132,10 +146,11 @@ class Typesensual
|
|
|
132
146
|
self
|
|
133
147
|
end
|
|
134
148
|
|
|
135
|
-
# Deletes the collection in Typesense
|
|
149
|
+
# Deletes the collection in Typesense. This is always the collection that was loaded, even if
|
|
150
|
+
# it was looked up through an alias which now points to another one.
|
|
136
151
|
# @return [void]
|
|
137
152
|
def delete!
|
|
138
|
-
|
|
153
|
+
client.collections[name].delete
|
|
139
154
|
end
|
|
140
155
|
|
|
141
156
|
# Create a new collection using the given collection hash
|
|
@@ -203,7 +218,7 @@ class Typesensual
|
|
|
203
218
|
end
|
|
204
219
|
|
|
205
220
|
def typesense_collection
|
|
206
|
-
@typesense_collection ||= client.collections[
|
|
221
|
+
@typesense_collection ||= client.collections[lookup_name]
|
|
207
222
|
end
|
|
208
223
|
|
|
209
224
|
private
|
data/lib/typesensual/index.rb
CHANGED
|
@@ -91,13 +91,12 @@ class Typesensual
|
|
|
91
91
|
end
|
|
92
92
|
end
|
|
93
93
|
|
|
94
|
-
# Get the collection that the alias points to
|
|
94
|
+
# Get the collection that the alias points to. Searches and document writes made through it
|
|
95
|
+
# go by way of the alias, so they follow it if it is later pointed at another collection.
|
|
95
96
|
#
|
|
96
97
|
# @return [Collection] the collection that the alias points to
|
|
97
98
|
def self.collection
|
|
98
99
|
@collection ||= Collection.new(alias_name)
|
|
99
|
-
rescue Typesense::Error::ObjectNotFound
|
|
100
|
-
nil
|
|
101
100
|
end
|
|
102
101
|
|
|
103
102
|
# Define the schema for the collection
|
|
@@ -68,6 +68,35 @@ class Typesensual
|
|
|
68
68
|
end
|
|
69
69
|
end
|
|
70
70
|
|
|
71
|
+
# Index all records from a model into a new collection, then update the alias to point to it.
|
|
72
|
+
#
|
|
73
|
+
# @param index [String] The name of the index to index into
|
|
74
|
+
# @param model [String] The name of the model to index from
|
|
75
|
+
# @example
|
|
76
|
+
# take typesensual:reindex[FooIndex,Foo]
|
|
77
|
+
def reindex(index:, model:, output: $stdout)
|
|
78
|
+
index = index.safe_constantize
|
|
79
|
+
model = model.safe_constantize
|
|
80
|
+
|
|
81
|
+
collection = index.create!
|
|
82
|
+
output.printf(
|
|
83
|
+
Paint["==> Reindexing %<model>s into %<index>s (Version %<version>s)\n", :bold],
|
|
84
|
+
model: model.name,
|
|
85
|
+
index: index.name,
|
|
86
|
+
version: collection.version
|
|
87
|
+
)
|
|
88
|
+
failures = index.index_many(
|
|
89
|
+
model.ids,
|
|
90
|
+
collection: collection
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
index.update_alias!(collection)
|
|
94
|
+
|
|
95
|
+
failures.each do |failure|
|
|
96
|
+
output.puts(failure.to_json)
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
71
100
|
# Update the alias for an index to point to a specific version
|
|
72
101
|
#
|
|
73
102
|
# @param index [String] The name of the index to update
|
|
@@ -76,7 +105,11 @@ class Typesensual
|
|
|
76
105
|
# rake typesensual:update_alias[FooIndex,1]
|
|
77
106
|
def update_alias(index:, version:, output: $stdout)
|
|
78
107
|
index = index.safe_constantize
|
|
79
|
-
old_coll =
|
|
108
|
+
old_coll = begin
|
|
109
|
+
index.collection
|
|
110
|
+
rescue Typesense::Error::ObjectNotFound
|
|
111
|
+
nil
|
|
112
|
+
end
|
|
80
113
|
new_coll = index.collection_for(version: version)
|
|
81
114
|
|
|
82
115
|
unless new_coll
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Typesensual
|
|
4
|
+
class Search
|
|
5
|
+
# Represents a facet returned with search results
|
|
6
|
+
class Facet
|
|
7
|
+
attr_reader :key
|
|
8
|
+
|
|
9
|
+
def initialize(key, facet)
|
|
10
|
+
@key = key
|
|
11
|
+
@facet = facet
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def count
|
|
15
|
+
@facet['count']
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def value
|
|
19
|
+
@facet['value']
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def highlighted
|
|
23
|
+
@facet['highlighted']
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
class Typesensual
|
|
4
4
|
class Search
|
|
5
5
|
class Results
|
|
6
|
-
def initialize(results)
|
|
6
|
+
def initialize(results, search:)
|
|
7
7
|
@results = results
|
|
8
|
+
@search = search
|
|
8
9
|
end
|
|
9
10
|
|
|
10
11
|
def hits
|
|
@@ -43,12 +44,22 @@ class Typesensual
|
|
|
43
44
|
current_page + 1 unless last_page?
|
|
44
45
|
end
|
|
45
46
|
|
|
47
|
+
def per_page
|
|
48
|
+
@results['request_params']['per_page'].to_i
|
|
49
|
+
end
|
|
50
|
+
|
|
46
51
|
def search_time_ms
|
|
47
52
|
@results['search_time_ms']
|
|
48
53
|
end
|
|
49
54
|
|
|
50
55
|
def total_pages
|
|
51
|
-
(@results['found'] /
|
|
56
|
+
(@results['found'] / per_page.to_f).ceil
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def facets
|
|
60
|
+
@search.facet_keys.zip(@results['facet_counts']).to_h do |(key, facet)|
|
|
61
|
+
[key, Facet.new(key, facet)]
|
|
62
|
+
end
|
|
52
63
|
end
|
|
53
64
|
end
|
|
54
65
|
end
|
data/lib/typesensual/search.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'typesensual/search/facet'
|
|
3
4
|
require 'typesensual/search/hit'
|
|
4
5
|
require 'typesensual/search/grouped_hit'
|
|
5
6
|
require 'typesensual/search/results'
|
|
@@ -8,6 +9,11 @@ class Typesensual
|
|
|
8
9
|
class Search
|
|
9
10
|
include StateHelpers
|
|
10
11
|
|
|
12
|
+
# The keys of all the facets (used for re-keying the facets in the results)
|
|
13
|
+
# @return [Array<String>]
|
|
14
|
+
# @!visibility private
|
|
15
|
+
attr_reader :facet_keys
|
|
16
|
+
|
|
11
17
|
# Initialize a new search object for a collection
|
|
12
18
|
#
|
|
13
19
|
# @param collection [Typesensual::Collection] the Typesensual collection object
|
|
@@ -21,6 +27,9 @@ class Typesensual
|
|
|
21
27
|
@sort_by = []
|
|
22
28
|
@facet_by = []
|
|
23
29
|
@facet_query = []
|
|
30
|
+
# this is just used by the Results class to determine names for facets
|
|
31
|
+
@facet_keys = []
|
|
32
|
+
@facet_return_parent = []
|
|
24
33
|
@include_fields = []
|
|
25
34
|
@exclude_fields = []
|
|
26
35
|
@group_by = []
|
|
@@ -41,12 +50,14 @@ class Typesensual
|
|
|
41
50
|
|
|
42
51
|
# Set the number of results to return per page
|
|
43
52
|
# @param count [Integer] the number of results to return per page
|
|
53
|
+
# @return [self]
|
|
44
54
|
def per(count)
|
|
45
55
|
set(per_page: count)
|
|
46
56
|
end
|
|
47
57
|
|
|
48
58
|
# Set the page number to return
|
|
49
59
|
# @param number [Integer] the page number to return
|
|
60
|
+
# @return [self]
|
|
50
61
|
def page(number)
|
|
51
62
|
set(page: number)
|
|
52
63
|
end
|
|
@@ -55,6 +66,7 @@ class Typesensual
|
|
|
55
66
|
# @param filter [String, Symbol, Hash<String, Symbol>] the filter to add. If a hash is
|
|
56
67
|
# provided, the keys are the fields and the values are the values to filter by. If a
|
|
57
68
|
# string is provided, it is added directly as a filter. All filters are ANDed together.
|
|
69
|
+
# @return [self]
|
|
58
70
|
def filter(filter)
|
|
59
71
|
if filter.is_a?(Hash)
|
|
60
72
|
@filter_by += filter.map { |key, value| "#{key}:#{value}" }
|
|
@@ -70,6 +82,7 @@ class Typesensual
|
|
|
70
82
|
# @param value [String, Symbol, Hash<String, Symbol>] the sort to add to the search. If
|
|
71
83
|
# a hash is provided, the keys are the fields and the values are the directions to sort.
|
|
72
84
|
# If a string is provided, it is added directly as a sort.
|
|
85
|
+
# @return [self]
|
|
73
86
|
def sort(value)
|
|
74
87
|
if value.is_a?(Hash)
|
|
75
88
|
@sort_by += value.map { |key, direction| "#{key}:#{direction}" }
|
|
@@ -79,20 +92,108 @@ class Typesensual
|
|
|
79
92
|
self
|
|
80
93
|
end
|
|
81
94
|
|
|
82
|
-
# Add a field to facet to the
|
|
83
|
-
# @
|
|
84
|
-
#
|
|
85
|
-
#
|
|
86
|
-
#
|
|
95
|
+
# Add a field to facet to the search
|
|
96
|
+
# @return [self]
|
|
97
|
+
# @overload facet(facets: Symbol, String, Array)
|
|
98
|
+
# Basic faceting with just a list of fields
|
|
99
|
+
# @param facets [String, Symbol, Array<String, Symbol>] the fields to facet on
|
|
100
|
+
# @example Facet by type
|
|
101
|
+
# # Generates `facet_by=type`
|
|
102
|
+
# .facet(:type)
|
|
103
|
+
# @example Facet by type and year
|
|
104
|
+
# # Generates `facet_by=type,year`
|
|
105
|
+
# .facet(['type', 'year'])
|
|
106
|
+
#
|
|
107
|
+
# @overload facet(facets: Hash)
|
|
108
|
+
# Advanced faceting with sort, ranges, return_parent, and query
|
|
109
|
+
# @param facets [Hash{String, Symbol => String}, Hash{String, Symbol => Hash}] the fields to
|
|
110
|
+
# facet by and their configuration. If a string is passed as the value, it is used to query
|
|
111
|
+
# the facet. If a hash is passed, each value is used to configure the facet using the
|
|
112
|
+
# following options:
|
|
113
|
+
#
|
|
114
|
+
# * **`facets[key][:sort]`** (`String, Symbol, Hash{String, Symbol => Symbol}`) — the
|
|
115
|
+
# field to sort by, and the direction to sort it in. If you pass a string or symbol, it is
|
|
116
|
+
# used as the sort direction for alphabetical sorting. Typesense only supports sorting by
|
|
117
|
+
# a single field, and passing more than one field will raise an ArgumentError.
|
|
118
|
+
# * **`facets[key][:ranges]`** (`Hash{String, Symbol => Range, Array}`) — the ranges to
|
|
119
|
+
# facet by, for numerical facets. For each key, you can pass a Range or an Array with two
|
|
120
|
+
# elements. Ranges MUST be end-exclusive and Arrays MUST have two elements, or else an
|
|
121
|
+
# ArgumentError will be raised.
|
|
122
|
+
# * **`facets[key][:return_parent]`** (`Boolean`) — if true, the parent object of the field
|
|
123
|
+
# will be returned, which can be useful for nested fields.
|
|
124
|
+
# * **`facets[key][:query]`** (`String`) — the query to facet by, equivalent to passing a
|
|
125
|
+
# string instead of a hash.
|
|
126
|
+
# @example Facet by category, sorted by category size, and return the parent
|
|
127
|
+
# # Generates `facet_by=categories.id(categories.size:desc)&facet_return_parent=categories.id`
|
|
128
|
+
# .facet('categories.id' => { sort: { 'categories.size' => :desc }, return_parent: true })
|
|
129
|
+
# @example Facet by decade
|
|
130
|
+
# # Generates `facet_by=year(1990s:[1990,2000],2000s:[2000,2010])`
|
|
131
|
+
# .facet('year' => { ranges: { '1990s' => 1990...2000, '2000s' => [2000, 2010] })
|
|
87
132
|
def facet(facets)
|
|
88
133
|
if facets.is_a?(Hash)
|
|
89
134
|
facets.each do |key, value|
|
|
90
|
-
@
|
|
91
|
-
|
|
135
|
+
@facet_keys << key.to_s
|
|
136
|
+
if value.is_a?(String)
|
|
137
|
+
# Basic facet searching with a string query
|
|
138
|
+
@facet_by << key.to_s
|
|
139
|
+
@facet_query << "#{key}:#{value}" if value
|
|
140
|
+
elsif value.is_a?(Hash)
|
|
141
|
+
# Advanced faceting with sort, ranges, return_parent, and query
|
|
142
|
+
facet_string = key.to_s
|
|
143
|
+
facet_params = {}
|
|
144
|
+
|
|
145
|
+
# Sort parameters
|
|
146
|
+
case value[:sort]
|
|
147
|
+
when Hash
|
|
148
|
+
raise ArgumentError, 'Facet sort_by must have one key' if value[:sort].count != 1
|
|
149
|
+
sort_key, sort_direction = value[:sort].first
|
|
150
|
+
facet_params[:sort_by] = "#{sort_key}:#{sort_direction}"
|
|
151
|
+
when Symbol
|
|
152
|
+
facet_params[:sort_by] = "_alpha:#{value[:sort]}"
|
|
153
|
+
when String
|
|
154
|
+
facet_params[:sort_by] = value[:sort]
|
|
155
|
+
when nil
|
|
156
|
+
nil
|
|
157
|
+
else
|
|
158
|
+
raise ArgumentError, 'Facet sort_by must be a Hash, Symbol, or String'
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Range parameters
|
|
162
|
+
if value[:ranges].is_a?(Hash)
|
|
163
|
+
ranges = value[:ranges].transform_values do |range|
|
|
164
|
+
case range
|
|
165
|
+
when Range
|
|
166
|
+
raise ArgumentError, 'Facet ranges must exclude end' unless range.exclude_end?
|
|
167
|
+
"[#{range.begin},#{range.end}]"
|
|
168
|
+
when Array
|
|
169
|
+
raise ArgumentError, 'Facet ranges must have two elements' unless range.count == 2
|
|
170
|
+
"[#{range.first},#{range.last}]"
|
|
171
|
+
else
|
|
172
|
+
raise ArgumentError, 'Facet ranges must be a Range or Array'
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
facet_params.merge!(ranges)
|
|
176
|
+
elsif !value[:ranges].nil?
|
|
177
|
+
raise ArgumentError, 'Facet ranges must be a Hash'
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Format facet params
|
|
181
|
+
unless facet_params.empty?
|
|
182
|
+
facet_string += "(#{facet_params.map { |k, v| "#{k}:#{v}" }.join(',')})"
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
@facet_return_parent << key.to_s if value[:return_parent]
|
|
186
|
+
@facet_query << "#{key}:#{value[:query]}" if value[:query]
|
|
187
|
+
@facet_by << facet_string
|
|
188
|
+
else
|
|
189
|
+
@facet_by << key.to_s
|
|
190
|
+
end
|
|
92
191
|
end
|
|
93
192
|
elsif facets.is_a?(Array)
|
|
193
|
+
@facet_keys += facets.map(&:to_s)
|
|
94
194
|
@facet_by += facets.map(&:to_s)
|
|
95
195
|
else
|
|
196
|
+
@facet_keys << facets.to_s
|
|
96
197
|
@facet_by << facets.to_s
|
|
97
198
|
end
|
|
98
199
|
self
|
|
@@ -100,6 +201,7 @@ class Typesensual
|
|
|
100
201
|
|
|
101
202
|
# Add fields to include in the search result documents
|
|
102
203
|
# @param fields [String, Symbol, Array<String, Symbol>] the fields to include
|
|
204
|
+
# @return [self]
|
|
103
205
|
def include_fields(*fields)
|
|
104
206
|
@include_fields += fields.map(&:to_s)
|
|
105
207
|
self
|
|
@@ -107,11 +209,15 @@ class Typesensual
|
|
|
107
209
|
|
|
108
210
|
# Add fields to exclude from the search result documents
|
|
109
211
|
# @param fields [String, Symbol, Array<String, Symbol>] the fields to exclude
|
|
212
|
+
# @return [self]
|
|
110
213
|
def exclude_fields(*fields)
|
|
111
214
|
@exclude_fields += fields.map(&:to_s)
|
|
112
215
|
self
|
|
113
216
|
end
|
|
114
217
|
|
|
218
|
+
# Add fields to group the search results by
|
|
219
|
+
# @param fields [String, Symbol, Array<String, Symbol>] the fields to group by
|
|
220
|
+
# @return [self]
|
|
115
221
|
def group_by(*fields)
|
|
116
222
|
@group_by += fields.map(&:to_s)
|
|
117
223
|
self
|
|
@@ -119,6 +225,7 @@ class Typesensual
|
|
|
119
225
|
|
|
120
226
|
# Set additional parameters to pass to the search
|
|
121
227
|
# @param values [Hash] the parameters to set
|
|
228
|
+
# @return [self]
|
|
122
229
|
def set(values)
|
|
123
230
|
@params.merge!(values)
|
|
124
231
|
self
|
|
@@ -128,13 +235,14 @@ class Typesensual
|
|
|
128
235
|
# @return [Hash] the query document
|
|
129
236
|
def query
|
|
130
237
|
{
|
|
131
|
-
collection: @collection.
|
|
238
|
+
collection: @collection.lookup_name,
|
|
132
239
|
filter_by: @filter_by.join(' && '),
|
|
133
240
|
q: @query,
|
|
134
241
|
query_by: @query_by&.join(','),
|
|
135
242
|
query_by_weights: @query_by_weights&.join(','),
|
|
136
243
|
sort_by: @sort_by&.join(','),
|
|
137
244
|
facet_by: @facet_by&.join(','),
|
|
245
|
+
facet_return_parent: @facet_return_parent&.join(','),
|
|
138
246
|
facet_query: @facet_query&.join(','),
|
|
139
247
|
include_fields: @include_fields&.join(','),
|
|
140
248
|
exclude_fields: @exclude_fields&.join(','),
|
|
@@ -145,7 +253,9 @@ class Typesensual
|
|
|
145
253
|
# Load the results from the search query
|
|
146
254
|
# @return [Typesensual::Search::Results] the results of the search
|
|
147
255
|
def load
|
|
148
|
-
|
|
256
|
+
result = self.class.multi(self).first
|
|
257
|
+
raise result if result.is_a?(StandardError)
|
|
258
|
+
result
|
|
149
259
|
end
|
|
150
260
|
|
|
151
261
|
# Perform multiple searches in one request. There are two variants of this method, one which
|
|
@@ -181,7 +291,9 @@ class Typesensual
|
|
|
181
291
|
|
|
182
292
|
# Wrap our results in Result objects
|
|
183
293
|
wrapped_results = results['results'].map do |result|
|
|
184
|
-
|
|
294
|
+
exception = exception_for(result) if result['code']
|
|
295
|
+
|
|
296
|
+
exception || Results.new(result, search: self)
|
|
185
297
|
end
|
|
186
298
|
|
|
187
299
|
# If we're doing named searches, re-key the results
|
|
@@ -191,5 +303,19 @@ class Typesensual
|
|
|
191
303
|
wrapped_results
|
|
192
304
|
end
|
|
193
305
|
end
|
|
306
|
+
|
|
307
|
+
# Roughly duplicates the logic from Typesense::ApiCall#custom_exception_klass_for
|
|
308
|
+
private_class_method def self.exception_for(result)
|
|
309
|
+
case result['code']
|
|
310
|
+
when 400 then Typesense::Error::RequestMalformed.new(result['message'])
|
|
311
|
+
when 401 then Typesense::Error::RequestUnauthorized.new(result['message'])
|
|
312
|
+
when 404 then Typesense::Error::ObjectNotFound.new(result['message'])
|
|
313
|
+
when 409 then Typesense::Error::ObjectAlreadyExists.new(result['message'])
|
|
314
|
+
when 422 then Typesense::Error::ObjectUnprocessable.new(result['message'])
|
|
315
|
+
when 500..599 then Typesense::Error::ServerError.new(result['message'])
|
|
316
|
+
when 100..399, nil then nil
|
|
317
|
+
else Typesense::Error.new(result['message'])
|
|
318
|
+
end
|
|
319
|
+
end
|
|
194
320
|
end
|
|
195
321
|
end
|
data/lib/typesensual/version.rb
CHANGED
data/lib/typesensual.rb
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'json'
|
|
4
|
+
require 'time'
|
|
5
|
+
require 'uri'
|
|
4
6
|
|
|
5
7
|
require 'active_support/concern'
|
|
8
|
+
require 'active_support/core_ext/object/blank'
|
|
6
9
|
require 'active_support/core_ext/string/inflections'
|
|
7
10
|
require 'active_support/core_ext/class/subclasses'
|
|
8
11
|
require 'active_support/core_ext/module/delegation'
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: typesensual
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Emma Lejeck
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: activesupport
|
|
@@ -52,9 +51,8 @@ dependencies:
|
|
|
52
51
|
- - ">="
|
|
53
52
|
- !ruby/object:Gem::Version
|
|
54
53
|
version: 0.13.0
|
|
55
|
-
description:
|
|
56
54
|
email:
|
|
57
|
-
- nuck@kitsu.
|
|
55
|
+
- nuck@kitsu.app
|
|
58
56
|
executables: []
|
|
59
57
|
extensions: []
|
|
60
58
|
extra_rdoc_files: []
|
|
@@ -80,6 +78,7 @@ files:
|
|
|
80
78
|
- lib/typesensual/rake_helper.rb
|
|
81
79
|
- lib/typesensual/schema.rb
|
|
82
80
|
- lib/typesensual/search.rb
|
|
81
|
+
- lib/typesensual/search/facet.rb
|
|
83
82
|
- lib/typesensual/search/grouped_hit.rb
|
|
84
83
|
- lib/typesensual/search/hit.rb
|
|
85
84
|
- lib/typesensual/search/results.rb
|
|
@@ -93,7 +92,6 @@ metadata:
|
|
|
93
92
|
source_code_uri: https://github.com/hummingbird-me/typesensual
|
|
94
93
|
changelog_uri: https://github.com/hummingbird-me/typesensual/blob/main/CHANGELOG.md
|
|
95
94
|
rubygems_mfa_required: 'true'
|
|
96
|
-
post_install_message:
|
|
97
95
|
rdoc_options: []
|
|
98
96
|
require_paths:
|
|
99
97
|
- lib
|
|
@@ -108,8 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
108
106
|
- !ruby/object:Gem::Version
|
|
109
107
|
version: '0'
|
|
110
108
|
requirements: []
|
|
111
|
-
rubygems_version: 3.
|
|
112
|
-
signing_key:
|
|
109
|
+
rubygems_version: 3.6.9
|
|
113
110
|
specification_version: 4
|
|
114
111
|
summary: A simple, sensual wrapper around Typesense for Ruby
|
|
115
112
|
test_files: []
|