diff --git a/.github/assets/SearchIndex.php.test b/.github/assets/SearchIndex.php.test deleted file mode 100644 index 79df15b..0000000 --- a/.github/assets/SearchIndex.php.test +++ /dev/null @@ -1,12 +0,0 @@ -query = $query; $this->elasticResult = $result; $resultArray = $result->asArray(); - $result = $result->asObject(); + $resultObject = $result->asObject(); - $this->setMatches($result->hits->hits) + $this->setMatches($resultObject->hits->hits) ->setSpellcheck($resultArray['suggest'] ?? []) - ->setTotalItems($result->hits->total->value); - if (property_exists($result, 'aggregations')) { - $this->setFacets($result->aggregations); + ->setTotalItems($resultObject->hits->total->value); + if (property_exists($resultObject, 'aggregations')) { + $this->setFacets($resultObject->aggregations); } } @@ -87,7 +87,7 @@ public function __construct(Elasticsearch $result, ElasticQuery $query, ElasticI * @param array $spellcheck * @return SearchResult */ - private function setSpellcheck($spellcheck): self + protected function setSpellcheck($spellcheck): self { $spellcheckList = []; diff --git a/src/Traits/IndexTraits/BaseIndexTrait.php b/src/Traits/IndexTraits/BaseIndexTrait.php index baebae7..15f9046 100644 --- a/src/Traits/IndexTraits/BaseIndexTrait.php +++ b/src/Traits/IndexTraits/BaseIndexTrait.php @@ -247,8 +247,9 @@ public function addAllDateFields() */ public function setFacetFields($fields) { + $this->facetFields = $fields; foreach ($fields as $field => $option) { - $this->addFacetField($field, $option); + $this->addFulltextField($option['Field']); } return $this; diff --git a/tests/E2E/EndToEndTest.php b/tests/E2E/EndToEndTest.php index ac5758c..04b2f4c 100644 --- a/tests/E2E/EndToEndTest.php +++ b/tests/E2E/EndToEndTest.php @@ -20,7 +20,7 @@ public function testSearching() /** @var ElasticIndex $index */ $index = new SearchIndex(); $query = new ElasticQuery(); - $query->addTerm('Silverstripe'); + $query->addTerm('Home'); $results = $index->doSearch($query); $this->assertArrayHasKey('index', $index->getClientQuery()); $this->assertGreaterThan(0, $results->getTotalItems()); diff --git a/tests/Fixtures/elasticresponse.json b/tests/Fixtures/elasticresponse.json new file mode 100644 index 0000000..d16bdd7 --- /dev/null +++ b/tests/Fixtures/elasticresponse.json @@ -0,0 +1,107 @@ +{ + "took": 1, + "timed_out": false, + "_shards": { + "total": 1, + "successful": 1, + "skipped": 0, + "failed": 0 + }, + "hits": { + "total": { + "value": 2, + "relation": "eq" + }, + "max_score": 1.2213857, + "hits": [ + { + "_index": "search-testindex", + "_id": "TestPage-6-de940140432a11cd1e4ec6c0eadb9d87", + "_score": 1.2213857, + "_source": { + "SiteTree.Title": "Test page", + "SiteTree.Created": "2023-10-18 07:58:28", + "SiteTree.Content": null, + "Page.TestObject.Title": null, + "Page.TestObject.TestRelation.Title": [], + "Page.RelationObject.Title": [ + null + ], + "_text": "Test page, 2023-10-18 07:58:28, , , , ", + "id": "TestPage-6", + "ObjectID": 6, + "UniqueKey": "TestPage-6-de940140432a11cd1e4ec6c0eadb9d87", + "ClassName": "TestPage", + "ClassHierarchy": [ + "SilverStripe\\View\\ViewableData", + "SilverStripe\\ORM\\DataObject", + "SilverStripe\\CMS\\Model\\SiteTree", + "Page", + "TestPage" + ], + "ViewStatus": [ + "null" + ], + "_extract_binary_content": true, + "_reduce_whitespace": true, + "_run_ml_inference": false + } + }, + { + "_index": "search-testindex", + "_id": "TestPage-7-c0b86d17ae9bf14a86b3821b15ff9bb7", + "_score": 1.1700189, + "_source": { + "SiteTree.Title": "Test page", + "SiteTree.Created": "2023-10-18 08:08:47", + "SiteTree.Content": null, + "Page.TestObject.Title": "TestObject", + "Page.TestObject.TestRelation.Title": [], + "Page.RelationObject.Title": [ + "TestRelationObject" + ], + "_text": "Test page, 2023-10-18 08:08:47, , TestObject, , TestRelationObject", + "id": "TestPage-7", + "ObjectID": 7, + "UniqueKey": "TestPage-7-c0b86d17ae9bf14a86b3821b15ff9bb7", + "ClassName": "TestPage", + "ClassHierarchy": [ + "SilverStripe\\View\\ViewableData", + "SilverStripe\\ORM\\DataObject", + "SilverStripe\\CMS\\Model\\SiteTree", + "Page", + "TestPage" + ], + "ViewStatus": [ + "null" + ], + "_extract_binary_content": true, + "_reduce_whitespace": true, + "_run_ml_inference": false + } + } + ] + }, + "aggregations": { + "TestObject": { + "doc_count_error_upper_bound": 0, + "sum_other_doc_count": 0, + "buckets": [ + { + "key": "TestObject", + "doc_count": 1 + } + ] + } + }, + "suggest": { + "0-fullterm": [ + { + "text": "test", + "offset": 0, + "length": 4, + "options": [] + } + ] + } +} \ No newline at end of file diff --git a/tests/unit/Indexes/ElasticIndexTest.php b/tests/unit/Indexes/ElasticIndexTest.php index a04bd88..cda4f8a 100644 --- a/tests/unit/Indexes/ElasticIndexTest.php +++ b/tests/unit/Indexes/ElasticIndexTest.php @@ -2,11 +2,11 @@ namespace Firesphere\ElasticSearch\Tests; +use App\src\SearchIndex; use Elastic\Elasticsearch\Client; use Firesphere\ElasticSearch\Indexes\ElasticIndex; use SilverStripe\Core\Config\Config; use SilverStripe\Dev\SapphireTest; -use App\src\SearchIndex; class ElasticIndexTest extends SapphireTest { @@ -57,13 +57,7 @@ public function testAddSetGet() $conf = $this->indexConfig; $conf['FulltextFields'] = array_merge( $conf['FulltextFields'] ?? [], - $conf['FilterFields'] ?? [], - $conf['SortFields'] ?? [] ); - $conf['FacetFields'] = $conf['FacetFields'] ?? []; - foreach ($conf['FacetFields'] as $field => $options) { - $conf['FulltextFields'][] = $options['Field']; - } $this->assertEquals($conf['FulltextFields'], $index->getFulltextFields()); $index->addFulltextField('Dummyfield'); $conf['FulltextFields'][] = 'Dummyfield'; @@ -88,8 +82,14 @@ public function testAddSetGet() $this->assertContains('FieldC', $index->getFulltextFields()); - // No facets set yet - $this->assertEquals([], $index->getFacetFields()); + $expectedFacets = [ + 'TestObject' => [ + 'BaseClass' => 'Page', + 'Field' => 'TestObject.Title', + 'Title' => 'TestObject', + ] + ]; + $this->assertEquals($expectedFacets, $index->getFacetFields()); $index->setFacetFields([\Page::class => ['Field' => 'MyContent']]); $this->assertEquals([\Page::class => ['Field' => 'MyContent']], $index->getFacetFields()); diff --git a/tests/unit/Queries/QueryBuilderTest.php b/tests/unit/Queries/QueryBuilderTest.php index 9ace48c..bec57d5 100644 --- a/tests/unit/Queries/QueryBuilderTest.php +++ b/tests/unit/Queries/QueryBuilderTest.php @@ -16,7 +16,7 @@ class QueryBuilderTest extends SapphireTest 'from' => 0, 'size' => 20, 'body' => [ - 'query' => [ + 'query' => [ 'bool' => [ 'must' => [ [ @@ -57,13 +57,20 @@ class QueryBuilderTest extends SapphireTest ] ] ], - 'suggest' => [ + 'suggest' => [ '0-fullterm' => [ 'text' => 'TestSearch', 'term' => [ 'field' => '_text' ] ] + ], + 'aggs' => [ + 'TestObject' => [ + 'terms' => [ + 'field' => 'Page.TestObject.Title.keyword' + ] + ] ] ] ]; diff --git a/tests/unit/Results/SearchResultTest.php b/tests/unit/Results/SearchResultTest.php new file mode 100644 index 0000000..b68ab35 --- /dev/null +++ b/tests/unit/Results/SearchResultTest.php @@ -0,0 +1,58 @@ + 'Test page']); + $testObj = new TestObject(['Title' => 'TestObject']); + $testObj->write(); + $testObject = new TestRelationObject(['Title' => 'TestRelationObject']); + $testObject->write(); + $testPage->TestObjectID = $testObj->ID; + $testPage->write(); + $testPage->RelationObject()->add($testObject); + $testPage->publishRecursive(); + parent::setUp(); + } + + public function testCreateViewableData() + { + $requestResponse = new Response( + 200, + [ + Elasticsearch::HEADER_CHECK => Elasticsearch::PRODUCT_NAME, + 'Content-Type' => 'application/json' + ], + file_get_contents(__DIR__ . '/../../Fixtures/elasticresponse.json') + ); + + $response = new Elasticsearch(); + $response->setResponse($requestResponse); + + $result = new SearchResult($response, new ElasticQuery(), new SearchIndex()); + + $this->assertInstanceOf(ArrayList::class, $result->getMatches()); + $this->assertInstanceOf(ArrayData::class, $result->getFacets()); + /** @var ArrayList $testFacet */ + $testFacet = $result->getFacets()->TestObject; + $this->assertEquals(1, $testFacet->count()); + $this->assertInstanceOf(TestObject::class, $testFacet->first()); + } +} \ No newline at end of file