summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest
diff options
context:
space:
mode:
authorPierre Schmitz <pierre@archlinux.de>2015-12-17 09:15:42 +0100
committerPierre Schmitz <pierre@archlinux.de>2015-12-17 09:44:51 +0100
commita1789ddde42033f1b05cc4929491214ee6e79383 (patch)
tree63615735c4ddffaaabf2428946bb26f90899f7bf /vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest
parent9e06a62f265e3a2aaabecc598d4bc617e06fa32d (diff)
Update to MediaWiki 1.26.0
Diffstat (limited to 'vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest')
-rw-r--r--vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/CompletionTest.php140
-rw-r--r--vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/PhraseTest.php82
-rw-r--r--vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/TermTest.php105
3 files changed, 327 insertions, 0 deletions
diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/CompletionTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/CompletionTest.php
new file mode 100644
index 00000000..6120743c
--- /dev/null
+++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/CompletionTest.php
@@ -0,0 +1,140 @@
+<?php
+namespace Elastica\Test\Suggest;
+
+use Elastica\Document;
+use Elastica\Index;
+use Elastica\Query;
+use Elastica\Suggest\Completion;
+use Elastica\Test\Base as BaseTest;
+
+class CompletionTest extends BaseTest
+{
+ /**
+ * @return Index
+ */
+ protected function _getIndexForTest()
+ {
+ $index = $this->_createIndex();
+ $type = $index->getType('song');
+
+ $type->setMapping(array(
+ 'fieldName' => array(
+ 'type' => 'completion',
+ 'payloads' => true,
+ ),
+ ));
+
+ $type->addDocuments(array(
+ new Document(1, array(
+ 'fieldName' => array(
+ 'input' => array('Nevermind', 'Nirvana'),
+ 'output' => 'Nevermind - Nirvana',
+ 'payload' => array(
+ 'year' => 1991,
+ ),
+ ),
+ )),
+ new Document(2, array(
+ 'fieldName' => array(
+ 'input' => array('Bleach', 'Nirvana'),
+ 'output' => 'Bleach - Nirvana',
+ 'payload' => array(
+ 'year' => 1989,
+ ),
+ ),
+ )),
+ new Document(3, array(
+ 'fieldName' => array(
+ 'input' => array('Incesticide', 'Nirvana'),
+ 'output' => 'Incesticide - Nirvana',
+ 'payload' => array(
+ 'year' => 1992,
+ ),
+ ),
+ )),
+ ));
+
+ $index->refresh();
+
+ return $index;
+ }
+
+ /**
+ * @group unit
+ */
+ public function testToArray()
+ {
+ $suggest = new Completion('suggestName', 'fieldName');
+ $suggest->setText('foo');
+ $suggest->setSize(10);
+ $expected = array(
+ 'text' => 'foo',
+ 'completion' => array(
+ 'size' => 10,
+ 'field' => 'fieldName',
+ ),
+ );
+ $this->assertEquals($expected, $suggest->toArray());
+ }
+
+ /**
+ * @group functional
+ */
+ public function testSuggestWorks()
+ {
+ $suggest = new Completion('suggestName', 'fieldName');
+ $suggest->setText('Never');
+
+ $index = $this->_getIndexForTest();
+ $resultSet = $index->search(Query::create($suggest));
+
+ $this->assertTrue($resultSet->hasSuggests());
+
+ $suggests = $resultSet->getSuggests();
+ $options = $suggests['suggestName'][0]['options'];
+
+ $this->assertCount(1, $options);
+ $this->assertEquals('Nevermind - Nirvana', $options[0]['text']);
+ $this->assertEquals(1991, $options[0]['payload']['year']);
+ }
+
+ /**
+ * @group functional
+ */
+ public function testFuzzySuggestWorks()
+ {
+ $suggest = new Completion('suggestName', 'fieldName');
+ $suggest->setFuzzy(array('fuzziness' => 2));
+ $suggest->setText('Neavermint');
+
+ $index = $this->_getIndexForTest();
+ $resultSet = $index->search(Query::create($suggest));
+
+ $this->assertTrue($resultSet->hasSuggests());
+
+ $suggests = $resultSet->getSuggests();
+ $options = $suggests['suggestName'][0]['options'];
+
+ $this->assertCount(1, $options);
+ $this->assertEquals('Nevermind - Nirvana', $options[0]['text']);
+ }
+
+ /**
+ * @group unit
+ */
+ public function testSetFuzzy()
+ {
+ $suggest = new Completion('suggestName', 'fieldName');
+
+ $fuzzy = array(
+ 'unicode_aware' => true,
+ 'fuzziness' => 3,
+ );
+
+ $suggest->setFuzzy($fuzzy);
+
+ $this->assertEquals($fuzzy, $suggest->getParam('fuzzy'));
+
+ $this->assertInstanceOf('Elastica\\Suggest\\Completion', $suggest->setFuzzy($fuzzy));
+ }
+}
diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/PhraseTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/PhraseTest.php
new file mode 100644
index 00000000..9ce345d4
--- /dev/null
+++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/PhraseTest.php
@@ -0,0 +1,82 @@
+<?php
+namespace Elastica\Test\Suggest;
+
+use Elastica\Document;
+use Elastica\Index;
+use Elastica\Suggest;
+use Elastica\Suggest\CandidateGenerator\DirectGenerator;
+use Elastica\Suggest\Phrase;
+use Elastica\Test\Base as BaseTest;
+
+class PhraseTest extends BaseTest
+{
+ /**
+ * @return Index
+ */
+ protected function _getIndexForTest()
+ {
+ $index = $this->_createIndex();
+ $type = $index->getType('testSuggestType');
+ $type->addDocuments(array(
+ new Document(1, array('text' => 'Github is pretty cool')),
+ new Document(2, array('text' => 'Elasticsearch is bonsai cool')),
+ new Document(3, array('text' => 'This is a test phrase')),
+ new Document(4, array('text' => 'Another sentence for testing')),
+ new Document(5, array('text' => 'Some more words here')),
+ ));
+ $index->refresh();
+
+ return $index;
+ }
+
+ /**
+ * @group unit
+ */
+ public function testToArray()
+ {
+ $suggest = new Suggest();
+ $phraseSuggest = new Phrase('suggest1', 'text');
+ $phraseSuggest->setText('elasticsearch is bansai coor');
+ $phraseSuggest->setAnalyzer('simple');
+ $suggest->addSuggestion($phraseSuggest);
+ $suggest->setGlobalText('global!');
+
+ $expected = array(
+ 'suggest' => array(
+ 'text' => 'global!',
+ 'suggest1' => array(
+ 'text' => 'elasticsearch is bansai coor',
+ 'phrase' => array(
+ 'field' => 'text',
+ 'analyzer' => 'simple',
+ ),
+ ),
+ ),
+ );
+
+ $this->assertEquals($expected, $suggest->toArray());
+ }
+
+ /**
+ * @group functional
+ */
+ public function testPhraseSuggest()
+ {
+ $suggest = new Suggest();
+ $phraseSuggest = new Phrase('suggest1', 'text');
+ $phraseSuggest->setText('elasticsearch is bansai coor');
+ $phraseSuggest->setAnalyzer('simple')->setHighlight('<suggest>', '</suggest>')->setStupidBackoffSmoothing(0.4);
+ $phraseSuggest->addCandidateGenerator(new DirectGenerator('text'));
+ $suggest->addSuggestion($phraseSuggest);
+
+ $index = $this->_getIndexForTest();
+ $result = $index->search($suggest);
+ $suggests = $result->getSuggests();
+
+ // 3 suggestions should be returned: One in which both misspellings are corrected, and two in which only one misspelling is corrected.
+ $this->assertEquals(3, sizeof($suggests['suggest1'][0]['options']));
+
+ $this->assertEquals('elasticsearch is <suggest>bonsai cool</suggest>', $suggests['suggest1'][0]['options'][0]['highlighted']);
+ $this->assertEquals('elasticsearch is bonsai cool', $suggests['suggest1'][0]['options'][0]['text']);
+ }
+}
diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/TermTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/TermTest.php
new file mode 100644
index 00000000..f1250e6f
--- /dev/null
+++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Suggest/TermTest.php
@@ -0,0 +1,105 @@
+<?php
+namespace Elastica\Test\Suggest;
+
+use Elastica\Document;
+use Elastica\Index;
+use Elastica\Suggest;
+use Elastica\Suggest\Term;
+use Elastica\Test\Base as BaseTest;
+
+class TermTest extends BaseTest
+{
+ /**
+ * @return Index
+ */
+ protected function _getIndexForTest()
+ {
+ $index = $this->_createIndex();
+ $type = $index->getType('testSuggestType');
+ $type->addDocuments(array(
+ new Document(1, array('id' => 1, 'text' => 'GitHub')),
+ new Document(2, array('id' => 1, 'text' => 'Elastic')),
+ new Document(3, array('id' => 1, 'text' => 'Search')),
+ new Document(4, array('id' => 1, 'text' => 'Food')),
+ new Document(5, array('id' => 1, 'text' => 'Flood')),
+ new Document(6, array('id' => 1, 'text' => 'Folks')),
+ ));
+ $index->refresh();
+
+ return $index;
+ }
+
+ /**
+ * @group unit
+ */
+ public function testToArray()
+ {
+ $suggest = new Suggest();
+ $suggest1 = new Term('suggest1', '_all');
+ $suggest->addSuggestion($suggest1->setText('Foor'));
+ $suggest2 = new Term('suggest2', '_all');
+ $suggest->addSuggestion($suggest2->setText('Girhub'));
+
+ $expected = array(
+ 'suggest' => array(
+ 'suggest1' => array(
+ 'term' => array(
+ 'field' => '_all',
+ ),
+ 'text' => 'Foor',
+ ),
+ 'suggest2' => array(
+ 'term' => array(
+ 'field' => '_all',
+ ),
+ 'text' => 'Girhub',
+ ),
+ ),
+ );
+
+ $this->assertEquals($expected, $suggest->toArray());
+ }
+
+ /**
+ * @group functional
+ */
+ public function testSuggestResults()
+ {
+ $suggest = new Suggest();
+ $suggest1 = new Term('suggest1', '_all');
+ $suggest->addSuggestion($suggest1->setText('Foor seach'));
+ $suggest2 = new Term('suggest2', '_all');
+ $suggest->addSuggestion($suggest2->setText('Girhub'));
+
+ $index = $this->_getIndexForTest();
+ $result = $index->search($suggest);
+
+ $this->assertEquals(2, $result->countSuggests());
+
+ $suggests = $result->getSuggests();
+
+ // Ensure that two suggestion results are returned for suggest1
+ $this->assertEquals(2, sizeof($suggests['suggest1']));
+
+ $this->assertEquals('github', $suggests['suggest2'][0]['options'][0]['text']);
+ $this->assertEquals('food', $suggests['suggest1'][0]['options'][0]['text']);
+ }
+
+ /**
+ * @group functional
+ */
+ public function testSuggestNoResults()
+ {
+ $termSuggest = new Term('suggest1', '_all');
+ $termSuggest->setText('Foobar')->setSize(4);
+
+ $index = $this->_getIndexForTest();
+ $result = $index->search($termSuggest);
+
+ $this->assertEquals(1, $result->countSuggests());
+
+ // Assert that no suggestions were returned
+ $suggests = $result->getSuggests();
+ $this->assertEquals(0, sizeof($suggests['suggest1'][0]['options']));
+ }
+}