Completed
Pull Request — master (#1851)
by
unknown
02:27
created

ScriptFieldsTest::testQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica\Test\Script;
4
5
use Elastica\Mapping;
6
use Elastica\Query;
7
use Elastica\Script\Script;
8
use Elastica\Script\ScriptFields;
9
use Elastica\Test\Base as BaseTest;
10
11
/**
12
 * @internal
13
 */
14
class ScriptFieldsTest extends BaseTest
15
{
16
    /**
17
     * @group unit
18
     */
19
    public function testNewScriptFields(): void
20
    {
21
        $script = new Script('1 + 2');
22
23
        // addScript
24
        $scriptFields = new ScriptFields();
25
        $scriptFields->addScript('test', $script);
26
        $this->assertSame($scriptFields->getParam('test'), $script);
27
28
        // setScripts
29
        $scriptFields = new ScriptFields();
30
        $scriptFields->setScripts([
31
            'test' => $script,
32
        ]);
33
        $this->assertSame($scriptFields->getParam('test'), $script);
34
35
        // Constructor
36
        $scriptFields = new ScriptFields([
37
            'test' => $script,
38
        ]);
39
        $this->assertSame($scriptFields->getParam('test'), $script);
40
    }
41
42
    /**
43
     * @group unit
44
     */
45
    public function testSetScriptFields(): void
46
    {
47
        $query = new Query();
48
        $script = new Script('1 + 2');
49
50
        $scriptFields = new ScriptFields([
51
            'test' => $script,
52
        ]);
53
        $query->setScriptFields($scriptFields);
54
        $this->assertSame($query->getParam('script_fields'), $scriptFields);
55
56
        $query->setScriptFields([
57
            'test' => $script,
58
        ]);
59
        $this->assertSame($query->getParam('script_fields')->getParam('test'), $script);
60
    }
61
62
    /**
63
     * @group functional
64
     */
65
    public function testQuery(): void
66
    {
67
        $index = $this->_createIndex();
68
69
        $doc = $index->createDocument(1, ['firstname' => 'guschti', 'lastname' => 'ruflin']);
70
        $index->addDocument($doc);
71
        $index->refresh();
72
73
        $query = new Query();
74
        $script = new Script('1 + 2');
75
        $scriptFields = new ScriptFields([
76
            'test' => $script,
77
        ]);
78
        $query->setScriptFields($scriptFields);
79
80
        $resultSet = $index->search($query);
81
        $first = $resultSet->current()->getData();
82
83
        // 1 + 2
84
        $this->assertEquals(3, $first['test'][0]);
85
    }
86
87
    /**
88
     * @group functional
89
     */
90
    public function testScriptFieldWithJoin(): void
91
    {
92
        $client = $this->_getClient();
93
        $index = $client->getIndex('testscriptfieldwithjoin');
94
        $index->create([], true);
95
96
        $mapping = new Mapping([
97
            'text' => ['type' => 'keyword'],
98
            'name' => ['type' => 'keyword'],
99
            'my_join_field' => [
100
                'type' => 'join',
101
                'relations' => [
102
                    'question' => 'answer',
103
                ],
104
            ],
105
        ]);
106
107
        $index->setMapping($mapping);
108
        $index->refresh();
109
110
        $doc1 = $index->createDocument(1, [
111
            'text' => 'this is the 1st question',
112
            'my_join_field' => [
113
                'name' => 'question',
114
            ],
115
        ]);
116
        $doc2 = $index->createDocument(2, [
117
            'text' => 'this is the 2nd question',
118
            'my_join_field' => [
119
                'name' => 'question',
120
            ],
121
        ]);
122
        $index->addDocuments([$doc1, $doc2]);
123
124
        $doc3 = $index->createDocument(3, [
125
            'text' => 'this is an answer, the 1st',
126
            'name' => 'rico',
127
            'my_join_field' => [
128
                'name' => 'answer',
129
                'parent' => 1,
130
            ],
131
        ]);
132
        $doc4 = $index->createDocument(4, [
133
            'text' => 'this is an answer, the 2nd',
134
            'name' => 'fede',
135
            'my_join_field' => [
136
                'name' => 'answer',
137
                'parent' => 2,
138
            ],
139
        ]);
140
        $doc5 = $index->createDocument(5, [
141
            'text' => 'this is an answer, the 3rd',
142
            'name' => 'fede',
143
            'my_join_field' => [
144
                'name' => 'answer',
145
                'parent' => 2,
146
            ],
147
        ]);
148
149
        $this->_getClient()->addDocuments([$doc3, $doc4, $doc5], ['routing' => 1]);
150
        $index->refresh();
151
152
        $query = new Query();
153
        $script = new Script("doc['my_join_field#question']");
154
        $scriptFields = new ScriptFields([
155
            'text' => $script,
156
        ]);
157
        $query->setScriptFields($scriptFields);
158
        $resultSet = $index->search($query);
159
        $results = $resultSet->getResults();
160
161
        $this->assertEquals(1, ($results[0]->getHit())['fields']['text'][0]);
162
        $this->assertEquals(2, ($results[1]->getHit())['fields']['text'][0]);
163
    }
164
}
165