1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Query; |
4
|
|
|
|
5
|
|
|
use Elastica\Document; |
6
|
|
|
use Elastica\Mapping; |
7
|
|
|
use Elastica\Query\Wildcard; |
8
|
|
|
use Elastica\Test\Base as BaseTest; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @internal |
12
|
|
|
*/ |
13
|
|
|
class WildcardTest extends BaseTest |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @group unit |
17
|
|
|
*/ |
18
|
|
|
public function testConstruct(): void |
19
|
|
|
{ |
20
|
|
|
$wildcard = new Wildcard('name', 'aaa*'); |
21
|
|
|
|
22
|
|
|
$data = $wildcard->getParam('name'); |
23
|
|
|
$this->assertIsArray($data); |
24
|
|
|
|
25
|
|
|
$this->assertSame('aaa*', $data['value']); |
26
|
|
|
$this->assertSame(1.0, $data['boost']); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @group unit |
31
|
|
|
*/ |
32
|
|
View Code Duplication |
public function testToArray(): void |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$wildcard = new Wildcard('name', 'value*', 2.0); |
35
|
|
|
$wildcard->setRewrite(Wildcard::REWRITE_SCORING_BOOLEAN); |
36
|
|
|
|
37
|
|
|
$expectedArray = [ |
38
|
|
|
'wildcard' => [ |
39
|
|
|
'name' => [ |
40
|
|
|
'value' => 'value*', |
41
|
|
|
'boost' => 2.0, |
42
|
|
|
'rewrite' => 'scoring_boolean', |
43
|
|
|
], |
44
|
|
|
], |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
$this->assertEquals($expectedArray, $wildcard->toArray()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @group functional |
52
|
|
|
*/ |
53
|
|
|
public function testSearchWithAnalyzer(): void |
54
|
|
|
{ |
55
|
|
|
$client = $this->_getClient(); |
56
|
|
|
$index = $client->getIndex('test'); |
57
|
|
|
|
58
|
|
|
$indexParams = [ |
59
|
|
|
'settings' => [ |
60
|
|
|
'analysis' => [ |
61
|
|
|
'analyzer' => [ |
62
|
|
|
'lw' => [ |
63
|
|
|
'type' => 'custom', |
64
|
|
|
'tokenizer' => 'keyword', |
65
|
|
|
'filter' => ['lowercase'], |
66
|
|
|
], |
67
|
|
|
], |
68
|
|
|
], |
69
|
|
|
], |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$index->create($indexParams, true); |
73
|
|
|
|
74
|
|
|
$mapping = new Mapping([ |
75
|
|
|
'name' => ['type' => 'text', 'analyzer' => 'lw'], |
76
|
|
|
]); |
77
|
|
|
$index->setMapping($mapping); |
78
|
|
|
|
79
|
|
|
$index->addDocuments([ |
80
|
|
|
new Document(1, ['name' => 'Basel-Stadt']), |
81
|
|
|
new Document(2, ['name' => 'New York']), |
82
|
|
|
new Document(3, ['name' => 'Baden']), |
83
|
|
|
new Document(4, ['name' => 'Baden Baden']), |
84
|
|
|
new Document(5, ['name' => 'New Orleans']), |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
$index->refresh(); |
88
|
|
|
|
89
|
|
|
$query = new Wildcard('name', 'ba*'); |
90
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
91
|
|
|
|
92
|
|
|
$this->assertEquals(3, $resultSet->count()); |
93
|
|
|
|
94
|
|
|
$query = new Wildcard('name', 'baden*'); |
95
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$this->assertEquals(2, $resultSet->count()); |
98
|
|
|
|
99
|
|
|
$query = new Wildcard('name', 'baden b*'); |
100
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
101
|
|
|
|
102
|
|
|
$this->assertEquals(1, $resultSet->count()); |
103
|
|
|
|
104
|
|
|
$query = new Wildcard('name', 'baden bas*'); |
105
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
106
|
|
|
|
107
|
|
|
$this->assertEquals(0, $resultSet->count()); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.