Skip to content

Commit

Permalink
Improve search specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaskins committed Dec 20, 2024
1 parent 38522d2 commit efb5e6c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 99 deletions.
81 changes: 8 additions & 73 deletions spec/search_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,18 @@ require "../src/search"
require "../src/json"

private macro get_info(index)
redis.ft.info({{index}}).as(Array)
end

private macro wait_for_indexing_complete(index)
%key_index = 40
%failures_index = 38
private def wait_for_indexing_complete(redis, index)
while info = redis.ft.info(index).as(Array).in_slices_of(2).to_h
break if info["indexing"] != "0"

while (%info = get_info({{index}})) && %info[%key_index + 1] != "0"
unless %info[%key_index] == "indexing"
raise "wrong array index: #{%info[%key_index].inspect}"
end
unless %info[%failures_index] == "hash_indexing_failures"
raise %{#{%info[%failures_index]} != "hash_indexing_failures"}
end

unless %info[%failures_index + 1] == "0"
raise "Hash indexing failures: #{%info[%failures_index]}"
unless info["hash_indexing_failures"] == "0"
raise "Hash indexing failures: #{info["hash_indexing_failures"]}"
end
end

%info
info
end

redis = Redis::Client.new
Expand Down Expand Up @@ -64,8 +55,8 @@ module Redis
$.section AS section TEXT NOSTEM
INDEX

wait_for_indexing_complete hash_index
wait_for_indexing_complete json_index
wait_for_indexing_complete redis, hash_index
wait_for_indexing_complete redis, json_index
end

after_all do # you're my wonderwall
Expand All @@ -75,62 +66,6 @@ module Redis
redis.unlink keys.map(&.as(String))
end

pending "gets index metadata" do
expected = [
"index_name", hash_index,
"index_options", [] of String,
"index_definition", [
"key_type", "HASH",
"prefixes", ["#{hash_prefix}:"],
"default_score", "1",
],
"attributes", [
[
"identifier", "name",
"attribute", "name",
"type", "TEXT",
"WEIGHT", "1",
"SORTABLE", "NOSTEM",
],
],
"num_docs", "0",
"max_doc_id", "0",
"num_terms", "0",
"num_records", "0",
"inverted_sz_mb", "0",
"vector_index_sz_mb", "0",
"total_inverted_index_blocks", "4",
"offset_vectors_sz_mb", "0",
"doc_table_size_mb", "0",
"sortable_values_size_mb", "0",
"key_table_size_mb", "0",
"records_per_doc_avg", "nan",
"bytes_per_record_avg", "nan",
"offsets_per_term_avg", "nan",
"offset_bits_per_record_avg", "nan",
"hash_indexing_failures", "0",
"indexing", "0",
"percent_indexed", "1",
"gc_stats", [
"bytes_collected", "0",
"total_ms_run", "0",
"total_cycles", "0",
"average_cycle_time_ms", "nan",
"last_run_time_ms", "0",
"gc_numeric_trees_missed", "0",
"gc_blocks_denied", "0",
],
"cursor_stats", [
"global_idle", 0,
"global_total", 0,
"index_capacity", 128,
"index_total", 0,
],
]

redis.ft.info(hash_index).should eq expected
end

describe "hashes" do
it "does a simple search" do
redis.hset "#{hash_prefix}:simple:match", "name", "included"
Expand Down
6 changes: 3 additions & 3 deletions src/connection.cr
Original file line number Diff line number Diff line change
Expand Up @@ -355,16 +355,16 @@ module Redis
loop do
@writer.encode command
flush
result = read
@log.debug &.emit "redis", command: command[0...2].join(' '), duration_ms: (Time.monotonic - start).total_milliseconds
return result
return read
rescue ex : IO::Error
if retries > 0
retries -= 1
initialize @uri
else
raise ex
end
ensure
@log.debug &.emit "redis", command: command.join(' '), duration_ms: (Time.monotonic - start).total_milliseconds
end
end

Expand Down
43 changes: 20 additions & 23 deletions src/search.cr
Original file line number Diff line number Diff line change
Expand Up @@ -149,34 +149,31 @@ module Redis
# Pre-allocate the command buffer based on args so it performs as few
# heap allocations as possible.
command = Array(String).new(
1 + # ft.search
1 + # index
1 + # query
1 + # nocontent
1 + # verbatim
1 + # nostopwords
1 + # withscores
1 + # withpayloads
4 + # filter
6 + # geofilter
3 + # ft.search index query
(nocontent ? 1 : 0) +
(verbatim ? 1 : 0) +
(nostopwords ? 1 : 0) +
(withscores ? 1 : 0) +
(withpayloads ? 1 : 0) +
(filter ? 4 : 0) +
(geofilter ? 6 : 0) +
(inkeys.try(&.size) || 0) + 1 +
(infields.try(&.size) || 0) + 1 +
(return_value.try(&.size) || 0) + 1 +
(summarize.try(&.fields).try(&.size) || 0) + 8 +
(highlight.try(&.fields).try(&.size) || 0) + 6 +
2 + # slop
2 + # timeout
1 + # inorder
2 + # language
2 + # expander
2 + # scorer
1 + # explainscore
2 + # payload
3 + # sortby
3 + # limit
1 + (params.try { |params| params.size * 2 } || 0) +
2 + # dialect
0 # end
(slop ? 2 : 0) +
(timeout ? 2 : 0) +
(inorder ? 1 : 0) +
(language ? 2 : 0) +
(expander ? 2 : 0) +
(scorer ? 2 : 0) +
(explainscore ? 1 : 0) +
(payload ? 2 : 0) +
(sortby ? 3 : 0) +
(limit ? 3 : 0) +
(params ? (1 + (params.try { |params| params.size * 2 } || 0)) : 0) +
2 # dialect
)
command << "ft.search" << index << query

Expand Down

0 comments on commit efb5e6c

Please sign in to comment.