Source of file ElasticSearcherUnitTest.php
Size: 9,485 Bytes - Last Modified: 2021-12-23T10:58:34+00:00
/var/www/docs.ssmods.com/process/src/tests/long/ElasticSearcherUnitTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 | <?php use SilverStripe\Elastica\ElasticaSearcher; class ElasticaSearcherUnitTest extends ElasticsearchTestBase { public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml'; public static $ignoreFixtureFileFor = array('testResultsForEmptySearch'); public function setUp() { parent::setUp(); } public function tearDown() { parent::tearDown(); } public function testSuggested() { $es = new ElasticaSearcher(); $locale = \i18n::default_locale(); $es->setLocale($locale); $es->setClasses('FlickrPhotoTO'); $fields = array('Title' => 1, 'Description' => 1); $results = $es->search('New Zealind', $fields, true); $this->assertEquals(100, $results->getTotalItems()); $this->assertEquals('New Zealand', $es->getSuggestedQuery()); } public function testResultsForEmptySearch() { $es = new ElasticaSearcher(); $es->hideResultsForEmptySearch(); $this->assertFalse($es->getShowResultsForEmptySearch()); $es->showResultsForEmptySearch(); $this->assertTrue($es->getShowResultsForEmptySearch()); } public function testMoreLikeThisSinglePhoto() { $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); $es = new ElasticaSearcher(); $locale = \i18n::default_locale(); $es->setLocale($locale); $es->setClasses('FlickrPhotoTO'); $fields = array('Description.standard' => 1, 'Title.standard' => 1); $results = $es->moreLikeThis($fp, $fields, true); $terms = $results->getList()->MoreLikeThisTerms; $fieldNamesReturned = array_keys($terms); $fieldNames = array_keys($fields); sort($fieldNames); sort($fieldNamesReturned); $this->assertEquals($fieldNames, $fieldNamesReturned); //FIXME - this seems anomolyous, check in more detail $expected = array('texas'); $this->assertEquals($expected, $terms['Title.standard']); $expected = array('collection', 'company', 'degolyer', 'everett', 'file', 'high', 'information', 'new', 'orleans', 'pacific', 'photographs', 'railroad', 'resolution', 'see', 'southern', 'texas', 'view', ); $actual = $terms['Description.standard']; sort($expected); sort($actual); $this->assertEquals($expected, $actual); } public function testSimilarNoWeighting() { $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); $es = new ElasticaSearcher(); $es->setClasses('FlickrPhotoTO'); $fields = array('Title.standard', 'Description.standard'); try { $paginated = $es->moreLikeThis($fp, $fields, true); $this->fail('Query has no weight and thus should have failed'); } catch (InvalidArgumentException $e) { $this->assertEquals('Fields must be of the form fieldname => weight', $e->getMessage()); } } public function testSimilarWeightingNotNumeric() { $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); $es = new ElasticaSearcher(); $es->setClasses('FlickrPhotoTO'); $fields = array('Title.standard' => 4, 'Description.standard' => 'not numeric'); try { $paginated = $es->moreLikeThis($fp, $fields, true); $this->fail('Query has non numeric weight and thus should have failed'); } catch (InvalidArgumentException $e) { $this->assertEquals('Fields must be of the form fieldname => weight', $e->getMessage()); } } public function testSimilarToNonSearchable() { $m = Member::get()->first(); // this is not by default Searchable $es = new ElasticaSearcher(); $es->setClasses('FlickrPhotoTO'); $fields = array('Title.standard' => 4, 'Description.standard' => 2); try { $paginated = $es->moreLikeThis($m, $fields, true); $this->fail('Querying for a non searchable object, thus should have failed'); } catch (InvalidArgumentException $e) { $this->assertEquals('Objects of class Member are not searchable', $e->getMessage()); } } public function testSimilarGood() { $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); $es = new ElasticaSearcher(); $es->setClasses('FlickrPhotoTO'); $fields = array('Title.standard' => 1, 'Description.standard' => 1); $paginated = $es->moreLikeThis($fp, $fields, true); $results = $paginated->getList()->toArray(); // FIXME - this test appears fragile due to sharding issues with more like this $ctr = 0; if ($ctr < 9) { $this->assertStringStartsWith( '[Texas and New Orleans, Southern Pacific', $results[$ctr]->Title ); ++$ctr; } } // if this is not set to unbounded, zero, a conditional is triggered to add max doc freq to the request /** } **/ public function testSimilarNullFields() { $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); $es = new ElasticaSearcher(); $es->setClasses('FlickrPhotoTO'); try { $paginated = $es->moreLikeThis($fp, null, true); } catch (InvalidArgumentException $e) { $this->assertEquals('Fields cannot be null', $e->getMessage()); } } public function testSimilarNullItem() { $es = new ElasticaSearcher(); $es->setClasses('FlickrPhotoTO'); $fields = array('Title.standard' => 1, 'Description.standard' => 1); try { $paginated = $es->moreLikeThis(null, $fields, true); } catch (InvalidArgumentException $e) { $this->assertEquals('A searchable item cannot be null', $e->getMessage()); } } public function testHighlightsAsIfCMSEdited() { $es = new ElasticaSearcher(); $locale = \i18n::default_locale(); $es->setLocale($locale); $es->setClasses('FlickrPhotoTO'); $filter = array('ClazzName' => 'FlickrPhotoTO', 'Name' => 'Title'); $titleField = SearchableField::get()->filter($filter)->first(); $titleField->ShowHighlights = true; $titleField->write(); $filter = array('ClazzName' => 'FlickrPhotoTO', 'Name' => 'Description'); $nameField = SearchableField::get()->filter($filter)->first(); $nameField->ShowHighlights = true; $nameField->write(); $fields = array('Title' => 1, 'Description' => 1); $query = 'New Zealand'; $paginated = $es->search($query, $fields); $ctr = 0; foreach ($paginated->getList()->toArray() as $result) { ++$ctr; foreach ($result->SearchHighlightsByField->Description_standard->getIterator() as $highlight) { $snippet = $highlight->Snippet; $snippet = strtolower($snippet); $wordFound = false; $lcquery = explode(' ', strtolower($query)); foreach ($lcquery as $part) { $bracketed = '<strong class="hl">'.$part.'</strong>'; if (strpos($snippet, $bracketed) > 0) { $wordFound = true; } } $this->assertTrue($wordFound, 'Highlight should have been found'); } } } public function testHighlightPassingFields() { $es = new ElasticaSearcher(); $es->setClasses('FlickrPhotoTO'); $es->setHighlightedFields(array('Title', 'Title.standard', 'Description')); $fields = array('Title' => 1, 'Description' => 1); $query = 'New Zealand'; $paginated = $es->search($query, $fields); $ctr = 0; foreach ($paginated->getList()->toArray() as $result) { ++$ctr; foreach ($result->SearchHighlightsByField->Description->getIterator() as $highlight) { $snippet = $highlight->Snippet; $snippet = strtolower($snippet); $wordFound = false; $lcquery = explode(' ', strtolower($query)); foreach ($lcquery as $part) { $bracketed = '<strong class="hl">'.$part.'</strong>'; if (strpos($snippet, $bracketed) > 0) { $wordFound = true; } } $this->assertTrue($wordFound, 'Highlight should have been found'); } } } public function testAutoCompleteGood() { $es = new ElasticaSearcher(); $es->setClasses('FlickrPhotoTO'); $fields = array('Title' => 1, 'Description' => 1); $query = 'Lond'; $results = $es->autocomplete_search($query, 'Title'); $this->assertEquals(7, $results->getTotalItems()); foreach ($results->toArray() as $result) { $this->assertTrue(strpos($result->Title, $query) > 0); } } private function makeCode($paginated) { $results = $paginated->getList()->toArray(); $ctr = 0; echo '$result = $paginated->getList()->toArray();'."\n"; foreach ($results as $result) { echo '$this->assertEquals("'.$result->Title.'", $results['.$ctr.']->Title);'."\n"; ++$ctr; } } } |