Skip to content

Commit c668bcd

Browse files
committed
schema suggester, suggest result, suggest hydrator; fixing select multi class query issue;
1 parent 492f0e4 commit c668bcd

16 files changed

+405
-66
lines changed

Exception/SuggestResultException.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Mdiyakov\DoctrineSolrBundle\Exception;
4+
5+
class SuggestResultException extends \RuntimeException
6+
{
7+
8+
}

Manager/SuggesterManager.php

+30-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Mdiyakov\DoctrineSolrBundle\Config\Config;
66
use Mdiyakov\DoctrineSolrBundle\Query\SuggestQueryBuilder;
77
use Mdiyakov\DoctrineSolrBundle\Suggester\ClassSuggester;
8+
use Mdiyakov\DoctrineSolrBundle\Suggester\SchemaSuggester;
89

910
class SuggesterManager
1011
{
@@ -13,6 +14,11 @@ class SuggesterManager
1314
*/
1415
private $classSuggesters = [];
1516

17+
/**
18+
* @var SchemaSuggester[]
19+
*/
20+
private $schemaSuggesters = [];
21+
1622
/**
1723
* @param Config $config
1824
* @param SuggestQueryBuilder $queryBuilder
@@ -29,15 +35,15 @@ public function __construct(Config $config, SuggestQueryBuilder $queryBuilder)
2935
$queryBuilder->buildClassSuggestQuery($entityClass)
3036
);
3137
}
32-
/*
38+
3339
$entitySchemaName = $entityConfig['schema'];
34-
if (!array_key_exists($entitySchemaName, $this->schemaFinders)) {
35-
$this->schemaFinders[$entitySchemaName] = new SchemaFinder(
36-
$queryBuilder,
37-
$config->getSchemaByEntityClass($entityClass),
38-
$config
40+
if (!array_key_exists($entitySchemaName, $this->schemaSuggesters)) {
41+
$this->schemaSuggesters[$entitySchemaName] = new SchemaSuggester(
42+
$queryBuilder->buildSchemaSuggestQuery(
43+
$config->getSchemaByEntityClass($entityConfig['class'])
44+
)
3945
);
40-
}*/
46+
}
4147
}
4248
}
4349

@@ -56,4 +62,21 @@ public function getClassSuggester($class)
5662

5763
return $this->classSuggesters[$class];
5864
}
65+
66+
67+
/**
68+
* @param string $schema
69+
* @return SchemaSuggester
70+
* @throws \InvalidArgumentException
71+
*/
72+
public function getSchemaSuggester($schema)
73+
{
74+
if (!array_key_exists($schema, $this->schemaSuggesters)) {
75+
throw new \InvalidArgumentException(
76+
sprintf('Schema suggester %s is not found', $schema)
77+
);
78+
}
79+
80+
return $this->schemaSuggesters[$schema];
81+
}
5982
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Mdiyakov\DoctrineSolrBundle\Query\Hydrator;
4+
5+
use Mdiyakov\DoctrineSolrBundle\Exception\HydratorException;
6+
use Mdiyakov\DoctrineSolrBundle\Query\Suggest\Result\FieldResult;
7+
use Mdiyakov\DoctrineSolrBundle\Query\Suggest\Solarium\Result\Result;
8+
use Mdiyakov\DoctrineSolrBundle\Query\Suggest\Solarium\Result\Term;
9+
use Mdiyakov\DoctrineSolrBundle\Schema\Schema;
10+
use Mdiyakov\DoctrineSolrBundle\Query\Suggest\Result\Result as SuggestQueryResult;
11+
12+
13+
class SuggestQueryHydrator
14+
{
15+
/**
16+
* @var Schema
17+
*/
18+
private $schema;
19+
20+
/**
21+
* @param Schema $schema
22+
*/
23+
public function __construct(Schema $schema)
24+
{
25+
$this->schema = $schema;
26+
}
27+
28+
/**
29+
* @param Result $solrResult
30+
* @return SuggestQueryResult
31+
*/
32+
public function hydrate(Result $solrResult)
33+
{
34+
if (!$solrResult->count()) {
35+
throw new HydratorException('Result dataset is empty');
36+
}
37+
38+
$result = new SuggestQueryResult();
39+
40+
foreach ($solrResult->getResults() as $suggester => $termData) {
41+
$field = $this->schema->getFieldBySuggester($suggester);
42+
43+
/** @var Term $term */
44+
$term = current($termData);
45+
$result->addFieldResult(
46+
new FieldResult($field->getEntityFieldName(), key($termData), $term->getSuggestions())
47+
);
48+
}
49+
50+
return $result;
51+
}
52+
}

Query/SelectQueryBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function buildMultiClassSelectQuery(Schema $schema, $classes)
103103
}
104104

105105
$multiClassQueryConfig['entityConfigs'][] = $entityConfig;
106-
$multiClassQueryConfig['hydrators'][$entityConfig[$discriminatorConfigField->getConfigFieldName()]] = $this->hydrators[$class];
106+
$multiClassQueryConfig['hydrators'][$discriminatorConfigField->getValue($entityConfig)] = $this->hydrators[$class];
107107
}
108108

