6
6
use Elastica \Index ;
7
7
use Elastica \Percolator ;
8
8
use Elastica \Query \Term ;
9
+ use Elastica \Query ;
9
10
use Elastica \Test \Base as BaseTest ;
10
11
11
12
class PercolatorTest extends BaseTest
@@ -42,13 +43,26 @@ public function testMatchDoc()
42
43
$ percolator = new Percolator ($ index );
43
44
44
45
$ percolatorName = 'percotest ' ;
46
+ $ percolatorSecondName = 'percotest_color ' ;
45
47
46
48
$ query = new Term (array ('name ' => 'ruflin ' ));
47
49
$ response = $ percolator ->registerQuery ($ percolatorName , $ query );
48
50
49
51
$ this ->assertTrue ($ response ->isOk ());
50
52
$ this ->assertFalse ($ response ->hasError ());
51
53
54
+ // Add index with the same query and additional parameter
55
+ $ secondParamKey = 'color ' ;
56
+ $ secondParamValue = 'blue ' ;
57
+ $ querySecond = new Query ();
58
+ $ querySecond ->setQuery ($ query );
59
+ $ querySecond ->setParam ($ secondParamKey , $ secondParamValue );
60
+ $ responseSecond = $ percolator ->registerQuery ($ percolatorSecondName , $ querySecond );
61
+
62
+ // Check if it's ok
63
+ $ this ->assertTrue ($ responseSecond ->isOk ());
64
+ $ this ->assertFalse ($ responseSecond ->hasError ());
65
+
52
66
$ doc1 = new Document ();
53
67
$ doc1 ->set ('name ' , 'ruflin ' );
54
68
@@ -62,9 +76,27 @@ public function testMatchDoc()
62
76
$ matches1 = $ percolator ->matchDoc ($ doc1 );
63
77
64
78
$ this ->assertTrue (in_array ($ percolatorName , $ matches1 ));
65
- $ this ->assertEquals (1 , count ($ matches1 ));
79
+ $ this ->assertTrue (in_array ($ percolatorSecondName , $ matches1 ));
80
+ $ this ->assertEquals (2 , count ($ matches1 ));
66
81
67
82
$ matches2 = $ percolator ->matchDoc ($ doc2 );
68
83
$ this ->assertEmpty ($ matches2 );
84
+
85
+ // Test using document with additional parameter
86
+ $ docSecond = $ doc1 ;
87
+ $ docSecond ->set ($ secondParamKey , $ secondParamValue );
88
+
89
+ // Create term for our parameter to filter data while percolating
90
+ $ secondTerm = new Term (array ($ secondParamKey => $ secondParamValue ));
91
+ $ secondQuery = new Query ();
92
+ $ secondQuery ->setQuery ($ secondTerm );
93
+
94
+ // Match the document to percolator queries and filter results using optional parameter
95
+ $ secondMatches = $ percolator ->matchDoc ($ docSecond , $ secondQuery );
96
+
97
+ // Check if only one proper index was returned
98
+ $ this ->assertFalse (in_array ($ percolatorName , $ secondMatches ));
99
+ $ this ->assertTrue (in_array ($ percolatorSecondName , $ secondMatches ));
100
+ $ this ->assertEquals (1 , count ($ secondMatches ));
69
101
}
70
102
}
0 commit comments