summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MultiMatchTest.php
blob: 78a8a8febc3dc9bd430a46a4a587b079037183bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php

namespace Elastica\Test\Query;

use Elastica\Document;
use Elastica\Index;
use Elastica\Query;
use Elastica\Query\MultiMatch;
use Elastica\Test\Base as BaseTest;
use Elastica\Type;
use Elastica\Type\Mapping;

class MultiMatchTest extends BaseTest
{
    private $index;
    private $multiMatch;

    private static $data = array(
        array('id' => 1, 'name' => 'Rodolfo', 'last_name' => 'Moraes',   'full_name' => 'Rodolfo Moraes'),
        array('id' => 2, 'name' => 'Tristan', 'last_name' => 'Maindron', 'full_name' => 'Tristan Maindron'),
        array('id' => 3, 'name' => 'Monique', 'last_name' => 'Maindron', 'full_name' => 'Monique Maindron'),
        array('id' => 4, 'name' => 'John',    'last_name' => 'not Doe',  'full_name' => 'John not Doe'),
    );

    protected function setUp()
    {
        $this->index = $this->_generateIndex();
        $this->multiMatch = new MultiMatch();
    }

    public function testMinimumShouldMatch()
    {
        $this->multiMatch->setQuery('Tristan Maindron');
        $this->multiMatch->setFields(array('full_name', 'name'));
        $this->multiMatch->setMinimumShouldMatch(2);
        $resultSet = $this->_getResults();

        $this->assertEquals(1, $resultSet->count());
    }

    public function testAndOperator()
    {
        $this->multiMatch->setQuery('Monique Maindron');
        $this->multiMatch->setFields(array('full_name', 'name'));
        $this->multiMatch->setOperator(MultiMatch::OPERATOR_AND);
        $resultSet = $this->_getResults();

        $this->assertEquals(1, $resultSet->count());
    }

    public function testType()
    {
        $this->multiMatch->setQuery('Trist');
        $this->multiMatch->setFields(array('full_name', 'name'));
        $this->multiMatch->setType(MultiMatch::TYPE_PHRASE_PREFIX);
        $resultSet = $this->_getResults();

        $this->assertEquals(1, $resultSet->count());
    }

    public function testFuzzy()
    {
        $this->multiMatch->setQuery('Tritsan'); // Mispell on purpose
        $this->multiMatch->setFields(array('full_name', 'name'));
        $this->multiMatch->setFuzziness(2);
        $resultSet = $this->_getResults();

        $this->assertEquals(1, $resultSet->count());

        $this->multiMatch->setQuery('Tritsan'); // Mispell on purpose
        $this->multiMatch->setFields(array('full_name', 'name'));
        $this->multiMatch->setFuzziness(0);
        $resultSet = $this->_getResults();

        $this->assertEquals(0, $resultSet->count());
    }

    public function testFuzzyWithOptions1()
    {
        // Here Elasticsearch will not accept mispells
        // on the first 6 letters.
        $this->multiMatch->setQuery('Tritsan'); // Mispell on purpose
        $this->multiMatch->setFields(array('full_name', 'name'));
        $this->multiMatch->setFuzziness(2);
        $this->multiMatch->setPrefixLength(6);
        $resultSet = $this->_getResults();

        $this->assertEquals(0, $resultSet->count());   
    }
    
    public function testFuzzyWithOptions2() {

        // Here with a 'M' search we should hit 'Moraes' first
        // and then stop because MaxExpansion = 1.
        // If MaxExpansion was set to 2, we could hit "Maindron" too.
        $this->multiMatch->setQuery('M');
        $this->multiMatch->setFields(array('name'));
        $this->multiMatch->setType(MultiMatch::TYPE_PHRASE_PREFIX);
        $this->multiMatch->setPrefixLength(0);
        $this->multiMatch->setMaxExpansions(1);
        $resultSet = $this->_getResults();

        $this->assertEquals(1, $resultSet->count());
    }

    public function testZeroTerm()
    {
        $this->multiMatch->setQuery('not'); // This is a stopword.
        $this->multiMatch->setFields(array('full_name', 'last_name'));
        $this->multiMatch->setZeroTermsQuery(MultiMatch::ZERO_TERM_NONE);
        $this->multiMatch->setAnalyzer('stops');
        $resultSet = $this->_getResults();

        $this->assertEquals(0, $resultSet->count());

        $this->multiMatch->setZeroTermsQuery(MultiMatch::ZERO_TERM_ALL);
        $resultSet = $this->_getResults();

        $this->assertEquals(4, $resultSet->count());
    }

    public function testBaseMultiMatch()
    {
        $this->multiMatch->setQuery('Rodolfo');
        $this->multiMatch->setFields(array('name', 'last_name'));
        $resultSet = $this->_getResults();

        $this->assertEquals(1, $resultSet->count());

        $this->multiMatch->setQuery('Moraes');
        $this->multiMatch->setFields(array('name', 'last_name'));
        $resultSet = $this->_getResults();

        $this->assertEquals(1, $resultSet->count());
    }

    /**
     * Executes the query with the current multimatch.
     */
    private function _getResults()
    {
        return $this->index->search(new Query($this->multiMatch));
    }

    /**
     * Builds an index for testing.
     */
    private function _generateIndex()
    {
        $client = $this->_getClient();
        $index = $client->getIndex('test');

        $index->create(array(
            'analysis' => array(
                'analyzer' => array(
                    'noStops' => array(
                        'type'      => 'standard',
                        'stopwords' => '_none_'
                    ),
                    'stops' => array(
                        'type'      => 'standard',
                        'stopwords' => array('not')
                    ),
                ),
            )
        ), true);

        $type = $index->getType('test');

        $mapping = new Mapping($type, array(
            'name'      => array('type' => 'string', 'store' => 'no', 'analyzer' => 'noStops'),
            'last_name' => array('type' => 'string', 'store' => 'no', 'analyzer' => 'noStops'),
            'full_name' => array('type' => 'string', 'store' => 'no', 'analyzer' => 'noStops'),
        ));

        $type->setMapping($mapping);

        foreach (self::$data as $key => $docData) {
            $type->addDocument(new Document($key, $docData));
        }

        // Refresh index
        $index->refresh();

        return $index;
    }
}