Skip to content

Commit e4e7bfd

Browse files
authored
chore: Manual review end to end (#4)
1 parent 8373603 commit e4e7bfd

7 files changed

Lines changed: 87 additions & 74 deletions

File tree

examples/autocomplete/autocomplete.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ def demo_autocomplete
2727
.with_score
2828
.order(search_score: :desc)
2929
.limit(5)
30-
31-
results.each do |item|
32-
puts format(" - %-50s (score: %.2f)", item.description.truncate(50), item.search_score)
33-
end
34-
puts " (no results)" if results.empty?
30+
puts results.map { |item| " - #{item.description.truncate(50)} (score: #{item.search_score.round(2)})" }
3531
end
3632
end
3733

examples/faceted_search/faceted_search.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,18 @@
2525
facets = relation.facets
2626

2727
puts "Top results:"
28-
rows.each do |item|
28+
puts rows.map { |item|
2929
color = item.metadata&.fetch("color", nil) || "N/A"
30-
stock = item.in_stock ? "In Stock" : "Out of Stock"
31-
puts " - #{item.description[0, 50]}... [#{item.category}] " \
32-
"(rating: #{item.rating}, #{stock}, color: #{color})"
33-
end
30+
" - #{item.description.truncate(50)} [#{item.category}] (rating: #{item.rating}, color: #{color})"
31+
}
3432

3533
puts "\nFacet buckets:"
3634
facets.each do |key, data|
3735
buckets = data.is_a?(Hash) ? Array(data["buckets"]) : []
3836
puts "#{key} (#{buckets.length} buckets)"
39-
buckets.each do |bucket|
40-
puts " - #{bucket["key"]}: #{bucket["doc_count"]}"
41-
end
37+
puts buckets.map { |bucket| " - #{bucket["key"]}: #{bucket["doc_count"]}" }
4238
end
4339

44-
puts "\n" + "=" * 60
40+
puts "\n#{"=" * 60}"
4541
puts "Done!"
4642
end

examples/more_like_this/more_like_this.rb

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require_relative "../common"
55

66
def demo_similar_to_single_product
7-
puts "\n" + "=" * 60
7+
puts "\n#{"=" * 60}"
88
puts "Demo 1: Similar to a single product"
99
puts "=" * 60
1010

@@ -19,69 +19,55 @@ def demo_similar_to_single_product
1919
.order(search_score: :desc)
2020
.limit(5)
2121

22-
similar.each do |item|
22+
puts similar.map { |item|
2323
marker = item.id == source_id ? " (source)" : ""
24-
puts format(" %<id>d: %<desc>s... [%<category>s]%<marker>s",
25-
id: item.id,
26-
desc: item.description[0, 50],
27-
category: item.category,
28-
marker: marker)
29-
end
24+
" #{item.id}: #{item.description.truncate(50)} [#{item.category}]#{marker}"
25+
}
3026
end
3127

3228
def demo_similar_to_multiple_products
33-
puts "\n" + "=" * 60
29+
puts "\n#{"=" * 60}"
3430
puts "Demo 2: Similar to multiple products (browsing history)"
3531
puts "=" * 60
3632

3733
browsed_ids = [3, 12, 29]
3834
browsed = MockItem.where(id: browsed_ids)
3935

4036
puts "\nUser's browsing history:"
41-
browsed.each do |item|
42-
puts " #{item.id}: #{item.description[0, 50]}... [#{item.category}]"
43-
end
37+
puts browsed.map { |item| " #{item.id}: #{item.description.truncate(50)} [#{item.category}]" }
4438

45-
relations = browsed_ids.map { |id| MockItem.more_like_this(id, fields: [:description]) }
46-
combined = relations.reduce { |memo, relation| memo.or(relation) }
39+
combined_description = browsed.pluck(:description).join(" ")
40+
json_doc = { description: combined_description }.to_json
4741

48-
puts "\nRecommended products (similar to any browsed item):"
49-
similar = combined.where.not(id: browsed_ids)
50-
.extending(ParadeDB::SearchMethods)
42+
puts "\nRecommended products (similar to browsing history):"
43+
similar = MockItem.more_like_this(json_doc)
44+
.where.not(id: browsed_ids)
5145
.with_score
5246
.order(search_score: :desc)
5347
.limit(5)
5448

55-
similar.each do |item|
56-
puts " #{item.id}: #{item.description[0, 50]}... [#{item.category}]"
57-
end
49+
puts similar.map { |item| " #{item.id}: #{item.description.truncate(50)} [#{item.category}]" }
5850
end
5951

6052
def demo_combined_with_filters
61-
puts "\n" + "=" * 60
62-
puts "Demo 3: MoreLikeThis + ActiveRecord filters"
53+
puts "\n#{"=" * 60}"
54+
puts "Demo 3: MoreLikeThis + Filters (in_stock=true, rating >= 4)"
6355
puts "=" * 60
6456

6557
source_id = 15
66-
source = MockItem.find(source_id)
67-
puts "\nSource: '#{source.description}' (rating: #{source.rating})"
6858

69-
puts "\nSimilar products (in_stock=true, rating >= 4):"
7059
results = MockItem.more_like_this(source_id, fields: [:description])
7160
.where(in_stock: true)
7261
.where("rating >= ?", 4)
7362
.with_score
7463
.order(search_score: :desc)
7564
.limit(5)
7665

77-
results.each do |item|
78-
stock = item.in_stock ? "In Stock" : "Out of Stock"
79-
puts " #{item.id}: #{item.description[0, 40]}... (rating: #{item.rating}, #{stock})"
80-
end
66+
puts results.map { |item| " #{item.id}: #{item.description.truncate(40)} (rating: #{item.rating})" }
8167
end
8268

8369
def demo_multifield_similarity
84-
puts "\n" + "=" * 60
70+
puts "\n#{"=" * 60}"
8571
puts "Demo 4: Multi-field similarity"
8672
puts "=" * 60
8773

@@ -91,17 +77,11 @@ def demo_multifield_similarity
9177

9278
puts "\nSimilar by DESCRIPTION only:"
9379
by_description = MockItem.more_like_this(source_id, fields: [:description]).where.not(id: source_id).limit(3)
94-
by_description.each do |item|
95-
puts " #{item.id}: #{item.description[0, 40]}... [#{item.category}]"
96-
end
80+
puts by_description.map { |item| " #{item.id}: #{item.description.truncate(40)} [#{item.category}]" }
9781

9882
puts "\nSimilar by DESCRIPTION + CATEGORY:"
9983
by_both = MockItem.more_like_this(source_id, fields: [:description, :category]).where.not(id: source_id).limit(3)
100-
by_both.each do |item|
101-
puts " #{item.id}: #{item.description[0, 40]}... [#{item.category}]"
102-
end
103-
rescue ActiveRecord::StatementInvalid => error
104-
puts " (Skipped: #{error.message})"
84+
puts by_both.map { |item| " #{item.id}: #{item.description.truncate(40)} [#{item.category}]" }
10585
end
10686

10787
if $PROGRAM_NAME == __FILE__
@@ -118,6 +98,6 @@ def demo_multifield_similarity
11898
demo_combined_with_filters
11999
demo_multifield_similarity
120100

121-
puts "\n" + "=" * 60
101+
puts "\n#{"=" * 60}"
122102
puts "Done!"
123103
end

examples/quickstart/quickstart.rb

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
def demo_basic_search
77
puts "\n--- Basic Search: 'shoes' ---"
8-
MockItem.search(:description).matching_all("shoes").limit(5).each do |item|
9-
puts " - #{item.description[0, 60]}..."
10-
end
8+
results = MockItem.search(:description).matching_all("shoes").limit(5)
9+
puts results.map { |item| " - #{item.description.truncate(60)}" }
1110
end
1211

1312
def demo_scored_search
@@ -18,9 +17,7 @@ def demo_scored_search
1817
.order(search_score: :desc)
1918
.limit(5)
2019

21-
results.each do |item|
22-
puts format(" - %-50s (score: %.2f)", "#{item.description[0, 50]}...", item.search_score.to_f)
23-
end
20+
puts results.map { |item| " - #{item.description.truncate(50)} (score: #{item.search_score.round(2)})" }
2421
end
2522

2623
def demo_phrase_search
@@ -31,9 +28,7 @@ def demo_phrase_search
3128
.order(search_score: :desc)
3229
.limit(5)
3330

34-
results.each do |item|
35-
puts format(" - %-50s (score: %.2f)", "#{item.description[0, 50]}...", item.search_score.to_f)
36-
end
31+
puts results.map { |item| " - #{item.description.truncate(50)} (score: #{item.search_score.round(2)})" }
3732
end
3833

3934
def demo_snippet_highlighting
@@ -45,9 +40,7 @@ def demo_snippet_highlighting
4540
.order(search_score: :desc)
4641
.limit(3)
4742

48-
results.each do |item|
49-
puts " - #{item.description_snippet}"
50-
end
43+
puts results.map { |item| " - #{item.description_snippet}" }
5144
end
5245

5346
def demo_filtered_search
@@ -60,9 +53,7 @@ def demo_filtered_search
6053
.order(search_score: :desc)
6154
.limit(5)
6255

63-
results.each do |item|
64-
puts " - #{item.description[0, 40]}... (rating: #{item.rating})"
65-
end
56+
puts results.map { |item| " - #{item.description.truncate(40)} (rating: #{item.rating})" }
6657
end
6758

6859
if $PROGRAM_NAME == __FILE__

examples/rag/rag.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,14 @@ def generate(query, context)
9595
end
9696

9797
puts "\nRetrieved #{items.length} products:"
98-
items.each do |item|
99-
puts format(" - %-60s (score: %.2f)", item.description, item.search_score.to_f)
100-
end
98+
puts items.map { |item| " - #{item.description} (score: #{item.search_score.round(2)})" }
10199

102100
context = format_context(items)
103101
puts "\nAnswer:"
104102
puts "-" * 40
105103
puts generate(query, context)
106104
end
107105

108-
puts "\n" + "=" * 60
106+
puts "\n#{"=" * 60}"
109107
puts "Done!"
110108
end

spec/user_api_behavior_integration_spec.rb

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,48 @@ def test_term_regex_and_fuzzy_execute
6969
assert_equal [1, 2], fuzzy_ids
7070
end
7171

72-
def test_more_like_this_executes_and_returns_similar_rows
72+
def test_more_like_this_with_id_executes_and_returns_similar_rows
7373
ids = BehaviorProduct.more_like_this(3, fields: [:description]).limit(5).pluck(:id)
7474

7575
assert_includes ids, 4
7676
end
7777

78+
def test_more_like_this_with_json_single_field_executes
79+
json_doc = { description: "running shoes" }.to_json
80+
ids = BehaviorProduct.more_like_this(json_doc).order(:id).pluck(:id)
81+
82+
assert_includes ids, 1
83+
assert_includes ids, 2
84+
end
85+
86+
def test_more_like_this_with_json_multiple_fields_executes
87+
json_doc = { description: "running shoes", category: "footwear" }.to_json
88+
ids = BehaviorProduct.more_like_this(json_doc).order(:id).pluck(:id)
89+
90+
assert_includes ids, 1
91+
assert_includes ids, 2
92+
end
93+
94+
def test_more_like_this_with_json_category_only_executes
95+
json_doc = { category: "audio" }.to_json
96+
ids = BehaviorProduct.more_like_this(json_doc).order(:id).pluck(:id)
97+
98+
assert_includes ids, 3
99+
assert_includes ids, 4
100+
end
101+
102+
def test_more_like_this_with_json_combined_with_filters_executes
103+
json_doc = { description: "running" }.to_json
104+
ids = BehaviorProduct.more_like_this(json_doc)
105+
.where(in_stock: true)
106+
.where("rating >= ?", 4)
107+
.order(:id)
108+
.pluck(:id)
109+
110+
assert_includes ids, 1
111+
assert_includes ids, 2
112+
end
113+
78114
def test_with_score_and_with_snippet_materialize_columns
79115
rows = BehaviorProduct.search(:description)
80116
.matching_all("running shoes")

spec/user_api_unit_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,27 @@ def test_match_all_wrapper
8080
assert_sql_equal %(SELECT products.* FROM products WHERE ("products"."id" @@@ pdb.all())), sql
8181
end
8282

83-
def test_more_like_this
83+
def test_more_like_this_with_id
8484
sql = UnitProduct.more_like_this(5, fields: [:description]).to_sql
8585
assert_sql_equal %(SELECT products.* FROM products WHERE ("products"."id" @@@ pdb.more_like_this(5, ARRAY['description']))), sql
8686
end
8787

88+
def test_more_like_this_with_json_string
89+
sql = UnitProduct.more_like_this('{"description": "running shoes"}').to_sql
90+
assert_sql_equal %(SELECT products.* FROM products WHERE ("products"."id" @@@ pdb.more_like_this('{"description": "running shoes"}'))), sql
91+
end
92+
93+
def test_more_like_this_with_json_and_fields
94+
sql = UnitProduct.more_like_this('{"description": "running shoes", "category": "footwear"}', fields: [:description, :category]).to_sql
95+
assert_sql_equal %(SELECT products.* FROM products WHERE ("products"."id" @@@ pdb.more_like_this('{"description": "running shoes", "category": "footwear"}', ARRAY['description', 'category']))), sql
96+
end
97+
98+
def test_more_like_this_with_json_hash
99+
json_doc = { description: "running shoes", category: "footwear" }.to_json
100+
sql = UnitProduct.more_like_this(json_doc).to_sql
101+
assert_sql_equal %(SELECT products.* FROM products WHERE ("products"."id" @@@ pdb.more_like_this('{"description":"running shoes","category":"footwear"}'))), sql
102+
end
103+
88104
def test_excluding
89105
sql = UnitProduct.search(:description).matching_all("shoes").excluding("cheap").to_sql
90106
assert_sql_equal %(SELECT products.* FROM products WHERE ("products"."description" &&& 'shoes') AND (NOT ("products"."description" &&& 'cheap'))), sql

0 commit comments

Comments
 (0)