@@ -564,6 +564,88 @@ def testSummarize(self):
564
564
self .assertEqual ('ACT I SCENE I. London. The palace. Enter <b>KING</b> <b>HENRY</b>, LORD JOHN OF LANCASTER, the EARL of WESTMORELAND, SIR... ' ,
565
565
doc .txt )
566
566
567
+ def testAlias (self ):
568
+ conn = self .redis ()
569
+ with conn as r :
570
+ if check_version_2 (r ):
571
+
572
+ index1 = Client ('testAlias' , port = conn .port )
573
+ index1 .redis .flushdb ()
574
+ index2 = Client ('testAlias2' , port = conn .port )
575
+
576
+ index1 .redis .hset ("index1:lonestar" , mapping = {'name' : 'lonestar' })
577
+ index2 .redis .hset ("index2:yogurt" , mapping = {'name' : 'yogurt' })
578
+
579
+ time .sleep (2 )
580
+
581
+ def1 = IndexDefinition (prefix = ['index1:' ],score_field = 'name' )
582
+ def2 = IndexDefinition (prefix = ['index2:' ],score_field = 'name' )
583
+
584
+ index1 .create_index ((TextField ('name' ),),definition = def1 )
585
+ index2 .create_index ((TextField ('name' ),),definition = def2 )
586
+
587
+ res = index1 .search ('*' ).docs [0 ]
588
+ self .assertEqual ('index1:lonestar' , res .id )
589
+
590
+ # create alias and check for results
591
+ index1 .aliasadd ("spaceballs" )
592
+ alias_client = Client ('spaceballs' , port = conn .port )
593
+ res = alias_client .search ('*' ).docs [0 ]
594
+ self .assertEqual ('index1:lonestar' , res .id )
595
+
596
+ # We should throw an exception when trying to add an alias that already exists
597
+ with self .assertRaises (Exception ) as context :
598
+ index2 .aliasadd ('spaceballs' )
599
+ self .assertEqual ('Alias already exists' , str (context .exception ))
600
+
601
+ #update alias and ensure new results
602
+ index2 .aliasupdate ("spaceballs" )
603
+ alias_client2 = Client ('spaceballs' , port = conn .port )
604
+ res = alias_client2 .search ('*' ).docs [0 ]
605
+ self .assertEqual ('index2:yogurt' , res .id )
606
+
607
+ index2 .aliasdel ("spaceballs" )
608
+ with self .assertRaises (Exception ) as context :
609
+ alias_client2 .search ('*' ).docs [0 ]
610
+ self .assertEqual ('spaceballs: no such index' , str (context .exception ))
611
+
612
+ else :
613
+
614
+ # Creating a client with one index
615
+ index1 = Client ('testAlias' , port = conn .port )
616
+ index1 .redis .flushdb ()
617
+
618
+ index1 .create_index ((TextField ('txt' ),))
619
+ index1 .add_document ('doc1' , txt = 'text goes here' )
620
+
621
+ index2 = Client ('testAlias2' , port = conn .port )
622
+ index2 .create_index ((TextField ('txt' ),))
623
+ index2 .add_document ('doc2' , txt = 'text goes here' )
624
+
625
+
626
+ # add the actual alias and check
627
+ index1 .aliasadd ('myalias' )
628
+ alias_client = Client ('myalias' , port = conn .port )
629
+ res = alias_client .search ('*' ).docs [0 ]
630
+ self .assertEqual ('doc1' , res .id )
631
+
632
+ # We should throw an exception when trying to add an alias that already exists
633
+ with self .assertRaises (Exception ) as context :
634
+ index2 .aliasadd ('myalias' )
635
+ self .assertEqual ('Alias already exists' , str (context .exception ))
636
+
637
+ # update the alias and ensure we get doc2
638
+ index2 .aliasupdate ('myalias' )
639
+ alias_client2 = Client ('myalias' , port = conn .port )
640
+ res = alias_client2 .search ('*' ).docs [0 ]
641
+ self .assertEqual ('doc2' , res .id )
642
+
643
+ # delete the alias and expect an error if we try to query again
644
+ index2 .aliasdel ('myalias' )
645
+ with self .assertRaises (Exception ) as context :
646
+ alias_client2 .search ('*' ).docs [0 ]
647
+ self .assertEqual ('myalias: no such index' , str (context .exception ))
648
+
567
649
def testTags (self ):
568
650
conn = self .redis ()
569
651
0 commit comments