109109
return new MultiClassSelectQuery(

Query/Suggest/AbstractSuggestQuery.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Mdiyakov\DoctrineSolrBundle\Query\Suggest;
44

55
use Mdiyakov\DoctrineSolrBundle\Exception\SuggestQueryException;
6+
use Mdiyakov\DoctrineSolrBundle\Query\Hydrator\SuggestQueryHydrator;
67
use Mdiyakov\DoctrineSolrBundle\Query\Suggest\Solarium\Query;
78
use Mdiyakov\DoctrineSolrBundle\Schema\Schema;
89
use Solarium\Client;
@@ -65,8 +66,8 @@ public function addField($entityFieldName)
6566
if (!$field->getSuggester()) {
6667
throw new SuggestQueryException(
6768
sprintf(
68-
'Class "%s" does not support a suggestion by field %s',
69-
$this->entityConfig['class'],
69+
'Schema "%s" does not support a suggestion by field %s',
70+
$this->schema->getName(),
7071
$entityFieldName
7172
)
7273
);
@@ -100,7 +101,12 @@ public function suggest()
100101

101102
$solrQuery->setCount($this->getCount());
102103

103-
return $this->client->execute($solrQuery);
104+
/** @var Solarium\Result\Result $result */
105+
$result = $this->client->execute($solrQuery);
106+
107+
$hydrator = new SuggestQueryHydrator($this->schema);
108+
109+
return $hydrator->hydrate($result);
104110
}
105111

106112
/**

Query/Suggest/Result/FieldResult.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Mdiyakov\DoctrineSolrBundle\Query\Suggest\Result;
4+
5+
class FieldResult
6+
{
7+
8+
/**
9+
* @var string
10+
*/
11+
private $entityFieldName;
12+
13+
/**
14+
* @var string
15+
*/
16+
private $initialTerm;
17+
18+
/**
19+
* @var SuggestionResult[]
20+
*/
21+
private $suggestions;
22+
23+
/**
24+
* FieldResult constructor.
25+
* @param string $entityFieldName
26+
* @param string $initialTerm
27+
* @param [][] $suggestions
28+
*/
29+
public function __construct($entityFieldName, $initialTerm, array $suggestions)
30+
{
31+
$this->entityFieldName = $entityFieldName;
32+
$this->initialTerm = $initialTerm;
33+
34+
foreach ($suggestions as $suggestion) {
35+
$this->suggestions[] = new SuggestionResult($suggestion['term'], $suggestion['weight'], $suggestion['payload']);
36+
}
37+
38+
}
39+
40+
/**
41+
* @return SuggestionResult[]
42+
*/
43+
public function getSuggestions()
44+
{
45+
return $this->suggestions;
46+
}
47+
48+
/**
49+
* @return string
50+
*/
51+
public function getInitialTerm()
52+
{
53+
return $this->initialTerm;
54+
}
55+
56+
/**
57+
* @return string
58+
*/
59+
public function getEntityFieldName()
60+
{
61+
return $this->entityFieldName;
62+
}
63+
64+
/**
65+
* @return int
66+
*/
67+
public function getCount()
68+
{
69+
return count($this->suggestions);
70+
}
71+
}

Query/Suggest/Result/Result.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Mdiyakov\DoctrineSolrBundle\Query\Suggest\Result;
4+
5+
use Mdiyakov\DoctrineSolrBundle\Exception\SuggestResultException;
6+
7+
class Result
8+
{
9+
/**
10+
* @var FieldResult[]
11+
*/
12+
private $data;
13+
14+
/**
15+
* @param FieldResult $fieldResult
16+
*/
17+
public function addFieldResult(FieldResult $fieldResult)
18+
{
19+
$this->data[$fieldResult->getEntityFieldName()] = $fieldResult;
20+
}
21+
22+
/**
23+
* @param $entityFieldName
24+
* @return FieldResult
25+
* @throws SuggestResultException
26+
*/
27+
public function getResultsByField($entityFieldName)
28+
{
29+
if (!array_key_exists($entityFieldName, $this->data)) {
30+
throw new SuggestResultException(
31+
sprintf('There is no result for %s entity field', $entityFieldName)
32+
);
33+
}
34+
35+
return $this->data[$entityFieldName];
36+
}
37+
}
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Mdiyakov\DoctrineSolrBundle\Query\Suggest\Result;
4+
5+
class SuggestionResult
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $term;
11+
12+
/**
13+
* @var float
14+
*/
15+
private $weight;
16+
17+
/**
18+
* @var string
19+
*/
20+
private $payload;
21+
22+
/**
23+
* SuggestionResult constructor.
24+
* @param string $term
25+
* @param float $weight
26+
* @param string $payload
27+
*/
28+
public function __construct($term, $weight, $payload)
29+
{
30+
$this->term = $term;
31+
$this->weight = $weight;
32+
$this->payload = $payload;
33+
}
34+
35+
/**
36+
* @return string
37+
*/
38+
public function getTerm()
39+
{
40+
return $this->term;
41+
}
42+
43+
/**
44+
* @return float
45+
*/
46+
public function getWeight()
47+
{
48+
return $this->weight;
49+
}
50+
51+
/**
52+
* @return string
53+
*/
54+
public function getPayload()
55+
{
56+
return $this->payload;
57+
}
58+
59+
}

Query/Suggest/SchemaSuggestQuery.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Mdiyakov\DoctrineSolrBundle\Query\Suggest;
4+
5+
use Mdiyakov\DoctrineSolrBundle\Query\Suggest\Solarium\Query;
6+
7+
class SchemaSuggestQuery extends AbstractSuggestQuery
8+
{
9+
10+
/**
11+
* @param Query $solrQuery
12+
*/
13+
protected function initDiscriminatorConditions(Query $solrQuery) {}
14+
}

0 commit comments

Comments
 (0)