@@ -34,10 +34,10 @@ public function setUp()
34
34
private function prepareDbData ()
35
35
{
36
36
$ command = $ this ->getConnection ()->createCommand ();
37
- $ command ->insert ('yiitest ' , 'article ' , ['title ' => 'I love yii! ' ], 1 );
38
- $ command ->insert ('yiitest ' , 'article ' , ['title ' => 'Symfony2 is another framework ' ], 2 );
39
- $ command ->insert ('yiitest ' , 'article ' , ['title ' => 'Yii2 out now! ' ], 3 );
40
- $ command ->insert ('yiitest ' , 'article ' , ['title ' => 'yii test ' ], 4 );
37
+ $ command ->insert ('yiitest ' , 'article ' , ['title ' => 'I love yii! ' , ' weight ' => 1 ], 1 );
38
+ $ command ->insert ('yiitest ' , 'article ' , ['title ' => 'Symfony2 is another framework ' , ' weight ' => 2 ], 2 );
39
+ $ command ->insert ('yiitest ' , 'article ' , ['title ' => 'Yii2 out now! ' , ' weight ' => 3 ], 3 );
40
+ $ command ->insert ('yiitest ' , 'article ' , ['title ' => 'yii test ' , ' weight ' => 4 ], 4 );
41
41
42
42
$ command ->flushIndex ('yiitest ' );
43
43
}
@@ -67,20 +67,34 @@ public function testYiiCanBeFoundByQuery()
67
67
68
68
public function testMinScore ()
69
69
{
70
- if (version_compare ($ this ->version , '1.6 ' , '< ' )) {
71
- $ this ->markTestSkipped ('Score calculation in ES < 1.6 is untestable ' );
72
- }
73
- $ queryParts = ['term ' => ['title ' => 'yii ' ]];
70
+ $ queryParts = [
71
+ 'function_score ' => [
72
+ 'boost_mode ' => 'replace ' ,
73
+ 'query ' => ['term ' => ['title ' => 'yii ' ]],
74
+ 'functions ' => [
75
+ ['script_score ' => [
76
+ 'script ' => "doc['weight'].getValue() " ,
77
+ ]],
78
+ ],
79
+ ],
80
+ ];
81
+ //without min_score should get 2 documents with weights 1 and 4
82
+
74
83
$ query = new Query ();
75
84
$ query ->from ('yiitest ' , 'article ' );
76
- $ query ->query = $ queryParts ;
77
- $ query ->minScore (0.9 );
85
+ $ query ->query ($ queryParts );
86
+
87
+ $ query ->minScore (0.5 );
78
88
$ result = $ query ->search ($ this ->getConnection ());
79
- $ this ->assertEquals (0 , $ result ['hits ' ]['total ' ]);
89
+ $ this ->assertEquals (2 , $ result ['hits ' ]['total ' ]);
80
90
81
- $ query ->minScore (0.6 );
91
+ $ query ->minScore (2 );
82
92
$ result = $ query ->search ($ this ->getConnection ());
83
93
$ this ->assertEquals (1 , $ result ['hits ' ]['total ' ]);
94
+
95
+ $ query ->minScore (5 );
96
+ $ result = $ query ->search ($ this ->getConnection ());
97
+ $ this ->assertEquals (0 , $ result ['hits ' ]['total ' ]);
84
98
}
85
99
86
100
public function testMltSearch ()
0 commit comments