22"""MoreLikeThis example: find similar documents without vectors.
33
44This example demonstrates ParadeDB's MoreLikeThis feature, which finds similar
5- documents based on term frequency analysis (TF-IDF), not vector embeddings.
6-
7- NOTE: MoreLikeThis is used directly in .filter(), not wrapped in ParadeDB().
8- This is because it's a table-level similarity query that uses the primary key
9- internally, rather than a field-targeted search expression.
5+ documents based BM25 provided by ParadeDB, not vector embeddings.
106
117Use cases:
128- "Related products" on product pages
139- "Similar articles" recommendations
1410- Content discovery and exploration
1511"""
1612
17- from _common import MockItem , setup_mock_items
13+ import sys
14+ from pathlib import Path
15+
16+ # Add parent directory to path for importing common utilities
17+ sys .path .insert (0 , str (Path (__file__ ).parent .parent ))
18+
19+ from common import MockItem , setup_mock_items
1820
1921from paradedb .functions import Score
2022from paradedb .search import MoreLikeThis
@@ -83,8 +85,8 @@ def demo_similar_to_multiple_products() -> None:
8385 print (f" { item .id } : { item .description [:50 ]} ... [{ item .category } ]" )
8486
8587
86- def demo_similar_by_text () -> None :
87- """Find products similar to a text description .
88+ def demo_similar_by_document () -> None :
89+ """Find products similar to a custom document .
8890
8991 Use case: User describes what they want, find matching products.
9092 This is different from regular search - it uses MLT's term analysis.
@@ -97,11 +99,10 @@ def demo_similar_by_text() -> None:
9799 user_description = "comfortable wireless audio for running"
98100 print (f"\n User wants: '{ user_description } '" )
99101
100- # MoreLikeThis with text requires fields parameter
101102 print ("\n Matching products:" )
102103 similar = (
103104 MockItem .objects .filter (
104- MoreLikeThis (text = user_description , fields = [ "description" ] )
105+ MoreLikeThis (document = { "description" : user_description } )
105106 )
106107 .annotate (score = Score ())
107108 .order_by ("-score" )[:5 ]
@@ -235,7 +236,7 @@ def demo_multifield_similarity() -> None:
235236
236237 demo_similar_to_single_product ()
237238 demo_similar_to_multiple_products ()
238- demo_similar_by_text ()
239+ demo_similar_by_document ()
239240 demo_tuning_parameters ()
240241 demo_combined_with_filters ()
241242 demo_multifield_similarity ()
0 commit comments