redi_search 6.3.0 → 7.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/lint.yml +3 -3
- data/.github/workflows/tests.yml +6 -6
- data/.rubocop.yml +1 -1
- data/.ruby-version +1 -1
- data/Appraisals +5 -5
- data/Gemfile +4 -1
- data/gemfiles/{activerecord_60.gemfile → activerecord_71.gemfile} +1 -1
- data/lib/redi_search/aggregate/clauses/apply.rb +24 -0
- data/lib/redi_search/aggregate/clauses/filter.rb +22 -0
- data/lib/redi_search/aggregate/clauses/group_by.rb +79 -0
- data/lib/redi_search/aggregate/clauses/limit.rb +28 -0
- data/lib/redi_search/aggregate/clauses/load.rb +22 -0
- data/lib/redi_search/aggregate/clauses/sort_by.rb +43 -0
- data/lib/redi_search/aggregate/clauses/verbatim.rb +17 -0
- data/lib/redi_search/aggregate/reducers/average.rb +26 -0
- data/lib/redi_search/aggregate/reducers/count.rb +23 -0
- data/lib/redi_search/aggregate/reducers/distinct_count.rb +26 -0
- data/lib/redi_search/aggregate/reducers/distinctish_count.rb +26 -0
- data/lib/redi_search/aggregate/reducers/max.rb +26 -0
- data/lib/redi_search/aggregate/reducers/min.rb +26 -0
- data/lib/redi_search/aggregate/reducers/quantile.rb +30 -0
- data/lib/redi_search/aggregate/reducers/stdev.rb +26 -0
- data/lib/redi_search/aggregate/reducers/sum.rb +26 -0
- data/lib/redi_search/aggregate/reducers/to_list.rb +26 -0
- data/lib/redi_search/aggregate.rb +104 -0
- data/lib/redi_search/application_clause.rb +37 -0
- data/lib/redi_search/client.rb +5 -4
- data/lib/redi_search/index.rb +6 -0
- data/lib/redi_search/log_subscriber.rb +8 -3
- data/lib/redi_search/model.rb +5 -1
- data/lib/redi_search/schema/tag_field.rb +8 -0
- data/lib/redi_search/schema.rb +10 -10
- data/lib/redi_search/search/clauses/highlight.rb +3 -1
- data/lib/redi_search/search/clauses/in_order.rb +2 -0
- data/lib/redi_search/search/clauses/language.rb +1 -0
- data/lib/redi_search/search/clauses/limit.rb +1 -0
- data/lib/redi_search/search/clauses/no_content.rb +2 -0
- data/lib/redi_search/search/clauses/no_stop_words.rb +2 -0
- data/lib/redi_search/search/clauses/return.rb +1 -0
- data/lib/redi_search/search/clauses/slop.rb +1 -0
- data/lib/redi_search/search/clauses/sort_by.rb +1 -0
- data/lib/redi_search/search/clauses/timeout.rb +24 -0
- data/lib/redi_search/search/clauses/verbatim.rb +2 -0
- data/lib/redi_search/search/clauses/with_payloads.rb +17 -0
- data/lib/redi_search/search/clauses/with_scores.rb +2 -0
- data/lib/redi_search/search/clauses/with_sort_keys.rb +17 -0
- data/lib/redi_search/search/clauses.rb +36 -55
- data/lib/redi_search/search/{clauses → queries}/and.rb +1 -1
- data/lib/redi_search/search/{clauses → queries}/boolean.rb +4 -4
- data/lib/redi_search/search/{clauses → queries}/or.rb +1 -1
- data/lib/redi_search/search/{clauses → queries}/where.rb +2 -2
- data/lib/redi_search/search/queries.rb +39 -0
- data/lib/redi_search/search.rb +7 -6
- data/lib/redi_search/version.rb +1 -1
- data/redi_search.gemspec +2 -6
- metadata +34 -60
- data/lib/redi_search/search/clauses/application_clause.rb +0 -31
@@ -0,0 +1,104 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RediSearch
|
4
|
+
class Aggregate
|
5
|
+
class MissingGroupByClause < StandardError
|
6
|
+
end
|
7
|
+
|
8
|
+
extend Forwardable
|
9
|
+
include LazilyLoad
|
10
|
+
include Search::Queries
|
11
|
+
|
12
|
+
attr_reader :query, :used_clauses, :index, :clauses
|
13
|
+
|
14
|
+
def_delegator :index, :model
|
15
|
+
|
16
|
+
def initialize(index, term = nil, **term_options)
|
17
|
+
@index = index
|
18
|
+
@clauses = []
|
19
|
+
@used_clauses = Set.new
|
20
|
+
|
21
|
+
@query = term && Search::Queries::And.new(self, term, nil, **term_options)
|
22
|
+
end
|
23
|
+
|
24
|
+
def verbatim
|
25
|
+
add_to_clauses(Clauses::Verbatim.new)
|
26
|
+
end
|
27
|
+
|
28
|
+
def load(*fields)
|
29
|
+
add_to_clauses(Clauses::Load.new(fields: fields))
|
30
|
+
end
|
31
|
+
|
32
|
+
def group_by(*fields)
|
33
|
+
add_to_clauses(Clauses::GroupBy.new(fields: fields))
|
34
|
+
end
|
35
|
+
|
36
|
+
def count(as: nil)
|
37
|
+
clause = clauses.reverse.find { |cl| cl.is_a?(Clauses::GroupBy) } ||
|
38
|
+
raise(MissingGroupByClause, "call group_by first")
|
39
|
+
|
40
|
+
clause.count(as: as)
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def quantile(property: nil, quantile: nil, as: nil)
|
45
|
+
clause = clauses.reverse.find { |cl| cl.is_a?(Clauses::GroupBy) } ||
|
46
|
+
raise(MissingGroupByClause, "call group_by first")
|
47
|
+
|
48
|
+
clause.count(property: property, quantile: quantile, as: as)
|
49
|
+
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
%i(distinct_count distinctish_count sum min max average stdev to_list).
|
54
|
+
each do |reducer|
|
55
|
+
define_method(reducer) do |property, as: nil|
|
56
|
+
clause = clauses.reverse.find { |cl| cl.is_a?(Clauses::GroupBy) } ||
|
57
|
+
raise(MissingGroupByClause, "call group_by first")
|
58
|
+
|
59
|
+
clause.public_send(reducer, property: property, as: as)
|
60
|
+
self
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def sort_by(*fields)
|
65
|
+
add_to_clauses(Clauses::SortBy.new(fields: fields))
|
66
|
+
end
|
67
|
+
|
68
|
+
def apply(expression, as:)
|
69
|
+
add_to_clauses(Clauses::Apply.new(expression: expression, as: as))
|
70
|
+
end
|
71
|
+
|
72
|
+
def filter(expression)
|
73
|
+
add_to_clauses(Clauses::Filter.new(expression: expression))
|
74
|
+
end
|
75
|
+
|
76
|
+
def limit(total, offset = 0)
|
77
|
+
add_to_clauses(Clauses::Limit.new(total: total, offset: offset))
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
attr_writer :index, :clauses
|
83
|
+
|
84
|
+
def command
|
85
|
+
["AGGREGATE", index.name, query.to_s,
|
86
|
+
*clauses.sort_by(&:clause_order).flat_map(&:clause)]
|
87
|
+
end
|
88
|
+
|
89
|
+
def parse_response(response)
|
90
|
+
@documents = response
|
91
|
+
end
|
92
|
+
|
93
|
+
def valid?
|
94
|
+
!query.to_s.empty?
|
95
|
+
end
|
96
|
+
|
97
|
+
def add_to_clauses(clause)
|
98
|
+
clause.validate! && clauses.push(clause) if
|
99
|
+
used_clauses.add?(clause.class)
|
100
|
+
|
101
|
+
self
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RediSearch
|
4
|
+
class ApplicationClause
|
5
|
+
include Validatable
|
6
|
+
|
7
|
+
def clause_order
|
8
|
+
self.class.order
|
9
|
+
end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def clause_term(term, **validations)
|
13
|
+
attr_reader term
|
14
|
+
|
15
|
+
validations.each do |validation_type, options|
|
16
|
+
define_validation(term, validation_type, options)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader :order
|
21
|
+
|
22
|
+
def clause_order(number)
|
23
|
+
@order = number
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def define_validation(term, type, options)
|
29
|
+
if options.is_a? Hash
|
30
|
+
public_send("validates_#{type}_of", term, **options)
|
31
|
+
else
|
32
|
+
public_send("validates_#{type}_of", term)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/redi_search/client.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "redis"
|
4
3
|
require "active_support/notifications"
|
5
4
|
|
6
5
|
module RediSearch
|
@@ -18,11 +17,12 @@ module RediSearch
|
|
18
17
|
end
|
19
18
|
|
20
19
|
def multi
|
21
|
-
|
22
|
-
|
20
|
+
instrument("pipeline", query: ["begin pipeline"])
|
21
|
+
Response.new(redis.pipelined do |pipeline|
|
23
22
|
capture_pipeline(pipeline) { yield }
|
24
|
-
instrument("pipeline", query: ["finish pipeline"])
|
25
23
|
end)
|
24
|
+
ensure
|
25
|
+
instrument("pipeline", query: ["finish pipeline"])
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
@@ -33,6 +33,7 @@ module RediSearch
|
|
33
33
|
def capture_pipeline(pipeline)
|
34
34
|
self.pipeline = pipeline
|
35
35
|
yield
|
36
|
+
ensure
|
36
37
|
self.pipeline = false
|
37
38
|
end
|
38
39
|
|
data/lib/redi_search/index.rb
CHANGED
@@ -14,6 +14,10 @@ module RediSearch
|
|
14
14
|
Search.new(self, term, **term_options)
|
15
15
|
end
|
16
16
|
|
17
|
+
def aggregate(term = nil, **term_options)
|
18
|
+
Aggregate.new(self, term, **term_options)
|
19
|
+
end
|
20
|
+
|
17
21
|
def spellcheck(query, distance: 1)
|
18
22
|
Spellcheck.new(self, query, distance: distance)
|
19
23
|
end
|
@@ -87,9 +91,11 @@ module RediSearch
|
|
87
91
|
info.num_docs.to_i
|
88
92
|
end
|
89
93
|
|
94
|
+
# rubocop:disable Style/ArgumentsForwarding
|
90
95
|
def add_field(name, type, **options, &block)
|
91
96
|
AddField.new(self, name, type, **options, &block).call!
|
92
97
|
end
|
98
|
+
# rubocop:enable Style/ArgumentsForwarding
|
93
99
|
|
94
100
|
private
|
95
101
|
|
@@ -1,6 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "active_support/version"
|
4
|
+
if ActiveSupport::VERSION::MAJOR >= 7 && ActiveSupport::VERSION::MINOR >= 1
|
5
|
+
require "active_support/deprecation"
|
6
|
+
require "active_support/deprecator"
|
7
|
+
end
|
4
8
|
require "active_support/log_subscriber"
|
5
9
|
if ActiveSupport::VERSION::MAJOR > 6
|
6
10
|
require "active_support/isolated_execution_state"
|
@@ -30,19 +34,20 @@ module RediSearch
|
|
30
34
|
command = command_string(event)
|
31
35
|
debug_color = action_color(event.payload[:action])
|
32
36
|
|
33
|
-
debug " #{log_name(event)} #{color(command, debug_color, true)}"
|
37
|
+
debug " #{log_name(event)} #{color(command, debug_color, bold: true)}"
|
34
38
|
end
|
35
39
|
|
36
40
|
private
|
37
41
|
|
38
42
|
def log_name(event)
|
39
|
-
color("#{event.payload[:name]} (#{event.duration.round(1)}ms)",
|
43
|
+
color("#{event.payload[:name]} (#{event.duration.round(1)}ms)",
|
44
|
+
RED, bold: true)
|
40
45
|
end
|
41
46
|
|
42
47
|
# rubocop:disable Metrics/MethodLength
|
43
48
|
def action_color(action)
|
44
49
|
case action.to_sym
|
45
|
-
when :search, :spellcheck then YELLOW
|
50
|
+
when :search, :spellcheck, :aggregate then YELLOW
|
46
51
|
when :create, :hset then GREEN
|
47
52
|
when :dropindex, :del then RED
|
48
53
|
when :hgetall, :info then CYAN
|
data/lib/redi_search/model.rb
CHANGED
@@ -18,7 +18,7 @@ module RediSearch
|
|
18
18
|
)
|
19
19
|
register_search_commit_hooks
|
20
20
|
|
21
|
-
scope :search_import, -> { all }
|
21
|
+
scope :search_import, -> { all } unless defined?(search_import)
|
22
22
|
|
23
23
|
include InstanceMethods
|
24
24
|
extend ModelClassMethods
|
@@ -39,6 +39,10 @@ module RediSearch
|
|
39
39
|
search_index.search(term, **term_options)
|
40
40
|
end
|
41
41
|
|
42
|
+
def aggregate(term = nil, **term_options)
|
43
|
+
search_index.aggregate(term, **term_options)
|
44
|
+
end
|
45
|
+
|
42
46
|
def spellcheck(term, distance: 1)
|
43
47
|
search_index.spellcheck(term, distance: distance)
|
44
48
|
end
|
@@ -28,6 +28,14 @@ module RediSearch
|
|
28
28
|
value.split(separator)
|
29
29
|
end
|
30
30
|
|
31
|
+
def serialize(record)
|
32
|
+
if value_block
|
33
|
+
record.instance_exec(&value_block)
|
34
|
+
else
|
35
|
+
record.public_send(name)
|
36
|
+
end.to_a
|
37
|
+
end
|
38
|
+
|
31
39
|
private
|
32
40
|
|
33
41
|
attr_reader :separator, :sortable, :no_index
|
data/lib/redi_search/schema.rb
CHANGED
@@ -10,29 +10,29 @@ module RediSearch
|
|
10
10
|
instance_exec(&block)
|
11
11
|
end
|
12
12
|
|
13
|
-
def text_field(name,
|
14
|
-
self[name] || push(Schema::TextField.new(name,
|
13
|
+
def text_field(name, ...)
|
14
|
+
self[name] || push(Schema::TextField.new(name, ...))
|
15
15
|
end
|
16
16
|
|
17
|
-
def numeric_field(name,
|
18
|
-
self[name] || push(Schema::NumericField.new(name,
|
17
|
+
def numeric_field(name, ...)
|
18
|
+
self[name] || push(Schema::NumericField.new(name, ...))
|
19
19
|
end
|
20
20
|
|
21
|
-
def tag_field(name,
|
22
|
-
self[name] || push(Schema::TagField.new(name,
|
21
|
+
def tag_field(name, ...)
|
22
|
+
self[name] || push(Schema::TagField.new(name, ...))
|
23
23
|
end
|
24
24
|
|
25
|
-
def geo_field(name,
|
26
|
-
self[name] || push(Schema::GeoField.new(name,
|
25
|
+
def geo_field(name, ...)
|
26
|
+
self[name] || push(Schema::GeoField.new(name, ...))
|
27
27
|
end
|
28
28
|
|
29
|
-
def add_field(name, type,
|
29
|
+
def add_field(name, type, ...)
|
30
30
|
case type
|
31
31
|
when :text then method(:text_field)
|
32
32
|
when :numeric then method(:numeric_field)
|
33
33
|
when :tag then method(:tag_field)
|
34
34
|
when :geo then method(:geo_field)
|
35
|
-
end.call(name,
|
35
|
+
end.call(name, ...)
|
36
36
|
end
|
37
37
|
|
38
38
|
def to_a
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RediSearch
|
4
|
+
class Search
|
5
|
+
module Clauses
|
6
|
+
class Timeout < ApplicationClause
|
7
|
+
clause_term :timeout,
|
8
|
+
numericality: { within: 0..Float::INFINITY,
|
9
|
+
only_integer: true }
|
10
|
+
clause_order 14
|
11
|
+
|
12
|
+
def initialize(timeout:)
|
13
|
+
@timeout = timeout
|
14
|
+
end
|
15
|
+
|
16
|
+
def clause
|
17
|
+
validate!
|
18
|
+
|
19
|
+
["TIMEOUT", timeout]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -3,96 +3,77 @@
|
|
3
3
|
module RediSearch
|
4
4
|
class Search
|
5
5
|
module Clauses
|
6
|
-
def
|
7
|
-
|
8
|
-
fields: fields, opening_tag: opening_tag, closing_tag: closing_tag
|
9
|
-
))
|
10
|
-
end
|
11
|
-
|
12
|
-
def slop(slop)
|
13
|
-
add_to_clause(Slop.new(slop: slop))
|
14
|
-
end
|
15
|
-
|
16
|
-
def in_order
|
17
|
-
add_to_clause(InOrder.new)
|
6
|
+
def no_content
|
7
|
+
add_to_clauses(NoContent.new)
|
18
8
|
end
|
19
9
|
|
20
10
|
def verbatim
|
21
|
-
|
11
|
+
add_to_clauses(Verbatim.new)
|
22
12
|
end
|
23
13
|
|
24
14
|
def no_stop_words
|
25
|
-
|
15
|
+
add_to_clauses(NoStopWords.new)
|
26
16
|
end
|
27
17
|
|
28
18
|
def with_scores
|
29
|
-
|
19
|
+
add_to_clauses(WithScores.new)
|
30
20
|
end
|
31
21
|
|
32
|
-
def
|
33
|
-
|
22
|
+
def with_payloads
|
23
|
+
add_to_clauses(WithPayloads.new)
|
34
24
|
end
|
35
25
|
|
36
|
-
def
|
37
|
-
|
26
|
+
def with_sort_keys
|
27
|
+
add_to_clauses(WithSortKeys.new)
|
38
28
|
end
|
39
29
|
|
40
|
-
def
|
41
|
-
|
30
|
+
def return(*fields)
|
31
|
+
add_to_clauses(Return.new(fields: fields))
|
42
32
|
end
|
43
33
|
|
44
|
-
def
|
45
|
-
|
34
|
+
def highlight(fields: [], opening_tag: "<b>", closing_tag: "</b>")
|
35
|
+
add_to_clauses(Highlight.new(
|
36
|
+
fields: fields, opening_tag: opening_tag, closing_tag: closing_tag
|
37
|
+
))
|
46
38
|
end
|
47
39
|
|
48
|
-
def
|
49
|
-
|
40
|
+
def slop(slop)
|
41
|
+
add_to_clauses(Slop.new(slop: slop))
|
50
42
|
end
|
51
43
|
|
52
|
-
def
|
53
|
-
|
54
|
-
|
55
|
-
RediSearch.client.call!(
|
56
|
-
"SEARCH", index.name, term_clause.to_s, *Limit.new(total: 0).clause
|
57
|
-
).first
|
44
|
+
def timeout(timeout)
|
45
|
+
add_to_clauses(Timeout.new(timeout: timeout))
|
58
46
|
end
|
59
47
|
|
60
|
-
def
|
61
|
-
|
62
|
-
|
63
|
-
self
|
48
|
+
def in_order
|
49
|
+
add_to_clauses(InOrder.new)
|
64
50
|
end
|
65
51
|
|
66
|
-
def
|
67
|
-
|
68
|
-
|
69
|
-
@term_clause.not(condition)
|
52
|
+
def language(language)
|
53
|
+
add_to_clauses(Language.new(language: language))
|
70
54
|
end
|
71
55
|
|
72
|
-
def
|
73
|
-
|
56
|
+
def sort_by(field, order: :asc)
|
57
|
+
add_to_clauses(SortBy.new(field: field, order: order))
|
58
|
+
end
|
74
59
|
|
75
|
-
|
76
|
-
|
77
|
-
else
|
78
|
-
self
|
79
|
-
end
|
60
|
+
def limit(total, offset = 0)
|
61
|
+
add_to_clauses(Limit.new(total: total, offset: offset))
|
80
62
|
end
|
81
63
|
|
82
|
-
def
|
83
|
-
|
64
|
+
def count
|
65
|
+
return to_a.size if loaded?
|
84
66
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
self
|
89
|
-
end
|
67
|
+
RediSearch.client.call!(
|
68
|
+
"SEARCH", index.name, query.to_s, *Limit.new(total: 0).clause
|
69
|
+
).first
|
90
70
|
end
|
91
71
|
|
92
72
|
private
|
93
73
|
|
94
|
-
def
|
95
|
-
clauses.push(
|
74
|
+
def add_to_clauses(clause)
|
75
|
+
clause.validate! && clauses.push(clause) if
|
76
|
+
used_clauses.add?(clause.class)
|
96
77
|
|
97
78
|
self
|
98
79
|
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module RediSearch
|
4
4
|
class Search
|
5
|
-
module
|
5
|
+
module Queries
|
6
6
|
class Boolean
|
7
7
|
extend Forwardable
|
8
8
|
|
@@ -65,10 +65,10 @@ module RediSearch
|
|
65
65
|
end
|
66
66
|
|
67
67
|
def queryify_search
|
68
|
-
if term.
|
69
|
-
term.
|
68
|
+
if term.query.is_a?(RediSearch::Search::Queries::Where)
|
69
|
+
term.query
|
70
70
|
else
|
71
|
-
"(#{term.
|
71
|
+
"(#{term.query})"
|
72
72
|
end
|
73
73
|
end
|
74
74
|
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module RediSearch
|
4
4
|
class Search
|
5
|
-
module
|
5
|
+
module Queries
|
6
6
|
class Where
|
7
7
|
extend Forwardable
|
8
8
|
|
@@ -39,7 +39,7 @@ module RediSearch
|
|
39
39
|
|
40
40
|
def queryify_term
|
41
41
|
if term.is_a? RediSearch::Search
|
42
|
-
"(#{term.
|
42
|
+
"(#{term.query})"
|
43
43
|
else
|
44
44
|
term.to_s
|
45
45
|
end
|