44require_relative "../common"
55
66def 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+ }
3026end
3127
3228def 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 "\n User'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 "\n Recommended products (similar to any browsed item ):"
49- similar = combined . where . not ( id : browsed_ids )
50- . extending ( ParadeDB :: SearchMethods )
42+ puts "\n Recommended 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 } ]" }
5850end
5951
6052def 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 "\n Source: '#{ source . description } ' (rating: #{ source . rating } )"
6858
69- puts "\n Similar 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 } )" }
8167end
8268
8369def 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 "\n Similar 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 "\n Similar 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 } ]" }
10585end
10686
10787if $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!"
123103end
0 commit comments