1+ from ravendb import MoreLikeThisOptions
12from ravendb .documents .indexes .definitions import FieldIndexing , FieldStorage
23from ravendb .documents .indexes .index_creation import IndexCreation , AbstractIndexCreationTask
3- from ravendb .infrastructure .entities import User
4+ from ravendb .infrastructure .entities import User , Post
45from ravendb .tests .test_base import TestBase
56
67
@@ -15,6 +16,19 @@ def __init__(self):
1516 self ._store ("name" , FieldStorage .YES )
1617
1718
19+ class Posts_ByTitleAndDesc (AbstractIndexCreationTask ):
20+ def __init__ (self ):
21+ super (Posts_ByTitleAndDesc , self ).__init__ ()
22+ self .map = "from p in docs.Posts select new { p.title, p.desc }"
23+ self ._index ("title" , FieldIndexing .SEARCH )
24+ self ._store ("title" , FieldIndexing .YES )
25+ self ._analyze ("title" , "Lucene.Net.Analysis.SimpleAnalyzer" )
26+
27+ self ._index ("desc" , FieldIndexing .SEARCH )
28+ self ._store ("desc" , FieldStorage .YES )
29+ self ._analyze ("desc" , "Lucene.Net.Analysis.SimpleAnalyzer" )
30+
31+
1832class TestIndexesFromClient (TestBase ):
1933 def setUp (self ):
2034 super (TestIndexesFromClient , self ).setUp ()
@@ -32,3 +46,37 @@ def test_can_create_indexes_using_index_creation(self):
3246 with self .store .open_session () as session :
3347 users = list (session .query_index_type (Users_ByName , User ))
3448 self .assertEqual (1 , len (users ))
49+
50+ def test_more_like_this (self ):
51+ with self .store .open_session () as session :
52+ post1 = Post ("posts/1" , "doduck" , "prototype" )
53+ post2 = Post ("posts/2" , "doduck" , "prototype your idea" )
54+ post3 = Post ("posts/3" , "doduck" , "love programming" )
55+ post4 = Post ("posts/4" , "We do" , "prototype" )
56+ post5 = Post ("posts/5" , "We love" , "challange" )
57+
58+ session .store (post1 )
59+ session .store (post2 )
60+ session .store (post3 )
61+ session .store (post4 )
62+ session .store (post5 )
63+ session .save_changes ()
64+
65+ Posts_ByTitleAndDesc ().execute (self .store )
66+ self .wait_for_indexing (self .store )
67+
68+ with self .store .open_session () as session :
69+ options = MoreLikeThisOptions (minimum_document_frequency = 1 , minimum_term_frequency = 0 )
70+ results = list (
71+ session .query_index_type (Posts_ByTitleAndDesc , Post ).more_like_this (
72+ lambda f : f .using_document (lambda x : x .where_equals ("id()" , "posts/1" )).with_options (options )
73+ )
74+ )
75+ self .assertEqual (3 , len (results ))
76+
77+ self .assertEqual ("doduck" , results [0 ].title )
78+ self .assertEqual ("prototype your idea" , results [0 ].desc )
79+ self .assertEqual ("doduck" , results [1 ].title )
80+ self .assertEqual ("love programming" , results [1 ].desc )
81+ self .assertEqual ("We do" , results [2 ].title )
82+ self .assertEqual ("prototype" , results [2 ].desc )
0 commit comments