Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some standard tests and faceted responses #57

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .github/assets/SearchIndex.php.test

This file was deleted.

10 changes: 0 additions & 10 deletions .github/assets/search.yml.test

This file was deleted.

4 changes: 1 addition & 3 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ jobs:
steps:
- uses: actions/checkout@v1
- uses: ddev/github-action-setup-ddev@v1
# example: composer install
- run: git clone https://github.com/Firesphere/search-testapp.git app
- run: ddev composer install --prefer-dist --no-interaction --no-progress
- run: ddev composer vendor-expose
- run: cp .github/assets/search.yml.test app/_config/search.yml
- run: cp .github/assets/SearchIndex.php.test app/src/SearchIndex.php
- run: cp example.env .env
- run: ddev sake dev/build
- run: ddev xdebug
Expand Down
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":1,"defects":{"Firesphere\\ElasticSearch\\Tests\\EndToEndTest::testSearching":4,"Firesphere\\ElasticSearch\\Extensions\\DataObjectElasticExtensionTest::testCreatingObject":4,"Firesphere\\ElasticSearch\\Extensions\\DataObjectElasticExtensionTest::testOnAfterDelete":4,"Firesphere\\ElasticSearch\\Queries\\QueryBuilderTest::testBuildQuery":4,"Firesphere\\ElasticSearch\\Tests\\ElasticCoreServiceTest::testUpdateIndex":4},"times":{"Firesphere\\ElasticSearch\\Tests\\EndToEndTest::testSearching":0.005,"Firesphere\\ElasticSearch\\Extensions\\DataObjectElasticExtensionTest::testCreatingObject":2.175,"Firesphere\\ElasticSearch\\Extensions\\DataObjectElasticExtensionTest::testOnAfterDelete":2.074,"Firesphere\\ElasticSearch\\Tests\\SynonymSetTest::testRequireDefaultRecords":1.764,"Firesphere\\ElasticSearch\\Queries\\QueryBuilderTest::testBuildQuery":0.002,"Firesphere\\ElasticSearch\\Tests\\ElasticCoreServiceTest::testConstruct":0.121,"Firesphere\\ElasticSearch\\Tests\\ElasticCoreServiceTest::testUpdateIndex":0.132,"Firesphere\\ElasticSearch\\Tasks\\ConfigureSynonymsTaskTest::testRun":0.482}}
{"version":1,"defects":{"Firesphere\\ElasticSearch\\Tests\\EndToEndTest::testSearching":4,"Firesphere\\ElasticSearch\\Extensions\\DataObjectElasticExtensionTest::testCreatingObject":4,"Firesphere\\ElasticSearch\\Extensions\\DataObjectElasticExtensionTest::testOnAfterDelete":1,"Firesphere\\ElasticSearch\\Queries\\QueryBuilderTest::testBuildQuery":4,"Firesphere\\ElasticSearch\\Tests\\ElasticCoreServiceTest::testUpdateIndex":1,"Firesphere\\ElasticSearch\\Tests\\SearchResultTest::testCreateViewableData":4,"Firesphere\\ElasticSearch\\Extensions\\DataObjectElasticExtensionTest::testAddRemoveFromElastic":1,"Firesphere\\ElasticSearch\\Extensions\\DataObjectElasticExtensionTest::testShouldPush":1,"Firesphere\\ElasticSearch\\Tests\\ElasticIndexTest::testConstruct":4,"Firesphere\\ElasticSearch\\Tests\\ElasticIndexTest::testInit":1,"Firesphere\\ElasticSearch\\Tests\\ElasticIndexTest::testAddSetGet":1,"Firesphere\\ElasticSearch\\Tests\\ElasticIndexTest::testAddAllFields":1,"Firesphere\\ElasticSearch\\Tests\\SynonymSetTest::testRequireDefaultRecords":4,"Firesphere\\ElasticSearch\\Tests\\ElasticQueryTest::testTerms":4,"Firesphere\\ElasticSearch\\Tests\\ElasticCoreServiceTest::testConstruct":4,"Firesphere\\ElasticSearch\\Tasks\\ConfigureSynonymsTaskTest::testRun":4,"Firesphere\\ElasticSearch\\Tasks\\ElasticIndexTaskTest::testConstruct":4,"Firesphere\\ElasticSearch\\Tasks\\ElasticIndexTaskTest::testRun":1},"times":{"Firesphere\\ElasticSearch\\Tests\\EndToEndTest::testSearching":0,"Firesphere\\ElasticSearch\\Extensions\\DataObjectElasticExtensionTest::testCreatingObject":0,"Firesphere\\ElasticSearch\\Extensions\\DataObjectElasticExtensionTest::testOnAfterDelete":0,"Firesphere\\ElasticSearch\\Tests\\SynonymSetTest::testRequireDefaultRecords":0,"Firesphere\\ElasticSearch\\Queries\\QueryBuilderTest::testBuildQuery":0,"Firesphere\\ElasticSearch\\Tests\\ElasticCoreServiceTest::testConstruct":0,"Firesphere\\ElasticSearch\\Tests\\ElasticCoreServiceTest::testUpdateIndex":0,"Firesphere\\ElasticSearch\\Tasks\\ConfigureSynonymsTaskTest::testRun":0,"Firesphere\\ElasticSearch\\Tests\\SearchResultTest::testCreateViewableData":0,"Firesphere\\ElasticSearch\\Extensions\\DataObjectElasticExtensionTest::testAddRemoveFromElastic":0,"Firesphere\\ElasticSearch\\Extensions\\DataObjectElasticExtensionTest::testShouldPush":0,"Firesphere\\ElasticSearch\\Tests\\ElasticIndexTest::testConstruct":0,"Firesphere\\ElasticSearch\\Tests\\ElasticIndexTest::testInit":0,"Firesphere\\ElasticSearch\\Tests\\ElasticIndexTest::testAddSetGet":0,"Firesphere\\ElasticSearch\\Tests\\ElasticIndexTest::testAddAllFields":0,"Firesphere\\ElasticSearch\\Tests\\ElasticQueryTest::testTerms":0,"Firesphere\\ElasticSearch\\Tasks\\ElasticIndexTaskTest::testConstruct":0,"Firesphere\\ElasticSearch\\Tasks\\ElasticIndexTaskTest::testRun":0}}
12 changes: 6 additions & 6 deletions src/Results/SearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ public function __construct(Elasticsearch $result, ElasticQuery $query, ElasticI
$this->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);
}
}

