diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2015-12-17 09:15:42 +0100 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2015-12-17 09:44:51 +0100 |
commit | a1789ddde42033f1b05cc4929491214ee6e79383 (patch) | |
tree | 63615735c4ddffaaabf2428946bb26f90899f7bf /vendor/ruflin/elastica/test/lib/Elastica/Test/Query | |
parent | 9e06a62f265e3a2aaabecc598d4bc617e06fa32d (diff) |
Update to MediaWiki 1.26.0
Diffstat (limited to 'vendor/ruflin/elastica/test/lib/Elastica/Test/Query')
32 files changed, 4199 insertions, 0 deletions
diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BoolQueryTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BoolQueryTest.php new file mode 100644 index 00000000..211d0c23 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BoolQueryTest.php @@ -0,0 +1,171 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Index; +use Elastica\Query\BoolQuery; +use Elastica\Query\Ids; +use Elastica\Query\Term; +use Elastica\Test\Base as BaseTest; +use Elastica\Type; + +class BoolQueryTest extends BaseTest +{ + /** + * @group unit + */ + public function testToArray() + { + $query = new BoolQuery(); + + $idsQuery1 = new Ids(); + $idsQuery1->setIds(1); + + $idsQuery2 = new Ids(); + $idsQuery2->setIds(2); + + $idsQuery3 = new Ids(); + $idsQuery3->setIds(3); + + $boost = 1.2; + $minMatch = 2; + + $query->setBoost($boost); + $query->setMinimumNumberShouldMatch($minMatch); + $query->addMust($idsQuery1); + $query->addMustNot($idsQuery2); + $query->addShould($idsQuery3->toArray()); + + $expectedArray = array( + 'bool' => array( + 'must' => array($idsQuery1->toArray()), + 'should' => array($idsQuery3->toArray()), + 'minimum_number_should_match' => $minMatch, + 'must_not' => array($idsQuery2->toArray()), + 'boost' => $boost, + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * Test to resolve the following issue. + * + * @link https://groups.google.com/forum/?fromgroups#!topic/elastica-php-client/zK_W_hClfvU + * + * @group unit + */ + public function testToArrayStructure() + { + $boolQuery = new BoolQuery(); + + $term1 = new Term(); + $term1->setParam('interests', 84); + + $term2 = new Term(); + $term2->setParam('interests', 92); + + $boolQuery->addShould($term1)->addShould($term2); + + $jsonString = '{"bool":{"should":[{"term":{"interests":84}},{"term":{"interests":92}}]}}'; + $this->assertEquals($jsonString, json_encode($boolQuery->toArray())); + } + + /** + * @group functional + */ + public function testSearch() + { + $client = $this->_getClient(); + $index = new Index($client, 'test'); + $index->create(array(), true); + + $type = new Type($index, 'helloworld'); + + $doc = new Document(1, array('id' => 1, 'email' => 'hans@test.com', 'username' => 'hans', 'test' => array('2', '3', '5'))); + $type->addDocument($doc); + $doc = new Document(2, array('id' => 2, 'email' => 'emil@test.com', 'username' => 'emil', 'test' => array('1', '3', '6'))); + $type->addDocument($doc); + $doc = new Document(3, array('id' => 3, 'email' => 'ruth@test.com', 'username' => 'ruth', 'test' => array('2', '3', '7'))); + $type->addDocument($doc); + + // Refresh index + $index->refresh(); + + $boolQuery = new BoolQuery(); + $termQuery1 = new Term(array('test' => '2')); + $boolQuery->addMust($termQuery1); + $resultSet = $type->search($boolQuery); + + $this->assertEquals(2, $resultSet->count()); + + $termQuery2 = new Term(array('test' => '5')); + $boolQuery->addMust($termQuery2); + $resultSet = $type->search($boolQuery); + + $this->assertEquals(1, $resultSet->count()); + + $termQuery3 = new Term(array('username' => 'hans')); + $boolQuery->addMust($termQuery3); + $resultSet = $type->search($boolQuery); + + $this->assertEquals(1, $resultSet->count()); + + $termQuery4 = new Term(array('username' => 'emil')); + $boolQuery->addMust($termQuery4); + $resultSet = $type->search($boolQuery); + + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + public function testEmptyBoolQuery() + { + $index = $this->_createIndex(); + $type = new Type($index, 'test'); + + $docNumber = 3; + for ($i = 0; $i < $docNumber; $i++) { + $doc = new Document($i, array('email' => 'test@test.com')); + $type->addDocument($doc); + } + + $index->refresh(); + + $boolQuery = new BoolQuery(); + + $resultSet = $type->search($boolQuery); + + $this->assertEquals($resultSet->count(), $docNumber); + } + + /** + * @group functional + */ + public function testOldObject() + { + if (version_compare(phpversion(), 7, '>=')) { + self::markTestSkipped('These objects are not supported in PHP 7'); + } + + $index = $this->_createIndex(); + $type = new Type($index, 'test'); + + $docNumber = 3; + for ($i = 0; $i < $docNumber; $i++) { + $doc = new Document($i, array('email' => 'test@test.com')); + $type->addDocument($doc); + } + + $index->refresh(); + + $boolQuery = new \Elastica\Query\Bool(); + + $resultSet = $type->search($boolQuery); + + $this->assertEquals($resultSet->count(), $docNumber); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BoostingTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BoostingTest.php new file mode 100644 index 00000000..8133fd37 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BoostingTest.php @@ -0,0 +1,89 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query\Boosting; +use Elastica\Query\Term; +use Elastica\Test\Base as BaseTest; + +class BoostingTest extends BaseTest +{ + /** + * @var array + */ + protected $sampleData = array( + array('name' => 'Vital Lama', 'price' => 5.2), + array('name' => 'Vital Match', 'price' => 2.1), + array('name' => 'Mercury Vital', 'price' => 7.5), + array('name' => 'Fist Mercury', 'price' => 3.8), + array('name' => 'Lama Vital 2nd', 'price' => 3.2), + ); + + protected function _getTestIndex() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + $type->setMapping(array( + 'name' => array('type' => 'string', 'index' => 'analyzed'), + 'price' => array('type' => 'float'), + )); + $docs = array(); + foreach ($this->sampleData as $key => $value) { + $docs[] = new Document($key, $value); + } + $type->addDocuments($docs); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $keyword = 'vital'; + $negativeKeyword = 'Mercury'; + + $query = new Boosting(); + $positiveQuery = new Term(array('name' => $keyword)); + $negativeQuery = new Term(array('name' => $negativeKeyword)); + $query->setPositiveQuery($positiveQuery); + $query->setNegativeQuery($negativeQuery); + $query->setNegativeBoost(0.3); + + $expected = array( + 'boosting' => array( + 'positive' => $positiveQuery->toArray(), + 'negative' => $negativeQuery->toArray(), + 'negative_boost' => 0.3, + ), + ); + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group functional + */ + public function testNegativeBoost() + { + $keyword = 'vital'; + $negativeKeyword = 'mercury'; + + $query = new Boosting(); + $positiveQuery = new Term(array('name' => $keyword)); + $negativeQuery = new Term(array('name' => $negativeKeyword)); + $query->setPositiveQuery($positiveQuery); + $query->setNegativeQuery($negativeQuery); + $query->setNegativeBoost(0.2); + + $response = $this->_getTestIndex()->search($query); + $results = $response->getResults(); + + $this->assertEquals($response->getTotalHits(), 4); + + $lastResult = $results[3]->getData(); + $this->assertEquals($lastResult['name'], $this->sampleData[2]['name']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BuilderTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BuilderTest.php new file mode 100644 index 00000000..a96e8b3a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/BuilderTest.php @@ -0,0 +1,273 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Query\Builder; +use Elastica\Test\Base as BaseTest; + +class BuilderTest extends BaseTest +{ + /** + * @group unit + * @covers \Elastica\Query\Builder::factory + * @covers \Elastica\Query\Builder::__construct + */ + public function testFactory() + { + $this->assertInstanceOf( + 'Elastica\Query\Builder', + Builder::factory('some string') + ); + } + + public function getQueryData() + { + return array( + array('allowLeadingWildcard', false, '{"allow_leading_wildcard":"false"}'), + array('allowLeadingWildcard', true, '{"allow_leading_wildcard":"true"}'), + array('analyzeWildcard', false, '{"analyze_wildcard":"false"}'), + array('analyzeWildcard', true, '{"analyze_wildcard":"true"}'), + array('analyzer', 'someAnalyzer', '{"analyzer":"someAnalyzer"}'), + array('autoGeneratePhraseQueries', true, '{"auto_generate_phrase_queries":"true"}'), + array('autoGeneratePhraseQueries', false, '{"auto_generate_phrase_queries":"false"}'), + array('boost', 2, '{"boost":"2"}'), + array('boost', 4.2, '{"boost":"4.2"}'), + array('defaultField', 'fieldName', '{"default_field":"fieldName"}'), + array('defaultOperator', 'OR', '{"default_operator":"OR"}'), + array('defaultOperator', 'AND', '{"default_operator":"AND"}'), + array('enablePositionIncrements', true, '{"enable_position_increments":"true"}'), + array('enablePositionIncrements', false, '{"enable_position_increments":"false"}'), + array('explain', true, '{"explain":"true"}'), + array('explain', false, '{"explain":"false"}'), + array('from', 42, '{"from":"42"}'), + array('fuzzyMinSim', 4.2, '{"fuzzy_min_sim":"4.2"}'), + array('fuzzyPrefixLength', 2, '{"fuzzy_prefix_length":"2"}'), + array('gt', 10, '{"gt":"10"}'), + array('gte', 11, '{"gte":"11"}'), + array('lowercaseExpandedTerms', true, '{"lowercase_expanded_terms":"true"}'), + array('lt', 10, '{"lt":"10"}'), + array('lte', 11, '{"lte":"11"}'), + array('minimumNumberShouldMatch', 21, '{"minimum_number_should_match":"21"}'), + array('phraseSlop', 6, '{"phrase_slop":"6"}'), + array('size', 7, '{"size":"7"}'), + array('tieBreakerMultiplier', 7, '{"tie_breaker_multiplier":"7"}'), + array('matchAll', 1.1, '{"match_all":{"boost":"1.1"}}'), + array('fields', array('age', 'sex', 'location'), '{"fields":["age","sex","location"]}'), + ); + } + + /** + * @group unit + * @dataProvider getQueryData + * @covers \Elastica\Query\Builder::__toString + * @covers \Elastica\Query\Builder::allowLeadingWildcard + * @covers \Elastica\Query\Builder::analyzeWildcard + * @covers \Elastica\Query\Builder::analyzer + * @covers \Elastica\Query\Builder::autoGeneratePhraseQueries + * @covers \Elastica\Query\Builder::boost + * @covers \Elastica\Query\Builder::defaultField + * @covers \Elastica\Query\Builder::defaultOperator + * @covers \Elastica\Query\Builder::enablePositionIncrements + * @covers \Elastica\Query\Builder::explain + * @covers \Elastica\Query\Builder::from + * @covers \Elastica\Query\Builder::fuzzyMinSim + * @covers \Elastica\Query\Builder::fuzzyPrefixLength + * @covers \Elastica\Query\Builder::gt + * @covers \Elastica\Query\Builder::gte + * @covers \Elastica\Query\Builder::lowercaseExpandedTerms + * @covers \Elastica\Query\Builder::lt + * @covers \Elastica\Query\Builder::lte + * @covers \Elastica\Query\Builder::minimumNumberShouldMatch + * @covers \Elastica\Query\Builder::phraseSlop + * @covers \Elastica\Query\Builder::size + * @covers \Elastica\Query\Builder::tieBreakerMultiplier + * @covers \Elastica\Query\Builder::matchAll + * @covers \Elastica\Query\Builder::fields + */ + public function testAllowLeadingWildcard($method, $argument, $result) + { + $builder = new Builder(); + $this->assertSame($builder, $builder->$method($argument)); + $this->assertSame($result, (string) $builder); + } + + public function getQueryTypes() + { + return array( + array('bool', 'bool'), + array('constantScore', 'constant_score'), + array('disMax', 'dis_max'), + array('facets', 'facets'), + array('filter', 'filter'), + array('filteredQuery', 'filtered'), + array('must', 'must'), + array('mustNot', 'must_not'), + array('prefix', 'prefix'), + array('query', 'query'), + array('queryString', 'query_string'), + array('range', 'range'), + array('should', 'should'), + array('sort', 'sort'), + array('term', 'term'), + array('textPhrase', 'text_phrase'), + array('wildcard', 'wildcard'), + ); + } + + /** + * @group unit + * @dataProvider getQueryTypes + * @covers \Elastica\Query\Builder::fieldClose + * @covers \Elastica\Query\Builder::close + * @covers \Elastica\Query\Builder::bool + * @covers \Elastica\Query\Builder::boolClose + * @covers \Elastica\Query\Builder::constantScore + * @covers \Elastica\Query\Builder::constantScoreClose + * @covers \Elastica\Query\Builder::disMax + * @covers \Elastica\Query\Builder::disMaxClose + * @covers \Elastica\Query\Builder::facets + * @covers \Elastica\Query\Builder::facetsClose + * @covers \Elastica\Query\Builder::filter + * @covers \Elastica\Query\Builder::filterClose + * @covers \Elastica\Query\Builder::filteredQuery + * @covers \Elastica\Query\Builder::filteredQueryClose + * @covers \Elastica\Query\Builder::must + * @covers \Elastica\Query\Builder::mustClose + * @covers \Elastica\Query\Builder::mustNot + * @covers \Elastica\Query\Builder::mustNotClose + * @covers \Elastica\Query\Builder::prefix + * @covers \Elastica\Query\Builder::prefixClose + * @covers \Elastica\Query\Builder::query + * @covers \Elastica\Query\Builder::queryClose + * @covers \Elastica\Query\Builder::queryString + * @covers \Elastica\Query\Builder::queryStringClose + * @covers \Elastica\Query\Builder::range + * @covers \Elastica\Query\Builder::rangeClose + * @covers \Elastica\Query\Builder::should + * @covers \Elastica\Query\Builder::shouldClose + * @covers \Elastica\Query\Builder::sort + * @covers \Elastica\Query\Builder::sortClose + * @covers \Elastica\Query\Builder::term + * @covers \Elastica\Query\Builder::termClose + * @covers \Elastica\Query\Builder::textPhrase + * @covers \Elastica\Query\Builder::textPhraseClose + * @covers \Elastica\Query\Builder::wildcard + * @covers \Elastica\Query\Builder::wildcardClose + */ + public function testQueryTypes($method, $queryType) + { + $builder = new Builder(); + $this->assertSame($builder, $builder->$method()); // open + $this->assertSame($builder, $builder->{$method.'Close'}()); // close + $this->assertSame('{"'.$queryType.'":{}}', (string) $builder); + } + + /** + * @group unit + * @covers \Elastica\Query\Builder::fieldOpen + * @covers \Elastica\Query\Builder::fieldClose + * @covers \Elastica\Query\Builder::open + * @covers \Elastica\Query\Builder::close + */ + public function testFieldOpenAndClose() + { + $builder = new Builder(); + $this->assertSame($builder, $builder->fieldOpen('someField')); + $this->assertSame($builder, $builder->fieldClose()); + $this->assertSame('{"someField":{}}', (string) $builder); + } + + /** + * @group unit + * @covers \Elastica\Query\Builder::sortField + */ + public function testSortField() + { + $builder = new Builder(); + $this->assertSame($builder, $builder->sortField('name', true)); + $this->assertSame('{"sort":{"name":{"reverse":"true"}}}', (string) $builder); + } + + /** + * @group unit + * @covers \Elastica\Query\Builder::sortFields + */ + public function testSortFields() + { + $builder = new Builder(); + $this->assertSame($builder, $builder->sortFields(array('field1' => 'asc', 'field2' => 'desc', 'field3' => 'asc'))); + $this->assertSame('{"sort":[{"field1":"asc"},{"field2":"desc"},{"field3":"asc"}]}', (string) $builder); + } + + /** + * @group unit + * @covers \Elastica\Query\Builder::queries + */ + public function testQueries() + { + $queries = array(); + + $builder = new Builder(); + $b1 = clone $builder; + $b2 = clone $builder; + + $queries[] = $b1->term()->field('age', 34)->termClose(); + $queries[] = $b2->term()->field('name', 'christer')->termClose(); + + $this->assertSame($builder, $builder->queries($queries)); + $this->assertSame('{"queries":[{"term":{"age":"34"}},{"term":{"name":"christer"}}]}', (string) $builder); + } + + public function getFieldData() + { + return array( + array('name', 'value', '{"name":"value"}'), + array('name', true, '{"name":"true"}'), + array('name', false, '{"name":"false"}'), + array('name', array(1, 2, 3), '{"name":["1","2","3"]}'), + array('name', array('foo', 'bar', 'baz'), '{"name":["foo","bar","baz"]}'), + ); + } + + /** + * @group unit + * @dataProvider getFieldData + * @covers \Elastica\Query\Builder::field + */ + public function testField($name, $value, $result) + { + $builder = new Builder(); + $this->assertSame($builder, $builder->field($name, $value)); + $this->assertSame($result, (string) $builder); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + * @expectedExceptionMessage The produced query is not a valid json string : "{{}" + * @covers \Elastica\Query\Builder::toArray + */ + public function testToArrayWithInvalidData() + { + $builder = new Builder(); + $builder->open('foo'); + $builder->toArray(); + } + + /** + * @group unit + * @covers \Elastica\Query\Builder::toArray + */ + public function testToArray() + { + $builder = new Builder(); + $builder->query()->term()->field('category.id', array(1, 2, 3))->termClose()->queryClose(); + $expected = array( + 'query' => array( + 'term' => array( + 'category.id' => array(1, 2, 3), + ), + ), + ); + $this->assertEquals($expected, $builder->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/CommonTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/CommonTest.php new file mode 100644 index 00000000..016213a8 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/CommonTest.php @@ -0,0 +1,63 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query\Common; +use Elastica\Test\Base as BaseTest; + +class CommonTest extends BaseTest +{ + /** + * @group unit + */ + public function testToArray() + { + $query = new Common('body', 'test query', .001); + $query->setLowFrequencyOperator(Common::OPERATOR_AND); + + $expected = array( + 'common' => array( + 'body' => array( + 'query' => 'test query', + 'cutoff_frequency' => .001, + 'low_freq_operator' => 'and', + ), + ), + ); + + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group functional + */ + public function testQuery() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $docs = array( + new Document(1, array('body' => 'foo baz')), + new Document(2, array('body' => 'foo bar baz')), + new Document(3, array('body' => 'foo bar baz bat')), + ); + //add documents to create common terms + for ($i = 4; $i < 24; $i++) { + $docs[] = new Document($i, array('body' => 'foo bar')); + } + $type->addDocuments($docs); + $index->refresh(); + + $query = new Common('body', 'foo bar baz bat', .5); + $results = $type->search($query)->getResults(); + + //documents containing only common words should not be returned + $this->assertEquals(3, sizeof($results)); + + $query->setMinimumShouldMatch(2); + $results = $type->search($query); + + //only the document containing both low frequency terms should match + $this->assertEquals(1, $results->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/ConstantScoreTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/ConstantScoreTest.php new file mode 100644 index 00000000..a3a213b9 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/ConstantScoreTest.php @@ -0,0 +1,162 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Filter\Ids; +use Elastica\Filter\Term; +use Elastica\Index; +use Elastica\Query\ConstantScore; +use Elastica\Query\MatchAll; +use Elastica\Test\Base as BaseTest; + +class ConstantScoreTest extends BaseTest +{ + public function dataProviderSampleQueries() + { + return array( + array( + new Term(array('foo', 'bar')), + array( + 'constant_score' => array( + 'filter' => array( + 'term' => array( + 'foo', + 'bar', + ), + ), + ), + ), + ), + array( + array( + 'and' => array( + array( + 'query' => array( + 'query_string' => array( + 'query' => 'foo', + 'default_field' => 'something', + ), + ), + ), + array( + 'query' => array( + 'query_string' => array( + 'query' => 'bar', + 'default_field' => 'something', + ), + ), + ), + ), + ), + '{"constant_score":{"filter":{"and":[{"query":{"query_string":{"query":"foo","default_field":"something"}}},{"query":{"query_string":{"query":"bar","default_field":"something"}}}]}}}', + ), + ); + } + /** + * @group unit + * @dataProvider dataProviderSampleQueries + */ + public function testSimple($filter, $expected) + { + $query = new ConstantScore(); + $query->setFilter($filter); + if (is_string($expected)) { + $expected = json_decode($expected, true); + } + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group unit + */ + public function testToArray() + { + $query = new ConstantScore(); + + $boost = 1.2; + $filter = new Ids(); + $filter->setIds(array(1)); + + $query->setFilter($filter); + $query->setBoost($boost); + + $expectedArray = array( + 'constant_score' => array( + 'filter' => $filter->toArray(), + 'boost' => $boost, + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group unit + */ + public function testConstruct() + { + $filter = new Ids(); + $filter->setIds(array(1)); + + $query = new ConstantScore($filter); + + $expectedArray = array( + 'constant_score' => array( + 'filter' => $filter->toArray(), + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group functional + */ + public function testQuery() + { + $index = $this->_createIndex(); + + $type = $index->getType('constant_score'); + $type->addDocuments(array( + new Document(1, array('id' => 1, 'email' => 'hans@test.com', 'username' => 'hans')), + new Document(2, array('id' => 2, 'email' => 'emil@test.com', 'username' => 'emil')), + new Document(3, array('id' => 3, 'email' => 'ruth@test.com', 'username' => 'ruth')), + )); + + // Refresh index + $index->refresh(); + + $boost = 1.3; + $query_match = new MatchAll(); + + $query = new ConstantScore(); + $query->setQuery($query_match); + $query->setBoost($boost); + + $expectedArray = array( + 'constant_score' => array( + 'query' => $query_match->toArray(), + 'boost' => $boost, + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + $resultSet = $type->search($query); + + $results = $resultSet->getResults(); + + $this->assertEquals($resultSet->count(), 3); + $this->assertEquals($results[1]->getScore(), 1); + } + + /** + * @group unit + */ + public function testConstructEmpty() + { + $query = new ConstantScore(); + $expectedArray = array('constant_score' => array()); + + $this->assertEquals($expectedArray, $query->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/DisMaxTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/DisMaxTest.php new file mode 100644 index 00000000..2c128018 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/DisMaxTest.php @@ -0,0 +1,84 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query\DisMax; +use Elastica\Query\Ids; +use Elastica\Query\QueryString; +use Elastica\Test\Base as BaseTest; + +class DisMaxTest extends BaseTest +{ + /** + * @group unit + */ + public function testToArray() + { + $query = new DisMax(); + + $idsQuery1 = new Ids(); + $idsQuery1->setIds(1); + + $idsQuery2 = new Ids(); + $idsQuery2->setIds(2); + + $idsQuery3 = new Ids(); + $idsQuery3->setIds(3); + + $boost = 1.2; + $tieBreaker = 2; + + $query->setBoost($boost); + $query->setTieBreaker($tieBreaker); + $query->addQuery($idsQuery1); + $query->addQuery($idsQuery2); + $query->addQuery($idsQuery3->toArray()); + + $expectedArray = array( + 'dis_max' => array( + 'tie_breaker' => $tieBreaker, + 'boost' => $boost, + 'queries' => array( + $idsQuery1->toArray(), + $idsQuery2->toArray(), + $idsQuery3->toArray(), + ), + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group functional + */ + public function testQuery() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'Baden')), + new Document(4, array('name' => 'Baden Baden')), + )); + + $index->refresh(); + + $queryString1 = new QueryString('Bade*'); + $queryString2 = new QueryString('Base*'); + + $boost = 1.2; + $tieBreaker = 2; + + $query = new DisMax(); + $query->setBoost($boost); + $query->setTieBreaker($tieBreaker); + $query->addQuery($queryString1); + $query->addQuery($queryString2); + $resultSet = $type->search($query); + + $this->assertEquals(3, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/EscapeStringTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/EscapeStringTest.php new file mode 100644 index 00000000..536a7bb3 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/EscapeStringTest.php @@ -0,0 +1,36 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Index; +use Elastica\Query\QueryString; +use Elastica\Test\Base as BaseTest; +use Elastica\Type; +use Elastica\Util; + +class EscapeStringTest extends BaseTest +{ + /** + * @group functional + */ + public function testSearch() + { + $index = $this->_createIndex(); + $index->getSettings()->setNumberOfReplicas(0); + + $type = new Type($index, 'helloworld'); + + $doc = new Document(1, array( + 'email' => 'test@test.com', 'username' => 'test 7/6 123', 'test' => array('2', '3', '5'), ) + ); + $type->addDocument($doc); + + // Refresh index + $index->refresh(); + + $queryString = new QueryString(Util::escapeTerm('test 7/6')); + $resultSet = $type->search($queryString); + + $this->assertEquals(1, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FilteredTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FilteredTest.php new file mode 100644 index 00000000..c4bea6f2 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FilteredTest.php @@ -0,0 +1,124 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Filter\Term; +use Elastica\Query\Filtered; +use Elastica\Query\QueryString; +use Elastica\Test\Base as BaseTest; + +class FilteredTest extends BaseTest +{ + /** + * @group functional + */ + public function testFilteredSearch() + { + $index = $this->_createIndex(); + $type = $index->getType('helloworld'); + + $type->addDocuments(array( + new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'hanswurst', 'test' => array('2', '3', '5'))), + new Document(2, array('id' => 2, 'email' => 'test@test.com', 'username' => 'peter', 'test' => array('2', '3', '5'))), + )); + + $queryString = new QueryString('test*'); + + $filter1 = new Term(); + $filter1->setTerm('username', 'peter'); + + $filter2 = new Term(); + $filter2->setTerm('username', 'qwerqwer'); + + $query1 = new Filtered($queryString, $filter1); + $query2 = new Filtered($queryString, $filter2); + $index->refresh(); + + $resultSet = $type->search($queryString); + $this->assertEquals(2, $resultSet->count()); + + $resultSet = $type->search($query1); + $this->assertEquals(1, $resultSet->count()); + + $resultSet = $type->search($query2); + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group unit + */ + public function testFilteredGetter() + { + $queryString = new QueryString('test*'); + + $filter1 = new Term(); + $filter1->setTerm('username', 'peter'); + + $filter2 = new Term(); + $filter2->setTerm('username', 'qwerqwer'); + + $query1 = new Filtered($queryString, $filter1); + $query2 = new Filtered($queryString, $filter2); + + $this->assertEquals($query1->getQuery(), $queryString); + $this->assertEquals($query2->getQuery(), $queryString); + $this->assertEquals($query1->getFilter(), $filter1); + $this->assertEquals($query2->getFilter(), $filter2); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testFilteredWithoutArgumentsShouldRaiseException() + { + $query = new Filtered(); + $query->toArray(); + } + + /** + * @group functional + */ + public function testFilteredSearchNoQuery() + { + $index = $this->_createIndex(); + $type = $index->getType('helloworld'); + + $type->addDocuments(array( + new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'hanswurst', 'test' => array('2', '3', '5'))), + new Document(2, array('id' => 2, 'email' => 'test@test.com', 'username' => 'peter', 'test' => array('2', '3', '5'))), + )); + + $index->refresh(); + + $filter = new Term(); + $filter->setTerm('username', 'peter'); + + $query = new Filtered(null, $filter); + + $resultSet = $type->search($query); + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testFilteredSearchNoFilter() + { + $index = $this->_createIndex(); + $type = $index->getType('helloworld'); + + $doc = new Document(1, array('id' => 1, 'email' => 'test@test.com', 'username' => 'hanswurst', 'test' => array('2', '3', '5'))); + $type->addDocument($doc); + $doc = new Document(2, array('id' => 2, 'email' => 'test@test.com', 'username' => 'peter', 'test' => array('2', '3', '5'))); + $type->addDocument($doc); + + $queryString = new QueryString('hans*'); + + $query = new Filtered($queryString); + $index->refresh(); + + $resultSet = $type->search($query); + $this->assertEquals(1, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FunctionScoreTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FunctionScoreTest.php new file mode 100644 index 00000000..25600034 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FunctionScoreTest.php @@ -0,0 +1,324 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Filter\Term; +use Elastica\Query\FunctionScore; +use Elastica\Query\MatchAll; +use Elastica\Script; +use Elastica\Test\Base as BaseTest; + +class FunctionScoreTest extends BaseTest +{ + protected $locationOrigin = '32.804654, -117.242594'; + + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(array( + 'name' => array('type' => 'string', 'index' => 'not_analyzed'), + 'location' => array('type' => 'geo_point'), + 'price' => array('type' => 'float'), + )); + + $type->addDocuments(array( + new Document(1, array( + 'name' => "Mr. Frostie's", + 'location' => array('lat' => 32.799605, 'lon' => -117.243027), + 'price' => 4.5, + )), + new Document(2, array( + 'name' => "Miller's Field", + 'location' => array('lat' => 32.795964, 'lon' => -117.255028), + 'price' => 9.5, + )), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $priceOrigin = 0; + $locationScale = '2mi'; + $priceScale = 9.25; + $query = new FunctionScore(); + $childQuery = new MatchAll(); + $query->setQuery($childQuery); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'location', $this->locationOrigin, $locationScale); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', $priceOrigin, $priceScale); + $expected = array( + 'function_score' => array( + 'query' => $childQuery->toArray(), + 'functions' => array( + array( + 'gauss' => array( + 'location' => array( + 'origin' => $this->locationOrigin, + 'scale' => $locationScale, + ), + ), + ), + array( + 'gauss' => array( + 'price' => array( + 'origin' => $priceOrigin, + 'scale' => $priceScale, + ), + ), + ), + ), + ), + ); + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group unit + */ + public function testDecayWeight() + { + $priceOrigin = 0; + $locationScale = '2mi'; + $priceScale = 9.25; + $query = new FunctionScore(); + $childQuery = new MatchAll(); + $query->setQuery($childQuery); + $query->addDecayFunction( + FunctionScore::DECAY_GAUSS, + 'location', + $this->locationOrigin, + $locationScale, + null, + null, + .5 + ); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', $priceOrigin, $priceScale, null, null, 2); + $expected = array( + 'function_score' => array( + 'query' => $childQuery->toArray(), + 'functions' => array( + array( + 'gauss' => array( + 'location' => array( + 'origin' => $this->locationOrigin, + 'scale' => $locationScale, + ), + ), + 'weight' => .5, + ), + array( + 'gauss' => array( + 'price' => array( + 'origin' => $priceOrigin, + 'scale' => $priceScale, + ), + ), + 'weight' => 2, + ), + ), + ), + ); + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group functional + */ + public function testGauss() + { + $query = new FunctionScore(); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'location', $this->locationOrigin, '4mi'); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', 0, 10); + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + // the document with the closest location and lowest price should be scored highest + $result0 = $results[0]->getData(); + $this->assertEquals("Mr. Frostie's", $result0['name']); + } + + /** + * @group functional + */ + public function testWeight() + { + $filter = new Term(array('price' => 4.5)); + $query = new FunctionScore(); + $query->addBoostFactorFunction(5.0, $filter); + $expected = array( + 'function_score' => array( + 'functions' => array( + array( + 'weight' => 5.0, + 'filter' => array( + 'term' => array( + 'price' => 4.5, + ), + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $query->toArray()); + + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + // the document with price = 4.5 should be scored highest + $result0 = $results[0]->getData(); + $this->assertEquals("Mr. Frostie's", $result0['name']); + } + + /** + * @group functional + */ + public function testRandomScore() + { + $filter = new Term(array('price' => 4.5)); + $query = new FunctionScore(); + $query->addRandomScoreFunction(2, $filter); + $expected = array( + 'function_score' => array( + 'functions' => array( + array( + 'random_score' => array( + 'seed' => 2, + ), + 'filter' => array( + 'term' => array( + 'price' => 4.5, + ), + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $query->toArray()); + + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + // the document with the random score should have a score > 1, means it is the first result + $result0 = $results[0]->getData(); + + $this->assertEquals("Miller's Field", $result0['name']); + } + + /** + * @group unit + */ + public function testRandomScoreWeight() + { + $filter = new Term(array('price' => 4.5)); + $query = new FunctionScore(); + $query->addRandomScoreFunction(2, $filter, 2); + $expected = array( + 'function_score' => array( + 'functions' => array( + array( + 'random_score' => array( + 'seed' => 2, + ), + 'filter' => array( + 'term' => array( + 'price' => 4.5, + ), + ), + 'weight' => 2, + ), + ), + ), + ); + + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group functional + */ + public function testRandomScoreWithoutSeed() + { + $query = new FunctionScore(); + $query->setRandomScore(); + + $response = $this->_getIndexForTest()->search($query); + + $this->assertEquals(2, $response->count()); + } + + /** + * @group functional + */ + public function testScriptScore() + { + $scriptString = "_score * doc['price'].value"; + $script = new Script($scriptString); + $query = new FunctionScore(); + $query->addScriptScoreFunction($script); + $expected = array( + 'function_score' => array( + 'functions' => array( + array( + 'script_score' => array( + 'script' => $scriptString, + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $query->toArray()); + + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + // the document the highest price should be scored highest + $result0 = $results[0]->getData(); + $this->assertEquals("Miller's Field", $result0['name']); + } + + /** + * @group functional + */ + public function testSetMinScore() + { + $expected = array( + 'function_score' => array( + 'min_score' => 0.8, + 'functions' => array( + array( + 'gauss' => array( + 'price' => array( + 'origin' => 0, + 'scale' => 10, + ), + ), + ), + ), + ), + ); + + $query = new FunctionScore(); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', 0, 10); + $returnedValue = $query->setMinScore(0.8); + + $this->assertEquals($expected, $query->toArray()); + $this->assertInstanceOf('Elastica\Query\FunctionScore', $returnedValue); + + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + $this->assertCount(1, $results); + $this->assertEquals(1, $results[0]->getId()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FuzzyLikeThisTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FuzzyLikeThisTest.php new file mode 100644 index 00000000..fe11e788 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FuzzyLikeThisTest.php @@ -0,0 +1,300 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Index; +use Elastica\Query\FuzzyLikeThis; +use Elastica\Test\Base as BaseTest; +use Elastica\Type; +use Elastica\Type\Mapping; + +class FuzzyLikeThisTest extends BaseTest +{ + /** + * @group functional + */ + public function testSearch() + { + $client = $this->_getClient(); + $index = new Index($client, 'test'); + $index->create(array(), true); + $index->getSettings()->setNumberOfReplicas(0); + //$index->getSettings()->setNumberOfShards(1); + + $type = new Type($index, 'helloworldfuzzy'); + $mapping = new Mapping($type, array( + 'email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + )); + + $mapping->setSource(array('enabled' => false)); + $type->setMapping($mapping); + + $doc = new Document(1000, array('email' => 'testemail@gmail.com', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); + $type->addDocument($doc); + + // Refresh index + $index->refresh(); + + $fltQuery = new FuzzyLikeThis(); + $fltQuery->setLikeText('sample gmail'); + $fltQuery->addFields(array('email', 'content')); + $fltQuery->setMinSimilarity(0.3); + $fltQuery->setMaxQueryTerms(3); + $resultSet = $type->search($fltQuery); + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group unit + */ + public function testSetPrefixLength() + { + $query = new FuzzyLikeThis(); + + $length = 3; + $query->setPrefixLength($length); + + $data = $query->toArray(); + + $this->assertEquals($length, $data['fuzzy_like_this']['prefix_length']); + } + + /** + * @group unit + */ + public function testAddFields() + { + $query = new FuzzyLikeThis(); + + $fields = array('test1', 'test2'); + $query->addFields($fields); + + $data = $query->toArray(); + + $this->assertEquals($fields, $data['fuzzy_like_this']['fields']); + } + + /** + * @group unit + */ + public function testSetLikeText() + { + $query = new FuzzyLikeThis(); + + $text = ' hello world'; + $query->setLikeText($text); + + $data = $query->toArray(); + + $this->assertEquals(trim($text), $data['fuzzy_like_this']['like_text']); + } + + /** + * @group unit + */ + public function testSetIgnoreTF() + { + $query = new FuzzyLikeThis(); + + $ignoreTF = false; + $query->setIgnoreTF($ignoreTF); + $data = $query->toArray(); + $this->assertEquals($ignoreTF, $data['fuzzy_like_this']['ignore_tf']); + + $ignoreTF = true; + $query->setIgnoreTF($ignoreTF); + $data = $query->toArray(); + $this->assertEquals($ignoreTF, $data['fuzzy_like_this']['ignore_tf']); + } + + /** + * @group unit + */ + public function testSetIgnoreTFDefault() + { + $query = new FuzzyLikeThis(); + + $data = $query->toArray(); + + $defaultIgnoreTF = false; + $this->assertEquals($defaultIgnoreTF, $data['fuzzy_like_this']['ignore_tf']); + } + + /** + * @group unit + */ + public function testSetMinSimilarity() + { + $query = new FuzzyLikeThis(); + + $similarity = 2; + $query->setMinSimilarity($similarity); + + $data = $query->toArray(); + + $this->assertEquals($similarity, $data['fuzzy_like_this']['min_similarity']); + } + + /** + * @group unit + */ + public function testSetBoost() + { + $query = new FuzzyLikeThis(); + + $boost = 2.2; + $query->setBoost($boost); + + $data = $query->toArray(); + + $this->assertEquals($boost, $data['fuzzy_like_this']['boost']); + } + + /** + * @group unit + */ + public function testAddAnalyzerViasetParam() + { + $analyzer = 'snowball'; + + $query = new FuzzyLikeThis(); + $query->setParam('analyzer', $analyzer); + + $data = $query->toArray(); + $this->assertEquals($analyzer, $data['fuzzy_like_this']['analyzer']); + } + + /** + * @group unit + */ + public function testSetAnalyzer() + { + $analyzer = 'snowball'; + + $query = new FuzzyLikeThis(); + $query->setAnalyzer($analyzer); + + $data = $query->toArray(); + $this->assertEquals($analyzer, $data['fuzzy_like_this']['analyzer']); + } + + /** + * @group unit + */ + public function testAnalyzerNotPresentInArrayToMaintainDefaultOfField() + { + $query = new FuzzyLikeThis(); + + $data = $query->toArray(); + $this->assertArrayNotHasKey('analyzer', $data); + } + + /** + * @group unit + */ + public function testArgArrayFieldsOverwrittenBySetParams() + { + $query = new FuzzyLikeThis(); + $query->setMaxQueryTerms(100); + $query->setParam('max_query_terms', 200); + + $data = $query->toArray(); + $this->assertEquals(200, $data['fuzzy_like_this']['max_query_terms']); + } + + /** + * @group functional + */ + public function testSearchSetAnalyzer() + { + $client = $this->_getClient(); + $index = new Index($client, 'test'); + $index->create(array('analysis' => array( + 'analyzer' => array( + 'searchAnalyzer' => array( + 'type' => 'custom', + 'tokenizer' => 'standard', + 'filter' => array('myStopWords'), + ), + ), + 'filter' => array( + 'myStopWords' => array( + 'type' => 'stop', + 'stopwords' => array('The'), + ), + ), + )), true); + + $index->getSettings()->setNumberOfReplicas(0); + //$index->getSettings()->setNumberOfShards(1); + + $type = new Type($index, 'helloworldfuzzy'); + $mapping = new Mapping($type, array( + 'email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + )); + + $mapping->setSource(array('enabled' => false)); + $type->setMapping($mapping); + + $type->addDocuments(array( + new Document(1000, array('email' => 'testemail@gmail.com', 'content' => 'The Fuzzy Test!')), + new Document(1001, array('email' => 'testemail@gmail.com', 'content' => 'Elastica Fuzzy Test')), + )); + + // Refresh index + $index->refresh(); + + $fltQuery = new FuzzyLikeThis(); + $fltQuery->addFields(array('email', 'content')); + $fltQuery->setLikeText('The'); + + $fltQuery->setMinSimilarity(0.1); + $fltQuery->setMaxQueryTerms(3); + + // Test before analyzer applied, should return 1 result + $resultSet = $type->search($fltQuery); + $this->assertEquals(1, $resultSet->count()); + + $fltQuery->setParam('analyzer', 'searchAnalyzer'); + + $resultSet = $type->search($fltQuery); + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + public function testNoLikeTextProvidedShouldReturnNoResults() + { + $client = $this->_getClient(); + $index = new Index($client, 'test'); + $index->create(array(), true); + $index->getSettings()->setNumberOfReplicas(0); + + $type = new Type($index, 'helloworldfuzzy'); + $mapping = new Mapping($type, array( + 'email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + )); + + $mapping->setSource(array('enabled' => false)); + $type->setMapping($mapping); + + $doc = new Document(1000, array('email' => 'testemail@gmail.com', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); + $type->addDocument($doc); + + // Refresh index + $index->refresh(); + + $fltQuery = new FuzzyLikeThis(); + $fltQuery->setLikeText(''); + $fltQuery->addFields(array('email', 'content')); + $fltQuery->setMinSimilarity(0.3); + $fltQuery->setMaxQueryTerms(3); + $resultSet = $type->search($fltQuery); + + $this->assertEquals(0, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FuzzyTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FuzzyTest.php new file mode 100644 index 00000000..e9107232 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FuzzyTest.php @@ -0,0 +1,136 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query\Fuzzy; +use Elastica\Test\Base as BaseTest; + +class FuzzyTest extends BaseTest +{ + /** + * @group unit + */ + public function testToArray() + { + $fuzzy = new Fuzzy(); + $fuzzy->addField('user', array('value' => 'Nicolas', 'boost' => 1.0)); + $expectedArray = array( + 'fuzzy' => array( + 'user' => array( + 'value' => 'Nicolas', + 'boost' => 1.0, + ), + ), + ); + $this->assertEquals($expectedArray, $fuzzy->toArray(), 'Deprecated method failed'); + + $fuzzy = new Fuzzy('user', 'Nicolas'); + $expectedArray = array( + 'fuzzy' => array( + 'user' => array( + 'value' => 'Nicolas', + ), + ), + ); + $this->assertEquals($expectedArray, $fuzzy->toArray()); + + $fuzzy = new Fuzzy(); + $fuzzy->setField('user', 'Nicolas')->setFieldOption('boost', 1.0); + $expectedArray = array( + 'fuzzy' => array( + 'user' => array( + 'value' => 'Nicolas', + 'boost' => 1.0, + ), + ), + ); + $this->assertEquals($expectedArray, $fuzzy->toArray()); + } + + /** + * @group functional + */ + public function testQuery() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'Baden')), + new Document(4, array('name' => 'Baden Baden')), + )); + + $index->refresh(); + + $field = 'name'; + + $query = new Fuzzy(); + $query->setField($field, 'Baden'); + + $resultSet = $index->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group unit + */ + public function testBadArguments() + { + $this->setExpectedException('Elastica\Exception\InvalidException'); + $query = new Fuzzy(); + $query->addField('name', array(array('value' => 'Baden'))); + + $this->setExpectedException('Elastica\Exception\InvalidException'); + $query = new Fuzzy(); + $query->setField('name', array()); + + $this->setExpectedException('Elastica\Exception\InvalidException'); + $query = new Fuzzy(); + $query->setField('name', 'value'); + $query->setField('name1', 'value1'); + } + + /** + * @group functional + */ + public function testFuzzyWithFacets() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'Baden')), + new Document(4, array('name' => 'Baden Baden')), + )); + + $index->refresh(); + + $field = 'name'; + + $fuzzyQuery = new Fuzzy(); + $fuzzyQuery->setField($field, 'Baden'); + + $facet = new \Elastica\Facet\Terms('test'); + $facet->setField('name'); + + $query = new \Elastica\Query($fuzzyQuery); + $query->addFacet($facet); + + $resultSet = $index->search($query); + + // Assert query worked ok + $this->assertEquals(2, $resultSet->count()); + + // Check Facets + $this->assertTrue($resultSet->hasFacets()); + $facets = $resultSet->getFacets(); + $this->assertEquals(2, $facets['test']['total']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HasChildTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HasChildTest.php new file mode 100644 index 00000000..a717b1ab --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HasChildTest.php @@ -0,0 +1,117 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query; +use Elastica\Query\HasChild; +use Elastica\Query\Match; +use Elastica\Query\MatchAll; +use Elastica\Test\Base as BaseTest; +use Elastica\Type\Mapping; + +class HasChildTest extends BaseTest +{ + /** + * @group unit + */ + public function testToArray() + { + $q = new MatchAll(); + + $type = 'test'; + + $query = new HasChild($q, $type); + + $expectedArray = array( + 'has_child' => array( + 'query' => $q->toArray(), + 'type' => $type, + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group unit + */ + public function testSetScope() + { + $q = new MatchAll(); + + $type = 'test'; + + $scope = 'foo'; + + $query = new HasChild($q, $type); + $query->setScope($scope); + + $expectedArray = array( + 'has_child' => array( + 'query' => $q->toArray(), + 'type' => $type, + '_scope' => $scope, + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group functional + */ + public function testTypeInsideHasChildSearch() + { + $index = $this->_getTestIndex(); + + $f = new Match(); + $f->setField('alt.name', 'testname'); + $query = new HasChild($f, 'child'); + + $searchQuery = new Query(); + $searchQuery->setQuery($query); + $searchResults = $index->search($searchQuery); + + $this->assertEquals(1, $searchResults->count()); + + $result = $searchResults->current()->getData(); + $expected = array('id' => 'parent2', 'user' => 'parent2', 'email' => 'parent2@test.com'); + + $this->assertEquals($expected, $result); + } + + protected function _getTestIndex() + { + $index = $this->_createIndex('has_child_test'); + + $parentType = $index->getType('parent'); + + $childType = $index->getType('child'); + $childMapping = new Mapping($childType); + $childMapping->setParent('parent'); + $childMapping->send(); + + $altType = $index->getType('alt'); + $altDoc = new Document('alt1', array('name' => 'altname')); + $altType->addDocument($altDoc); + + $parent1 = new Document('parent1', array('id' => 'parent1', 'user' => 'parent1', 'email' => 'parent1@test.com')); + $parentType->addDocument($parent1); + $parent2 = new Document('parent2', array('id' => 'parent2', 'user' => 'parent2', 'email' => 'parent2@test.com')); + $parentType->addDocument($parent2); + + $child1 = new Document('child1', array('id' => 'child1', 'user' => 'child1', 'email' => 'child1@test.com')); + $child1->setParent('parent1'); + $childType->addDocument($child1); + $child2 = new Document('child2', array('id' => 'child2', 'user' => 'child2', 'email' => 'child2@test.com')); + $child2->setParent('parent2'); + $childType->addDocument($child2); + $child3 = new Document('child3', array('id' => 'child3', 'user' => 'child3', 'email' => 'child3@test.com', 'alt' => array(array('name' => 'testname')))); + $child3->setParent('parent2'); + $childType->addDocument($child3); + + $index->refresh(); + + return $index; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HasParentTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HasParentTest.php new file mode 100644 index 00000000..31a89852 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HasParentTest.php @@ -0,0 +1,108 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query\HasParent; +use Elastica\Query\Match; +use Elastica\Query\MatchAll; +use Elastica\Search; +use Elastica\Test\Base as BaseTest; +use Elastica\Type\Mapping; + +class HasParentTest extends BaseTest +{ + /** + * @group unit + */ + public function testToArray() + { + $q = new MatchAll(); + + $type = 'test'; + + $query = new HasParent($q, $type); + + $expectedArray = array( + 'has_parent' => array( + 'query' => $q->toArray(), + 'type' => $type, + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group unit + */ + public function testSetScope() + { + $q = new MatchAll(); + + $type = 'test'; + + $scope = 'foo'; + + $query = new HasParent($q, $type); + $query->setScope($scope); + + $expectedArray = array( + 'has_parent' => array( + 'query' => $q->toArray(), + 'type' => $type, + '_scope' => $scope, + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group functional + */ + public function testHasParent() + { + $index = $this->_createIndex(); + + $shopType = $index->getType('shop'); + $productType = $index->getType('product'); + $mapping = new Mapping(); + $mapping->setParent('shop'); + $productType->setMapping($mapping); + + $shopType->addDocuments( + array( + new Document('zurich', array('brand' => 'google')), + new Document('london', array('brand' => 'apple')), + ) + ); + + $doc1 = new Document(1, array('device' => 'chromebook')); + $doc1->setParent('zurich'); + + $doc2 = new Document(2, array('device' => 'macmini')); + $doc2->setParent('london'); + + $productType->addDocument($doc1); + $productType->addDocument($doc2); + + $index->refresh(); + + // All documents + $parentQuery = new HasParent(new MatchAll(), $shopType->getName()); + $search = new Search($index->getClient()); + $results = $search->search($parentQuery); + $this->assertEquals(2, $results->count()); + + $match = new Match(); + $match->setField('brand', 'google'); + + $parentQuery = new HasParent($match, $shopType->getName()); + $search = new Search($index->getClient()); + $results = $search->search($parentQuery); + $this->assertEquals(1, $results->count()); + $result = $results->current(); + $data = $result->getData(); + $this->assertEquals($data['device'], 'chromebook'); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HighlightTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HighlightTest.php new file mode 100644 index 00000000..c6850019 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/HighlightTest.php @@ -0,0 +1,48 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query; +use Elastica\Query\QueryString; +use Elastica\Test\Base as BaseTest; + +class HighlightTest extends BaseTest +{ + /** + * @group functional + */ + public function testHightlightSearch() + { + $index = $this->_createIndex(); + $type = $index->getType('helloworld'); + + $phrase = 'My name is ruflin'; + + $type->addDocuments(array( + new Document(1, array('id' => 1, 'phrase' => $phrase, 'username' => 'hanswurst', 'test' => array('2', '3', '5'))), + new Document(2, array('id' => 2, 'phrase' => $phrase, 'username' => 'peter', 'test' => array('2', '3', '5'))), + )); + + $queryString = new QueryString('rufl*'); + $query = new Query($queryString); + $query->setHighlight(array( + 'pre_tags' => array('<em class="highlight">'), + 'post_tags' => array('</em>'), + 'fields' => array( + 'phrase' => array( + 'fragment_size' => 200, + 'number_of_fragments' => 1, + ), + ), + )); + + $index->refresh(); + + $resultSet = $type->search($query); + foreach ($resultSet as $result) { + $highlight = $result->getHighlights(); + $this->assertEquals(array('phrase' => array(0 => 'My name is <em class="highlight">ruflin</em>')), $highlight); + } + $this->assertEquals(2, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/IdsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/IdsTest.php new file mode 100644 index 00000000..7b39020f --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/IdsTest.php @@ -0,0 +1,187 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query\Ids; +use Elastica\Test\Base as BaseTest; + +class IdsTest extends BaseTest +{ + protected $_index; + protected $_type; + + protected function setUp() + { + parent::setUp(); + + $index = $this->_createIndex(); + + $type1 = $index->getType('helloworld1'); + $type2 = $index->getType('helloworld2'); + + $doc = new Document(1, array('name' => 'hello world')); + $type1->addDocument($doc); + + $doc = new Document(2, array('name' => 'nicolas ruflin')); + $type1->addDocument($doc); + + $doc = new Document(3, array('name' => 'ruflin')); + $type1->addDocument($doc); + + $doc = new Document(4, array('name' => 'hello world again')); + $type2->addDocument($doc); + + $index->refresh(); + + $this->_type = $type1; + $this->_index = $index; + } + + /** + * @group functional + */ + public function testSetIdsSearchSingle() + { + $query = new Ids(); + $query->setIds('1'); + + $resultSet = $this->_type->search($query); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetIdsSearchArray() + { + $query = new Ids(); + $query->setIds(array('1', '2')); + + $resultSet = $this->_type->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group functional + */ + public function testAddIdsSearchSingle() + { + $query = new Ids(); + $query->addId('3'); + + $resultSet = $this->_type->search($query); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testComboIdsSearchArray() + { + $query = new Ids(); + + $query->setIds(array('1', '2')); + $query->addId('3'); + + $resultSet = $this->_type->search($query); + + $this->assertEquals(3, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeSingleSearchSingle() + { + $query = new Ids(); + + $query->setIds('1'); + $query->setType('helloworld1'); + + $resultSet = $this->_index->search($query); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeSingleSearchArray() + { + $query = new Ids(); + + $query->setIds(array('1', '2')); + $query->setType('helloworld1'); + + $resultSet = $this->_index->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeSingleSearchSingleDocInOtherType() + { + $query = new Ids(); + + // Doc 4 is in the second type... + $query->setIds('4'); + $query->setType('helloworld1'); + + $resultSet = $this->_index->search($query); + + // ...therefore 0 results should be returned + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeSingleSearchArrayDocInOtherType() + { + $query = new Ids(); + + // Doc 4 is in the second type... + $query->setIds(array('1', '4')); + $query->setType('helloworld1'); + + $resultSet = $this->_index->search($query); + + // ...therefore only 1 result should be returned + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeArraySearchArray() + { + $query = new Ids(); + + $query->setIds(array('1', '4')); + $query->setType(array('helloworld1', 'helloworld2')); + + $resultSet = $this->_index->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group functional + */ + public function testSetTypeArraySearchSingle() + { + $query = new Ids(); + + $query->setIds('4'); + $query->setType(array('helloworld1', 'helloworld2')); + + $resultSet = $this->_index->search($query); + + $this->assertEquals(1, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/ImageTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/ImageTest.php new file mode 100644 index 00000000..989e4e4a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/ImageTest.php @@ -0,0 +1,159 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Index; +use Elastica\Query\Image; +use Elastica\Test\Base as BaseTest; +use Elastica\Type; +use Elastica\Type\Mapping; + +class ImageTest extends BaseTest +{ + /** + * @var string + */ + protected $_testFileContent; + + protected function setUp() + { + parent::setUp(); + $this->_testFileContent = base64_encode(file_get_contents(BASE_PATH.'/data/test.jpg')); + } + + /** + * @group unit + */ + public function testToArrayFromReference() + { + $client = $this->_getClient(); + $index = new Index($client, 'test'); + $type = new Type($index, 'helloworld'); + $field = 'image'; + + $query = new Image(); + $query->setFieldFeature($field, 'CEDD'); + $query->setFieldHash($field, 'BIT_SAMPLING'); + $query->setFieldBoost($field, 100); + + $query->setImageByReference($field, $index->getName(), $type->getName(), 10); + + $jsonString = '{"image":{"image":{"feature":"CEDD","hash":"BIT_SAMPLING","boost":100,"index":"test","type":"helloworld","id":10,"path":"image"}}}'; + $this->assertEquals($jsonString, json_encode($query->toArray())); + } + + /** + * @group unit + */ + public function testToArrayFromImage() + { + $field = 'image'; + + $query = new Image(); + $query->setFieldFeature($field, 'CEDD'); + $query->setFieldHash($field, 'BIT_SAMPLING'); + $query->setFieldBoost($field, 100); + + $query->setFieldImage($field, BASE_PATH.'/data/test.jpg'); + + $jsonString = '{"image":{"image":{"feature":"CEDD","hash":"BIT_SAMPLING","boost":100,"image":"\/9j\/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP\/sABFEdWNreQABAAQAAAA8AAD\/4QN6aHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI\/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjUtYzAyMSA3OS4xNTQ5MTEsIDIwMTMvMTAvMjktMTE6NDc6MTYgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bXBNTTpPcmlnaW5hbERvY3VtZW50SUQ9InhtcC5kaWQ6OWQ4MjQ5N2MtNzViMS0wYzQ5LTg4ZjMtMDdiNmRhMjU0ZWRhIiB4bXBNTTpEb2N1bWVudElEPSJ4bXAuZGlkOjA4NjBGM0Y1QkJGQTExRTM4MjQ0QzMzNjU2MjUxOEJGIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOjA4NjBGM0Y0QkJGQTExRTM4MjQ0QzMzNjU2MjUxOEJGIiB4bXA6Q3JlYXRvclRvb2w9IkFkb2JlIFBob3Rvc2hvcCBDQyAoV2luZG93cykiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo5ZDgyNDk3Yy03NWIxLTBjNDktODhmMy0wN2I2ZGEyNTRlZGEiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6OWQ4MjQ5N2MtNzViMS0wYzQ5LTg4ZjMtMDdiNmRhMjU0ZWRhIi8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+\/+4ADkFkb2JlAGTAAAAAAf\/bAIQABgQEBAUEBgUFBgkGBQYJCwgGBggLDAoKCwoKDBAMDAwMDAwQDA4PEA8ODBMTFBQTExwbGxscHx8fHx8fHx8fHwEHBwcNDA0YEBAYGhURFRofHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8f\/8AAEQgAZABkAwERAAIRAQMRAf\/EAKwAAAEFAQEBAQAAAAAAAAAAAAgAAwQFBwYCAQkBAQEAAwEBAAAAAAAAAAAAAAABAgQFAwYQAAEDAgMCBQwOCQQDAAAAAAIBAwQABRESBiEHMUFhExRRcYEisnPDNIQVRRaRodEyQlJikiOzVGYnCLFyo9NklKQlGIIzQ2VTdhcRAAIBAgQEBAYDAQAAAAAAAAABAhEDITESBEFRMgVhcZFCgaHR4SITsVJiFf\/aAAwDAQACEQMRAD8AJyHDhlDYImG1JWwVVUBVVVRTkqge6DC+ztfMH3KgF0GF9na+YPuUAugwvs7XzB9ygF0GF9na+YPuUBR6n1NpDTTAuXUmgdcTFmK22JvuYfFbRMcOVdnLXje3MLa\/JmEpqOZmty32GbiparFGaa24OS1QzXlyNoiJ85a50+5y9q9Tyd7kiva3v6jQ8xwraY\/E5gh9tDrBdxueBP2s6ax74rK6YtXq0DFx2LJjIjzafrAooaJ1sa2LfcU+pUM1eXE0i3nY7jEblwUjyYzqYg62gEK+wnDyV0IzUlVOqPVNMkdBhfZ2vmD7lZFF0GF9na+YPuUBD6JF88c3zIc30fNkyphjnwxwwqkJkHxKP3oO5SoUfoBUAqA47eXr5rSlqEY6C7eJuIwmC2iKJ755xE+AGPZXZWpu9z+qOHU8jzuXNK8Qe813vd2UlV243WaWJmvbOGqcarsQQHsCKVw4Qnclh+UmamLZ3dn3UKQCd1lkprwx42CCnIrpIqqvWSuva7Uve\/Q9la5l0W6rT6hgCSQPiLnlL2iTCth9ss+PqX9aOcv27W725spEEimsDtJpRyvoicaInan2NvJWjf7dKKrB6l8\/uYuDRWaT1fdNN3BJcIlNhxU6XCVcG3hTh\/VcT4Jdhdlali+7bqsiRk0ETZrvBvFsj3KCfORZI5wVdipxKJJxEK7FSu9bmpxTWRtJ1VSbWZSF6b8m8JVA9B8Sj96DuUqAfoDNdab+dKaZvDtnGPIuk6MqDMSNkRtolTHIpuEKKaIqYoOOHHtoCz3bb07frsriMO3yIPm7ms\/SCbLPz2bDLkIuDJx0Bh2u9SHf9VXG5kalHEyYhp8WOwqiOCfKXE+zXzO4u67jkaM5VdTSt3ulAtdrB54E84zBFyUfGIr2wMovUFOHqlX0Gy2ytQ\/08\/oe8I0R3LTIinBW6ZEphGxLthx61Ggiw6Mw8zwIQrXk20zMxXexpMLZLG7xRysSTyShRMERxdouf68MC5a5HcLCi9a45+fP4nhcjTEsdx2oTbuEywun9DIBZcUV4nAVBdRP1kUS7FTt12jcfiWzLgbJXWNghem\/JvCVQPQfEo\/eg7lKgH6AC1m2u6h3hnajeVty63h9hySqZlFDkGpHgvCqCi4VQEvp3dvYNA2a+yLI7KcclxlN1ZTgubY7ZqCjlEMPfLjXncdIvyI8gfbLGR+4W1hzaDzzAn1lMca+b28azinzRoxWISEIUwxw4Vxr642mTxSqQfbCowTI+IqvUVNtYSM4nI70GG39JXNC4W2VcFeoQEhItae9VbMjG50syPdfKNnXtmUOFxxxov1TZPH9FcfaOl2J423+SCVr6A2yF6b8m8JVA9B8Sj96DuUqAfoAOtGF+MtrT\/v3vrXaoDAmRhlRH4x+9fbJsusYqK\/prGSqqBgjgj9ul82YqMm3vZCBeHPHcwVOyoV8zGsJeMX\/AAaGQRdjlNTobctgkJl4BdbVOMTTH2uCvrIyUkmsmbWZbtJjtrMhKbGsWyj2wRxrBmSM73v3lqHpd+Pm+muBDHaHjUcUJxewKVz+43KW6cZYfUwuukfM4PczbHJuuor+XFm3tOyHC6iqPNB7Z1ztjGt1eB5WlWQRdd42yF6b8m8JVA9B8Sj96DuUqA5jeTvJtWhLSzMnMPSn5hmzBjsiioTwgpojhqqIA7Npe1QA2borLf77vMtFwYiOOMx5x3C5TEAkjtoucyTnFTLiRnlEccaoDBqAH7fhpB60X71jihjbLqSJLUU2NS8MMS6gvImxfjJypXG31jTLWsma16NHUh7ud4Q2VUt1wNUtxkqsv7V5kiXFUJE282q7dnvV5Kz2W9UFon08Hy+xjCdDaIdyYkNi82Ym04mIOAqEJIvGhJsrtp1VVkexYDLbRMcajZUVWotV2qzQilXGQLDSJ2grtM1+K2HCS14Xr0barJ\/V+RXJLMH7VWp52qr10lwVajtpzcONjjzbeOOK4cJku0q+d3O4c5apYcvBGpOdXU3XdTo0tPWJX5LeS43DK48K++BtE+jbXl2qq8q11e3bdwhql1S+S4GzZhRVZ21dA9SF6b8m8JVA9B8Sj96DuUqA5feRu0tOvLbGh3CVIhlCcJ+K7GUNjhAoduJiWYUx4NnXoAbdz+p9R6f3m2yyx5riwJVwdt1wgqZLGcwU2+dFtVwE0IEJCTbxLilUBg1AVmpVsnmKYF7Fpy1uN83IbfVEA0NUERxXjIlRE5axkk1R5ElSmIKWoLQ9Y7m9HUSSOhKraEqkQCq4oKl8LBNmbjrlbrt8oOsMY\/NGq7b4Huz6juduPC2z3IxLtVts+1XrguI+1WjC7KHS3EwToXbu8PWxt5EupgnxgFsS9lBr1e8vP3My1y5lCy1e7\/cubYGTd7ia4YBmePsrtQE66ola6UpvCsn6mGLZtu7XdAtoNu66gyOXAVQmIIKhtslxEZcBmnFh2qcvDXT23b6PVcz4I2LdmmLNTrqnuKgIXpvybwlUD0HxKP3oO5SoB+gAo0Qf43WlPvC\/9c9VAa9QGC79NZjcLoOnIp5oVtJHJypwHKVO1DlRoVxX5S8lcff39UtCyWZq35VwMrLUN3Pmbc235yJ0xYhxDxJxTNcoNtn75MVWvXbbqcaLqRhCbQTunN2OloOmIlquFriy3hHnJTjrYuKr57XMDVMcEXYmHEldCduM+pI29KeZIa3XbvWjQwsETMm3aGZPYVVSvJbS1\/VE\/XHkYlvp3zav3fa8LTWlQtsK2JDjyBZKIiqrjqmhe8NtPgJxV7xilkZJULrcV+Ya86v1H6ranjRxnyGnHrdPiCTYuKymZxpxsiPAsmJCQrhs4KpTfKAVAQvTfk3hKoHoPiUfvQdylQD9ABHoYvxxtP8A7E\/9c9VAW28PV7WltNPzhUSnO\/QW5lfhPmi5VVPignbFyJWvub6twb48DCc9KqChcZZqpm44rjpkRuOltIzJcxEvKSrjXz0at1ZpGk\/l10Ot0vD2sZ7eMO2kUe0iSYoclUwdewX\/AMQrkH5Sr1K7Gzte5nvZhxCMroGyKgOe1ToPR2o4stLzZ4cx6QwrBynGGykICIuXK8qZxUVXEcF2LQAafl6JR3zaYRSXY7KDMvCuEV5NvXwoA7KAVAQvTfk3hKoHoPiUfvQdylQD9AA9oUvx0tKfeN\/656qAjN48yyXqZJ09emnIc+P9JalLATNvKirJil71xMVyOBwphtTai15XbMbkaSMZxTWJiZ7utS3a+x7PazbkdKcQFliuVGWse3dMC29oO3BMcV2Vzf8Anzi+aNd2WFhp2w27T9jhWW3BzcKA0LLI8aonCRdUiXElXq11IxSVEbKVFQsayKKgESISKK7UXYqUAAmu7A9uz1+o2K\/xJsq2yykQXoh85IikJqoty21Hm0MUVRIcxIqcOHBQBhblNb3bW+7i16juwMNz5ZPg8MUSFr6F82kVBMjVMUDFdvDQHc0BC9N+TeEqgeg+JR+9B3KVAP0ADOgy\/Ha0J95H\/rnqoDT1Fpiwajt62+9wm50VVQxFxFQgNOA2zFUNs04iBUWoCn0Xu4tGkpEx+HMmTSlZRBZ7ovkyCYrkBzKJqir8dSXZw0B1dAKgFQHl1VFo1HhQVVOvhQH567obLA1XvYsNnvIrJhzpjzs0CVfpUaBx9RNeHAyDAuSgP0Et9ut9tiNwrdFZhQ2UwajR2xaaBFXHtQBEFNvUSgJFAQvTfk3hKoHoPiUfvQdylQD9AABrKPedG7y7o2RlBu1tujs6A+qIiqJPq8w+CFsICEk9tKoO2\/yy3qIiY+aNibV6M5t5f9+oD5\/lpvV\/6j+Wc\/f0B5X82u9Xi80fyzn7+gPJfm43qim3zR\/LOfv6AZP83+9ZODzP\/LO\/v6Ag3P8ANvvZm2+TC5y2xkktk0siPHNHQQ0wUm1N0xQuoqitARPyqafn3XfDbJ0dslhWRqRJmPInaijjJstipcGYzc2Jx4L1KAO2gFQEL035N4SqBmJ546KzzfR+byDkzZ8cMEwxwoQe\/vf8N+0oU5bWfqdjH9c\/V7NgvRfO3NZsMdvN8\/twx4cKA5j8BuP1K\/pKA+fgL9yv6OgF+An3J\/o6A8r\/APAOP1I7PQ6A8F\/j1x+o\/Z6FQHgv8deP1F7PQqA7rRnq\/wCal9S\/MfmrOubzRzfMc5gmObo\/a5sOrtoC\/wD73\/DftKAX97\/hv2lARP7p50\/4Of5j5eTJn9nHGhD\/2Q=="}}}'; + $this->assertEquals($jsonString, json_encode($query->toArray())); + } + + /** + * @group functional + */ + public function testFromReference() + { + $this->markTestSkipped('Tests skipped as plugin not working properly with ES 1.6.0. See https://github.com/ruflin/Elastica/pull/881'); + $field = 'image'; + + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + + $type = $index->getType('test'); + + $mapping = new Mapping($type, array( + $field => array( + 'type' => 'image', + 'store' => false, + 'include_in_all' => false, + 'feature' => array( + 'CEDD' => array( + 'hash' => 'BIT_SAMPLING', + ), + ), + ), + ) + ); + + $type->setMapping($mapping); + + $type->addDocuments(array( + new Document(1, array($field => $this->_testFileContent)), + new Document(2, array($field => $this->_testFileContent)), + new Document(3, array($field => $this->_testFileContent)), + )); + + $index->refresh(); + + $query = new Image(); + $query->setFieldFeature($field, 'CEDD'); + $query->setFieldHash($field, 'BIT_SAMPLING'); + $query->setFieldBoost($field, 100); + $query->setImageByReference($field, $index->getName(), $type->getName(), 1); + + $resultSet = $index->search($query); + $this->assertEquals(3, $resultSet->count()); + } + + /** + * @group functional + */ + public function testFromImage() + { + $this->markTestSkipped('Tests skipped as plugin not working properly with ES 1.6.0. See https://github.com/ruflin/Elastica/pull/881'); + + $field = 'image'; + + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + + $type = $index->getType('test'); + + $mapping = new Mapping($type, array( + $field => array( + 'type' => 'image', + 'store' => false, + 'include_in_all' => false, + 'feature' => array( + 'CEDD' => array( + 'hash' => 'BIT_SAMPLING', + ), + ), + ), + ) + ); + + $type->setMapping($mapping); + + $type->addDocuments(array( + new Document(1, array($field => $this->_testFileContent)), + new Document(2, array($field => $this->_testFileContent)), + new Document(3, array($field => $this->_testFileContent)), + )); + + $index->refresh(); + + $query = new Image(); + $query->setFieldFeature($field, 'CEDD'); + $query->setFieldHash($field, 'BIT_SAMPLING'); + $query->setFieldBoost($field, 100); + $query->setFieldImage($field, BASE_PATH.'/data/test.jpg'); + + $resultSet = $index->search($query); + $this->assertEquals(3, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MatchAllTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MatchAllTest.php new file mode 100644 index 00000000..7eb0fae0 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MatchAllTest.php @@ -0,0 +1,49 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query\MatchAll; +use Elastica\Search; +use Elastica\Test\Base as BaseTest; + +class MatchAllTest extends BaseTest +{ + /** + * @group unit + */ + public function testToArray() + { + $query = new MatchAll(); + + $expectedArray = array('match_all' => new \stdClass()); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group functional + */ + public function testMatchAllIndicesTypes() + { + $index1 = $this->_createIndex(); + $index2 = $this->_createIndex(); + + $client = $index1->getClient(); + + $search1 = new Search($client); + $resultSet1 = $search1->search(new MatchAll()); + + $doc1 = new Document(1, array('name' => 'ruflin')); + $doc2 = new Document(1, array('name' => 'ruflin')); + $index1->getType('test')->addDocument($doc1); + $index2->getType('test')->addDocument($doc2); + + $index1->refresh(); + $index2->refresh(); + + $search2 = new Search($client); + $resultSet2 = $search2->search(new MatchAll()); + + $this->assertEquals($resultSet1->getTotalHits() + 2, $resultSet2->getTotalHits()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MatchTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MatchTest.php new file mode 100644 index 00000000..98fdf26a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MatchTest.php @@ -0,0 +1,339 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query\Match; +use Elastica\Query\MatchPhrase; +use Elastica\Query\MatchPhrasePrefix; +use Elastica\Test\Base as BaseTest; + +class MatchTest extends BaseTest +{ + /** + * @group unit + */ + public function testToArray() + { + $field = 'test'; + $testQuery = 'Nicolas Ruflin'; + $type = 'phrase'; + $operator = 'and'; + $analyzer = 'myanalyzer'; + $boost = 2.0; + $minimumShouldMatch = 2; + $fuzziness = 0.3; + $fuzzyRewrite = 'constant_score_boolean'; + $prefixLength = 3; + $maxExpansions = 12; + + $query = new Match(); + $query->setFieldQuery($field, $testQuery); + $query->setFieldType($field, $type); + $query->setFieldOperator($field, $operator); + $query->setFieldAnalyzer($field, $analyzer); + $query->setFieldBoost($field, $boost); + $query->setFieldMinimumShouldMatch($field, $minimumShouldMatch); + $query->setFieldFuzziness($field, $fuzziness); + $query->setFieldFuzzyRewrite($field, $fuzzyRewrite); + $query->setFieldPrefixLength($field, $prefixLength); + $query->setFieldMaxExpansions($field, $maxExpansions); + + $expectedArray = array( + 'match' => array( + $field => array( + 'query' => $testQuery, + 'type' => $type, + 'operator' => $operator, + 'analyzer' => $analyzer, + 'boost' => $boost, + 'minimum_should_match' => $minimumShouldMatch, + 'fuzziness' => $fuzziness, + 'fuzzy_rewrite' => $fuzzyRewrite, + 'prefix_length' => $prefixLength, + 'max_expansions' => $maxExpansions, + ), + ), + ); + + $this->assertEquals($expectedArray, $query->toArray()); + } + + /** + * @group functional + */ + public function testMatch() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'New Hampshire')), + new Document(4, array('name' => 'Basel Land')), + )); + + $index->refresh(); + + $field = 'name'; + $operator = 'or'; + + $query = new Match(); + $query->setFieldQuery($field, 'Basel New'); + $query->setFieldOperator($field, $operator); + + $resultSet = $index->search($query); + + $this->assertEquals(4, $resultSet->count()); + } + + /** + * @group functional + */ + public function testMatchSetFieldBoost() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'New Hampshire')), + new Document(4, array('name' => 'Basel Land')), + )); + + $index->refresh(); + + $field = 'name'; + $operator = 'or'; + + $query = new Match(); + $query->setFieldQuery($field, 'Basel New'); + $query->setFieldOperator($field, $operator); + $query->setFieldBoost($field, 1.2); + + $resultSet = $index->search($query); + + $this->assertEquals(4, $resultSet->count()); + } + + /** + * @group functional + */ + public function testMatchSetFieldBoostWithString() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'New Hampshire')), + new Document(4, array('name' => 'Basel Land')), + )); + + $index->refresh(); + + $field = 'name'; + $operator = 'or'; + + $query = new Match(); + $query->setFieldQuery($field, 'Basel New'); + $query->setFieldOperator($field, $operator); + $query->setFieldBoost($field, '1.2'); + + $resultSet = $index->search($query); + + $this->assertEquals(4, $resultSet->count()); + } + + /** + * @group functional + */ + public function testMatchZeroTerm() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + )); + + $index->refresh(); + + $query = new Match(); + $query->setFieldQuery('name', ''); + $query->setFieldZeroTermsQuery('name', Match::ZERO_TERM_ALL); + + $resultSet = $index->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group functional + */ + public function testMatchPhrase() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'New Hampshire')), + new Document(4, array('name' => 'Basel Land')), + )); + + $index->refresh(); + + $field = 'name'; + $type = 'phrase'; + + $query = new Match(); + $query->setFieldQuery($field, 'New York'); + $query->setFieldType($field, $type); + + $resultSet = $index->search($query); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testMatchPhraseAlias() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'New Hampshire')), + new Document(4, array('name' => 'Basel Land')), + )); + + $index->refresh(); + + $field = 'name'; + + $query = new MatchPhrase(); + $query->setFieldQuery($field, 'New York'); + + $resultSet = $index->search($query); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testMatchPhrasePrefix() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'New Hampshire')), + new Document(4, array('name' => 'Basel Land')), + )); + + $index->refresh(); + + $field = 'name'; + $type = 'phrase_prefix'; + + $query = new Match(); + $query->setFieldQuery($field, 'New'); + $query->setFieldType($field, $type); + + $resultSet = $index->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group functional + */ + public function testMatchPhrasePrefixAlias() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'New Hampshire')), + new Document(4, array('name' => 'Basel Land')), + )); + + $index->refresh(); + + $field = 'name'; + + $query = new MatchPhrasePrefix(); + $query->setFieldQuery($field, 'New'); + + $resultSet = $index->search($query); + + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group unit + */ + public function testMatchFuzzinessType() + { + $field = 'test'; + $query = new Match(); + + $fuzziness = 'AUTO'; + $query->setFieldFuzziness($field, $fuzziness); + + $parameters = $query->getParam($field); + $this->assertEquals($fuzziness, $parameters['fuzziness']); + + $fuzziness = 0.3; + $query->setFieldFuzziness($field, $fuzziness); + + $parameters = $query->getParam($field); + $this->assertEquals($fuzziness, $parameters['fuzziness']); + } + + /** + * @group unit + */ + public function testConstruct() + { + $match = new Match(null, 'values'); + $this->assertEquals(array('match' => array()), $match->toArray()); + + $match = new Match('field', null); + $this->assertEquals(array('match' => array()), $match->toArray()); + + $match1 = new Match('field', 'values'); + $match2 = new Match(); + $match2->setField('field', 'values'); + $this->assertEquals($match1->toArray(), $match2->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MoreLikeThisTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MoreLikeThisTest.php new file mode 100644 index 00000000..5dfe9379 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MoreLikeThisTest.php @@ -0,0 +1,240 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Index; +use Elastica\Query; +use Elastica\Query\MoreLikeThis; +use Elastica\Test\Base as BaseTest; +use Elastica\Type; +use Elastica\Type\Mapping; + +class MoreLikeThisTest extends BaseTest +{ + /** + * @group functional + */ + public function testSearch() + { + $client = $this->_getClient(); + $index = new Index($client, 'test'); + $index->create(array(), true); + $index->getSettings()->setNumberOfReplicas(0); + //$index->getSettings()->setNumberOfShards(1); + + $type = new Type($index, 'helloworldmlt'); + $mapping = new Mapping($type, array( + 'email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), + )); + + $mapping->setSource(array('enabled' => false)); + $type->setMapping($mapping); + + $doc = new Document(1000, array('email' => 'testemail@gmail.com', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); + $type->addDocument($doc); + + $doc = new Document(1001, array('email' => 'nospam@gmail.com', 'content' => 'This is a fake nospam email address for gmail')); + $type->addDocument($doc); + + // Refresh index + $index->refresh(); + + $mltQuery = new MoreLikeThis(); + $mltQuery->setLikeText('fake gmail sample'); + $mltQuery->setFields(array('email', 'content')); + $mltQuery->setMaxQueryTerms(1); + $mltQuery->setMinDocFrequency(1); + $mltQuery->setMinTermFrequency(1); + + $query = new Query(); + $query->setFields(array('email', 'content')); + $query->setQuery($mltQuery); + + $resultSet = $type->search($query); + $resultSet->getResponse()->getData(); + $this->assertEquals(2, $resultSet->count()); + } + + /** + * @group unit + */ + public function testSetFields() + { + $query = new MoreLikeThis(); + + $fields = array('firstname', 'lastname'); + $query->setFields($fields); + + $data = $query->toArray(); + $this->assertEquals($fields, $data['more_like_this']['fields']); + } + + /** + * @group unit + */ + public function testSetIds() + { + $query = new MoreLikeThis(); + $ids = array(1, 2, 3); + $query->setIds($ids); + + $data = $query->toArray(); + $this->assertEquals($ids, $data['more_like_this']['ids']); + } + + /** + * @group unit + */ + public function testSetLikeText() + { + $query = new MoreLikeThis(); + $query->setLikeText(' hello world'); + + $data = $query->toArray(); + $this->assertEquals('hello world', $data['more_like_this']['like_text']); + } + + /** + * @group unit + */ + public function testSetBoost() + { + $query = new MoreLikeThis(); + + $boost = 1.3; + $query->setBoost($boost); + + $this->assertEquals($boost, $query->getParam('boost')); + } + + /** + * @group unit + */ + public function testSetMaxQueryTerms() + { + $query = new MoreLikeThis(); + + $max = 3; + $query->setMaxQueryTerms($max); + + $this->assertEquals($max, $query->getParam('max_query_terms')); + } + + /** + * @group unit + */ + public function testSetPercentTermsToMatch() + { + $query = new MoreLikeThis(); + + $match = 0.8; + $query->setPercentTermsToMatch($match); + + $this->assertEquals($match, $query->getParam('percent_terms_to_match')); + } + + /** + * @group unit + */ + public function testSetMinimumShouldMatch() + { + $query = new MoreLikeThis(); + + $match = '80%'; + $query->setMinimumShouldMatch($match); + + $this->assertEquals($match, $query->getParam('minimum_should_match')); + } + + /** + * @group unit + */ + public function testSetMinDocFrequency() + { + $query = new MoreLikeThis(); + + $freq = 2; + $query->setMinDocFrequency($freq); + + $this->assertEquals($freq, $query->getParam('min_doc_freq')); + } + + /** + * @group unit + */ + public function testSetMaxDocFrequency() + { + $query = new MoreLikeThis(); + + $freq = 2; + $query->setMaxDocFrequency($freq); + + $this->assertEquals($freq, $query->getParam('max_doc_freq')); + } + + /** + * @group unit + */ + public function testSetMinWordLength() + { + $query = new MoreLikeThis(); + + $length = 4; + $query->setMinWordLength($length); + + $this->assertEquals($length, $query->getParam('min_word_length')); + } + + /** + * @group unit + */ + public function testSetMaxWordLength() + { + $query = new MoreLikeThis(); + + $length = 5; + $query->setMaxWordLength($length); + + $this->assertEquals($length, $query->getParam('max_word_length')); + } + + /** + * @group unit + */ + public function testSetBoostTerms() + { + $query = new MoreLikeThis(); + + $boost = false; + $query->setBoostTerms($boost); + + $this->assertEquals($boost, $query->getParam('boost_terms')); + } + + /** + * @group unit + */ + public function testSetAnalyzer() + { + $query = new MoreLikeThis(); + + $analyzer = 'UpperCase'; + $query->setAnalyzer($analyzer); + + $this->assertEquals($analyzer, $query->getParam('analyzer')); + } + + /** + * @group unit + */ + public function testSetStopWords() + { + $query = new MoreLikeThis(); + + $stopWords = array('no', 'yes', 'test'); + $query->setStopWords($stopWords); + + $this->assertEquals($stopWords, $query->getParam('stop_words')); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MultiMatchTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MultiMatchTest.php new file mode 100644 index 00000000..e32f91af --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/MultiMatchTest.php @@ -0,0 +1,214 @@ +<?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'), + ); + + /** + * @group functional + */ + public function testMinimumShouldMatch() + { + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Tristan Maindron'); + $multiMatch->setFields(array('full_name', 'name')); + $multiMatch->setMinimumShouldMatch('2<100%'); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testAndOperator() + { + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Monique Maindron'); + $multiMatch->setFields(array('full_name', 'name')); + $multiMatch->setOperator(MultiMatch::OPERATOR_AND); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testType() + { + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Trist'); + $multiMatch->setFields(array('full_name', 'name')); + $multiMatch->setType(MultiMatch::TYPE_PHRASE_PREFIX); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testFuzzy() + { + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Tritsan'); // Mispell on purpose + $multiMatch->setFields(array('full_name', 'name')); + $multiMatch->setFuzziness(2); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(1, $resultSet->count()); + + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Tritsan'); // Mispell on purpose + $multiMatch->setFields(array('full_name', 'name')); + $multiMatch->setFuzziness(0); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + public function testFuzzyWithOptions1() + { + // Here Elasticsearch will not accept mispells + // on the first 6 letters. + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Tritsan'); // Mispell on purpose + $multiMatch->setFields(array('full_name', 'name')); + $multiMatch->setFuzziness(2); + $multiMatch->setPrefixLength(6); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(0, $resultSet->count()); + } + + /** + * @group functional + */ + 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. + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('M'); + $multiMatch->setFields(array('name')); + $multiMatch->setType(MultiMatch::TYPE_PHRASE_PREFIX); + $multiMatch->setPrefixLength(0); + $multiMatch->setMaxExpansions(1); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group functional + */ + public function testZeroTerm() + { + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('not'); // This is a stopword. + $multiMatch->setFields(array('full_name', 'last_name')); + $multiMatch->setZeroTermsQuery(MultiMatch::ZERO_TERM_NONE); + $multiMatch->setAnalyzer('stops'); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(0, $resultSet->count()); + + $multiMatch->setZeroTermsQuery(MultiMatch::ZERO_TERM_ALL); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(4, $resultSet->count()); + } + + /** + * @group functional + */ + public function testBaseMultiMatch() + { + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Rodolfo'); + $multiMatch->setFields(array('name', 'last_name')); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(1, $resultSet->count()); + + $multiMatch = new MultiMatch(); + $multiMatch->setQuery('Moraes'); + $multiMatch->setFields(array('name', 'last_name')); + $resultSet = $this->_getResults($multiMatch); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * Executes the query with the current multimatch. + */ + private function _getResults(MultiMatch $multiMatch) + { + return $this->_generateIndex()->search(new Query($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; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/NestedTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/NestedTest.php new file mode 100644 index 00000000..1b5fc368 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/NestedTest.php @@ -0,0 +1,30 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Query\Nested; +use Elastica\Query\QueryString; +use Elastica\Test\Base as BaseTest; + +class NestedTest extends BaseTest +{ + /** + * @group unit + */ + public function testSetQuery() + { + $nested = new Nested(); + $path = 'test1'; + + $queryString = new QueryString('test'); + $this->assertInstanceOf('Elastica\Query\Nested', $nested->setQuery($queryString)); + $this->assertInstanceOf('Elastica\Query\Nested', $nested->setPath($path)); + $expected = array( + 'nested' => array( + 'query' => $queryString->toArray(), + 'path' => $path, + ), + ); + + $this->assertEquals($expected, $nested->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PostFilterTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PostFilterTest.php new file mode 100644 index 00000000..178c803e --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PostFilterTest.php @@ -0,0 +1,64 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Filter\Term; +use Elastica\Query; +use Elastica\Query\Match; +use Elastica\Test\Base as BaseTest; + +class PostFilterTest extends BaseTest +{ + protected function _getTestIndex() + { + $index = $this->_createIndex(); + $docs = array( + new Document(1, array('color' => 'green', 'make' => 'ford')), + new Document(2, array('color' => 'blue', 'make' => 'volvo')), + new Document(3, array('color' => 'red', 'make' => 'ford')), + new Document(4, array('color' => 'green', 'make' => 'renault')), + ); + $index->getType('test')->addDocuments($docs); + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $query = new Query(); + + $post_filter = new Term(array('color' => 'green')); + $query->setPostFilter($post_filter); + + $data = $query->toArray(); + + $this->assertArrayHasKey('post_filter', $data); + $this->assertEquals(array('term' => array('color' => 'green')), $data['post_filter']); + } + + /** + * @group functional + */ + public function testQuery() + { + $query = new Query(); + + $match = new Match(); + $match->setField('make', 'ford'); + + $query->setQuery($match); + + $filter = new Term(); + $filter->setTerm('color', 'green'); + + $query->setPostFilter($filter); + + $results = $this->_getTestIndex()->search($query); + + $this->assertEquals(1, $results->getTotalHits()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PrefixTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PrefixTest.php new file mode 100644 index 00000000..e12ead05 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/PrefixTest.php @@ -0,0 +1,27 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Query\Prefix; +use Elastica\Test\Base as BaseTest; + +class PrefixTest extends BaseTest +{ + /** + * @group unit + */ + public function testToArray() + { + $query = new Prefix(); + $key = 'name'; + $value = 'ni'; + $boost = 2; + $query->setPrefix($key, $value, $boost); + + $data = $query->toArray(); + + $this->assertInternalType('array', $data['prefix']); + $this->assertInternalType('array', $data['prefix'][$key]); + $this->assertEquals($data['prefix'][$key]['value'], $value); + $this->assertEquals($data['prefix'][$key]['boost'], $boost); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/QueryStringTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/QueryStringTest.php new file mode 100644 index 00000000..5b549a30 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/QueryStringTest.php @@ -0,0 +1,189 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query\QueryString; +use Elastica\Test\Base as BaseTest; + +class QueryStringTest extends BaseTest +{ + /** + * @group unit + */ + public function testSearchMultipleFields() + { + $str = md5(rand()); + $query = new QueryString($str); + + $expected = array( + 'query' => $str, + ); + + $this->assertEquals(array('query_string' => $expected), $query->toArray()); + + $fields = array(); + $max = rand() % 10 + 1; + for ($i = 0; $i < $max; $i++) { + $fields[] = md5(rand()); + } + + $query->setFields($fields); + $expected['fields'] = $fields; + $this->assertEquals(array('query_string' => $expected), $query->toArray()); + + foreach (array(false, true) as $val) { + $query->setUseDisMax($val); + $expected['use_dis_max'] = $val; + + $this->assertEquals(array('query_string' => $expected), $query->toArray()); + } + } + + /** + * @group functional + */ + public function testSearch() + { + $index = $this->_createIndex(); + $index->getSettings()->setNumberOfReplicas(0); + $type = $index->getType('helloworld'); + + $doc = new Document(1, array('email' => 'test@test.com', 'username' => 'hanswurst', 'test' => array('2', '3', '5'))); + $type->addDocument($doc); + $index->refresh(); + + $queryString = new QueryString('test*'); + $resultSet = $type->search($queryString); + + $this->assertEquals(1, $resultSet->count()); + } + + /** + * Tests if search in multiple fields is possible. + * + * @group functional + */ + public function testSearchFields() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $doc = new Document(1, array('title' => 'hello world', 'firstname' => 'nicolas', 'lastname' => 'ruflin', 'price' => '102', 'year' => '2012')); + $type->addDocument($doc); + $index->refresh(); + + $query = new QueryString(); + $query = $query->setQuery('ruf*'); + $query = $query->setDefaultField('title'); + $query = $query->setFields(array('title', 'firstname', 'lastname', 'price', 'year')); + + $resultSet = $type->search($query); + $this->assertEquals(1, $resultSet->count()); + } + + /** + * @group unit + */ + public function testSetDefaultOperator() + { + $operator = 'AND'; + $query = new QueryString('test'); + $query->setDefaultOperator($operator); + + $data = $query->toArray(); + + $this->assertEquals($data['query_string']['default_operator'], $operator); + } + + /** + * @group unit + */ + public function testSetDefaultField() + { + $default = 'field1'; + $query = new QueryString('test'); + $query->setDefaultField($default); + + $data = $query->toArray(); + + $this->assertEquals($data['query_string']['default_field'], $default); + } + + /** + * @group unit + */ + public function testSetRewrite() + { + $rewrite = 'scoring_boolean'; + $query = new QueryString('test'); + $query->setRewrite($rewrite); + + $data = $query->toArray(); + + $this->assertEquals($data['query_string']['rewrite'], $rewrite); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testSetQueryInvalid() + { + $query = new QueryString(); + $query->setQuery(array()); + } + + /** + * @group unit + */ + public function testSetTimezone() + { + $timezone = 'Europe/Paris'; + $text = 'date:[2012 TO 2014]'; + + $query = new QueryString($text); + $query->setTimezone($timezone); + + $expected = array( + 'query_string' => array( + 'query' => $text, + 'time_zone' => $timezone, + ), + ); + + $this->assertEquals($expected, $query->toArray()); + $this->assertInstanceOf('Elastica\Query\QueryString', $query->setTimezone($timezone)); + } + + /** + * @group unit + */ + public function testSetPhraseSlop() + { + $phraseSlop = 9; + + $query = new QueryString('test'); + $query->setPhraseSlop($phraseSlop); + + $data = $query->toArray(); + $this->assertEquals($phraseSlop, $data['query_string']['phrase_slop']); + } + + /** + * @group functional + */ + public function testSetBoost() + { + $index = $this->_createIndex(); + $query = new QueryString('test'); + $query->setBoost(9.3); + + $doc = new Document('', array('name' => 'test')); + $index->getType('test')->addDocument($doc); + $index->refresh(); + + $resultSet = $index->search($query); + + $this->assertEquals(1, $resultSet->count()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RangeTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RangeTest.php new file mode 100644 index 00000000..108ef0c9 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RangeTest.php @@ -0,0 +1,79 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query\Range; +use Elastica\Test\Base as BaseTest; + +class RangeTest extends BaseTest +{ + /** + * @group functional + */ + public function testQuery() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + $index->create(array(), true); + $type = $index->getType('test'); + + $type->addDocuments(array( + new Document(1, array('age' => 16, 'height' => 140)), + new Document(2, array('age' => 21, 'height' => 155)), + new Document(3, array('age' => 33, 'height' => 160)), + new Document(4, array('age' => 68, 'height' => 160)), + )); + + $index->optimize(); + $index->refresh(); + + $query = new Range('age', array('from' => 10, 'to' => 20)); + $result = $type->search($query)->count(); + $this->assertEquals(1, $result); + + $query = new Range(); + $query->addField('height', array('gte' => 160)); + + $result = $type->search($query)->count(); + $this->assertEquals(2, $result); + } + + /** + * @group unit + */ + public function testToArray() + { + $range = new Range(); + + $field = array('from' => 20, 'to' => 40); + $range->addField('age', $field); + + $expectedArray = array( + 'range' => array( + 'age' => $field, + ), + ); + + $this->assertEquals($expectedArray, $range->toArray()); + } + + /** + * @group unit + */ + public function testConstruct() + { + $ranges = array('from' => 20, 'to' => 40); + $range = new Range( + 'age', + $ranges + ); + + $expectedArray = array( + 'range' => array( + 'age' => $ranges, + ), + ); + + $this->assertEquals($expectedArray, $range->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RegexpTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RegexpTest.php new file mode 100644 index 00000000..c67e6317 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RegexpTest.php @@ -0,0 +1,31 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Query\Regexp; +use Elastica\Test\Base as BaseTest; + +class RegexpTest extends BaseTest +{ + /** + * @group unit + */ + public function testToArray() + { + $field = 'name'; + $value = 'ruf'; + $boost = 2; + + $query = new Regexp($field, $value, $boost); + + $expectedArray = array( + 'regexp' => array( + $field => array( + 'value' => $value, + 'boost' => $boost, + ), + ), + ); + + $this->assertequals($expectedArray, $query->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RescoreTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RescoreTest.php new file mode 100644 index 00000000..87842b0f --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/RescoreTest.php @@ -0,0 +1,236 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Query; +use Elastica\Query\Match; +use Elastica\Query\Term; +use Elastica\Rescore\Query as QueryRescore; +use Elastica\Test\Base as BaseTest; + +class RescoreTest extends BaseTest +{ + /** + * @group unit + */ + public function testToArray() + { + $query = new Query(); + $mainQuery = new Match(); + $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); + $secQuery = new Term(); + $secQuery = $secQuery->setTerm('test2', 'bar', 2); + $queryRescore = new QueryRescore($secQuery); + $query->setQuery($mainQuery); + $query->setRescore($queryRescore); + $data = $query->toArray(); + + $expected = array( + 'query' => array( + 'match' => array( + 'test1' => array( + 'query' => 'foo', + ), + ), + ), + 'rescore' => array( + 'query' => array( + 'rescore_query' => array( + 'term' => array( + 'test2' => array( + 'value' => 'bar', + 'boost' => 2, + ), + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $data); + } + + /** + * @group unit + */ + public function testSetSize() + { + $query = new Query(); + $mainQuery = new Match(); + $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); + $secQuery = new Term(); + $secQuery = $secQuery->setTerm('test2', 'bar', 2); + $queryRescore = new QueryRescore($secQuery); + $queryRescore->setWindowSize(50); + $query->setQuery($mainQuery); + $query->setRescore($queryRescore); + $data = $query->toArray(); + + $expected = array( + 'query' => array( + 'match' => array( + 'test1' => array( + 'query' => 'foo', + ), + ), + ), + 'rescore' => array( + 'window_size' => 50, + 'query' => array( + 'rescore_query' => array( + 'term' => array( + 'test2' => array( + 'value' => 'bar', + 'boost' => 2, + ), + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $data); + } + + /** + * @group unit + */ + public function testSetWeights() + { + $query = new Query(); + $mainQuery = new Match(); + $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); + $secQuery = new Term(); + $secQuery = $secQuery->setTerm('test2', 'bar', 2); + $queryRescore = new QueryRescore($secQuery); + $queryRescore->setWindowSize(50); + $queryRescore->setQueryWeight(.7); + $queryRescore->setRescoreQueryWeight(1.2); + $query->setQuery($mainQuery); + $query->setRescore($queryRescore); + $data = $query->toArray(); + + $expected = array( + 'query' => array( + 'match' => array( + 'test1' => array( + 'query' => 'foo', + ), + ), + ), + 'rescore' => array( + 'window_size' => 50, + 'query' => array( + 'rescore_query' => array( + 'term' => array( + 'test2' => array( + 'value' => 'bar', + 'boost' => 2, + ), + ), + ), + 'query_weight' => 0.7, + 'rescore_query_weight' => 1.2, + ), + ), + ); + + $this->assertEquals($expected, $data); + } + + /** + * @group functional + */ + public function testMultipleQueries() + { + $query = new Query(); + $mainQuery = new Match(); + $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); + + $secQuery1 = new Term(); + $secQuery1 = $secQuery1->setTerm('test2', 'bar', 1); + $rescoreQuery1 = new QueryRescore(); + $rescoreQuery1->setRescoreQuery($secQuery1); + + $secQuery2 = new Term(); + $secQuery2 = $secQuery2->setTerm('test2', 'tom', 2); + $rescoreQuery2 = new QueryRescore(); + $rescoreQuery2->setRescoreQuery($secQuery2); + + $query->setQuery($mainQuery); + $query->setRescore(array($rescoreQuery1, $rescoreQuery2)); + $data = $query->toArray(); + + $expected = array( + 'query' => array( + 'match' => array( + 'test1' => array( + 'query' => 'foo', + ), + ), + ), + 'rescore' => array( + array( + 'query' => array( + 'rescore_query' => array( + 'term' => array( + 'test2' => array( + 'value' => 'bar', + 'boost' => 1, + ), + ), + ), + ), + ), + array( + 'query' => array( + 'rescore_query' => array( + 'term' => array( + 'test2' => array( + 'value' => 'tom', + 'boost' => 2, + ), + ), + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $data); + + $index = $this->_createIndex(); + $index->refresh(); + $results = $index->search($query); + $response = $results->getResponse(); + + $this->assertEquals(true, $response->isOk()); + $this->assertEquals(0, $results->getTotalHits()); + } + + /** + * @group functional + */ + public function testQuery() + { + $query = new Query(); + $mainQuery = new Match(); + $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); + $secQuery = new Term(); + $secQuery = $secQuery->setTerm('test2', 'bar', 2); + $queryRescore = new QueryRescore($secQuery); + $queryRescore->setWindowSize(50); + $queryRescore->setQueryWeight(.7); + $queryRescore->setRescoreQueryWeight(1.2); + $query->setQuery($mainQuery); + $query->setRescore($queryRescore); + $data = $query->toArray(); + + $index = $this->_createIndex(); + $index->refresh(); + $results = $index->search($query); + $response = $results->getResponse(); + + $this->assertEquals(true, $response->isOk()); + $this->assertEquals(0, $results->getTotalHits()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleQueryStringTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleQueryStringTest.php new file mode 100644 index 00000000..80316547 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleQueryStringTest.php @@ -0,0 +1,103 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query\SimpleQueryString; +use Elastica\Test\Base; + +class SimpleQueryStringTest extends Base +{ + /** + * @group unit + */ + public function testToArray() + { + $string = 'this is a test'; + $fields = array('field1', 'field2'); + $query = new SimpleQueryString($string, $fields); + $query->setDefaultOperator(SimpleQueryString::OPERATOR_OR); + $query->setAnalyzer('whitespace'); + + $expected = array( + 'simple_query_string' => array( + 'query' => $string, + 'fields' => $fields, + 'analyzer' => 'whitespace', + 'default_operator' => SimpleQueryString::OPERATOR_OR, + ), + ); + + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group functional + */ + public function testQuery() + { + $index = $this->_createIndex(); + $docs = array( + new Document(1, array('make' => 'Gibson', 'model' => 'Les Paul')), + new Document(2, array('make' => 'Gibson', 'model' => 'SG Standard')), + new Document(3, array('make' => 'Gibson', 'model' => 'SG Supreme')), + new Document(4, array('make' => 'Gibson', 'model' => 'SG Faded')), + new Document(5, array('make' => 'Fender', 'model' => 'Stratocaster')), + ); + $index->getType('guitars')->addDocuments($docs); + $index->refresh(); + + $query = new SimpleQueryString('gibson +sg +-faded', array('make', 'model')); + $results = $index->search($query); + + $this->assertEquals(2, $results->getTotalHits()); + + $query->setFields(array('model')); + $results = $index->search($query); + + // We should not get any hits, since the "make" field was not included in the query. + $this->assertEquals(0, $results->getTotalHits()); + } + + /** + * @group unit + */ + public function testSetMinimumShouldMatch() + { + $expected = array( + 'simple_query_string' => array( + 'query' => 'DONT PANIC', + 'minimum_should_match' => '75%', + ), + ); + + $query = new SimpleQueryString($expected['simple_query_string']['query']); + $query->setMinimumShouldMatch($expected['simple_query_string']['minimum_should_match']); + + $this->assertEquals($expected, $query->toArray()); + $this->assertInstanceOf('Elastica\Query\SimpleQueryString', $query->setMinimumShouldMatch('75%')); + } + + /** + * @group functional + */ + public function testSetMinimumShouldMatchWorks() + { + $index = $this->_createIndex(); + $type = $index->getType('foobars'); + $type->addDocuments(array( + new Document(1, array('body' => 'foo')), + new Document(2, array('body' => 'bar')), + new Document(3, array('body' => 'foo bar')), + new Document(4, array('body' => 'foo baz bar')), + )); + $index->refresh(); + + $query = new SimpleQueryString('foo bar'); + $query->setMinimumShouldMatch(2); + $results = $type->search($query); + + $this->assertCount(2, $results); + $this->assertEquals(3, $results[0]->getId()); + $this->assertEquals(4, $results[1]->getId()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleTest.php new file mode 100644 index 00000000..b4f1dc8d --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleTest.php @@ -0,0 +1,19 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Query\Simple; +use Elastica\Test\Base as BaseTest; + +class SimpleTest extends BaseTest +{ + /** + * @group unit + */ + public function testToArray() + { + $testQuery = array('hello' => array('world'), 'name' => 'ruflin'); + $query = new Simple($testQuery); + + $this->assertEquals($testQuery, $query->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/TermTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/TermTest.php new file mode 100644 index 00000000..da15f0f2 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/TermTest.php @@ -0,0 +1,27 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Query\Term; +use Elastica\Test\Base as BaseTest; + +class TermTest extends BaseTest +{ + /** + * @group unit + */ + public function testToArray() + { + $query = new Term(); + $key = 'name'; + $value = 'nicolas'; + $boost = 2; + $query->setTerm($key, $value, $boost); + + $data = $query->toArray(); + + $this->assertInternalType('array', $data['term']); + $this->assertInternalType('array', $data['term'][$key]); + $this->assertEquals($data['term'][$key]['value'], $value); + $this->assertEquals($data['term'][$key]['boost'], $boost); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/TermsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/TermsTest.php new file mode 100644 index 00000000..f0c2b4b4 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/TermsTest.php @@ -0,0 +1,65 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query\Terms; +use Elastica\Test\Base as BaseTest; + +class TermsTest extends BaseTest +{ + /** + * @group functional + */ + public function testFilteredSearch() + { + $index = $this->_createIndex(); + $type = $index->getType('helloworld'); + + $type->addDocuments(array( + new Document(1, array('name' => 'hello world')), + new Document(2, array('name' => 'nicolas ruflin')), + new Document(3, array('name' => 'ruflin')), + )); + + $query = new Terms(); + $query->setTerms('name', array('nicolas', 'hello')); + + $index->refresh(); + + $resultSet = $type->search($query); + + $this->assertEquals(2, $resultSet->count()); + + $query->addTerm('ruflin'); + $resultSet = $type->search($query); + + $this->assertEquals(3, $resultSet->count()); + } + + /** + * @group unit + */ + public function testSetMinimum() + { + $key = 'name'; + $terms = array('nicolas', 'ruflin'); + $minimum = 2; + + $query = new Terms($key, $terms); + $query->setMinimumMatch($minimum); + + $data = $query->toArray(); + $this->assertEquals($minimum, $data['terms']['minimum_match']); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\InvalidException + */ + public function testInvalidParams() + { + $query = new Terms(); + + $query->toArray(); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/WildcardTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/WildcardTest.php new file mode 100644 index 00000000..7a0c379e --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/WildcardTest.php @@ -0,0 +1,106 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Query\Wildcard; +use Elastica\Test\Base as BaseTest; +use Elastica\Type\Mapping; + +class WildcardTest extends BaseTest +{ + /** + * @group unit + */ + public function testConstructEmpty() + { + $wildcard = new Wildcard(); + $this->assertEmpty($wildcard->getParams()); + } + + /** + * @group unit + */ + public function testToArray() + { + $key = 'name'; + $value = 'Ru*lin'; + $boost = 2.0; + + $wildcard = new Wildcard($key, $value, $boost); + + $expectedArray = array( + 'wildcard' => array( + $key => array( + 'value' => $value, + 'boost' => $boost, + ), + ), + ); + + $this->assertEquals($expectedArray, $wildcard->toArray()); + } + + /** + * @group functional + */ + public function testSearchWithAnalyzer() + { + $client = $this->_getClient(); + $index = $client->getIndex('test'); + + $indexParams = array( + 'analysis' => array( + 'analyzer' => array( + 'lw' => array( + 'type' => 'custom', + 'tokenizer' => 'keyword', + 'filter' => array('lowercase'), + ), + ), + ), + ); + + $index->create($indexParams, true); + $type = $index->getType('test'); + + $mapping = new Mapping($type, array( + 'name' => array('type' => 'string', 'store' => 'no', 'analyzer' => 'lw'), + ) + ); + $type->setMapping($mapping); + + $type->addDocuments(array( + new Document(1, array('name' => 'Basel-Stadt')), + new Document(2, array('name' => 'New York')), + new Document(3, array('name' => 'Baden')), + new Document(4, array('name' => 'Baden Baden')), + new Document(5, array('name' => 'New Orleans')), + )); + + $index->refresh(); + + $query = new Wildcard(); + $query->setValue('name', 'ba*'); + $resultSet = $index->search($query); + + $this->assertEquals(3, $resultSet->count()); + + $query = new Wildcard(); + $query->setValue('name', 'baden*'); + $resultSet = $index->search($query); + + $this->assertEquals(2, $resultSet->count()); + + $query = new Wildcard(); + $query->setValue('name', 'baden b*'); + $resultSet = $index->search($query); + + $this->assertEquals(1, $resultSet->count()); + + $query = new Wildcard(); + $query->setValue('name', 'baden bas*'); + $resultSet = $index->search($query); + + $this->assertEquals(0, $resultSet->count()); + } +} |