Expand All @@ -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 = [];

Expand Down
3 changes: 2 additions & 1 deletion src/Traits/IndexTraits/BaseIndexTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/E2E/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
107 changes: 107 additions & 0 deletions tests/Fixtures/elasticresponse.json
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}
}
18 changes: 9 additions & 9 deletions tests/unit/Indexes/ElasticIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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';
Expand All @@ -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());
Expand Down
11 changes: 9 additions & 2 deletions tests/unit/Queries/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class QueryBuilderTest extends SapphireTest
'from' => 0,
'size' => 20,
'body' => [
'query' => [
'query' => [
'bool' => [
'must' => [
[
Expand Down Expand Up @@ -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'
]
]
]
]
];
Expand Down
58 changes: 58 additions & 0 deletions tests/unit/Results/SearchResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Firesphere\ElasticSearch\Tests;

use App\src\SearchIndex;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Firesphere\ElasticSearch\Queries\ElasticQuery;
use Firesphere\ElasticSearch\Results\SearchResult;
use Firesphere\ElasticSearch\Tasks\ElasticIndexTask;
use GuzzleHttp\Psr7\Response;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
use TestObject;
use TestPage;
use TestRelationObject;

class SearchResultTest extends SapphireTest
{
protected $usesDatabase = true;
protected function setUp(): void
{
$testPage = new TestPage(['Title' => '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());
}
}