forked from ongr-io/ElasticsearchDSL
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
571 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ElasticsearchDSL\Knn; | ||
|
||
use ONGR\ElasticsearchDSL\BuilderInterface; | ||
use ONGR\ElasticsearchDSL\FieldAwareTrait; | ||
|
||
class Knn implements BuilderInterface | ||
{ | ||
use FieldAwareTrait; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $field; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $queryVector; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $k; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $numCandidates; | ||
|
||
/** | ||
* @var float|null | ||
*/ | ||
private $boost; | ||
|
||
/** | ||
* @var float | ||
*/ | ||
private $similarity = null; | ||
|
||
/** | ||
* @var BuilderInterface | ||
*/ | ||
private $filter = null; | ||
|
||
|
||
/** | ||
* TermSuggest constructor. | ||
* @param string $field | ||
* @param array $queryVector | ||
* @param int $k | ||
* @param int $numCandidates | ||
*/ | ||
public function __construct( | ||
string $field, | ||
array $queryVector, | ||
int $k, | ||
int $numCandidates | ||
) { | ||
$this->setField($field); | ||
$this->setQueryVector($queryVector); | ||
$this->setK($k); | ||
$this->setNumCandidates($numCandidates); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getField(): string | ||
{ | ||
return $this->field; | ||
} | ||
|
||
/** | ||
* @param string $field | ||
*/ | ||
public function setField(string $field): void | ||
{ | ||
$this->field = $field; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getQueryVector(): array | ||
{ | ||
return $this->queryVector; | ||
} | ||
|
||
/** | ||
* @param array $queryVector | ||
*/ | ||
public function setQueryVector(array $queryVector): void | ||
{ | ||
$this->queryVector = $queryVector; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getK(): int | ||
{ | ||
return $this->k; | ||
} | ||
|
||
/** | ||
* @param int $k | ||
*/ | ||
public function setK(int $k): void | ||
{ | ||
$this->k = $k; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getNumCandidates(): int | ||
{ | ||
return $this->numCandidates; | ||
} | ||
|
||
/** | ||
* @param int $numCandidates | ||
*/ | ||
public function setNumCandidates(int $numCandidates): void | ||
{ | ||
$this->numCandidates = $numCandidates; | ||
} | ||
|
||
/** | ||
* @return float|null | ||
*/ | ||
public function getSimilarity(): ?float | ||
{ | ||
return $this->similarity; | ||
} | ||
|
||
/** | ||
* @param float $similarity | ||
*/ | ||
public function setSimilarity(float $similarity): void | ||
{ | ||
$this->similarity = $similarity; | ||
} | ||
|
||
/** | ||
* @return float|null | ||
*/ | ||
public function getBoost(): ?float | ||
{ | ||
return $this->boost; | ||
} | ||
|
||
/** | ||
* @param float $boost | ||
*/ | ||
public function setBoost(float $boost): void | ||
{ | ||
$this->boost = $boost; | ||
} | ||
|
||
/** | ||
* @return BuilderInterface|null | ||
*/ | ||
public function getFilter(): ?BuilderInterface | ||
{ | ||
return $this->filter; | ||
} | ||
|
||
/** | ||
* @param BuilderInterface $filter | ||
*/ | ||
public function setFilter(BuilderInterface $filter): void | ||
{ | ||
$this->filter = $filter; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getType() | ||
{ | ||
return 'knn'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function toArray() | ||
{ | ||
$output = [ | ||
'field' => $this->getField(), | ||
'query_vector' => $this->getQueryVector(), | ||
'k' => $this->getK(), | ||
'num_candidates' => $this->getNumCandidates(), | ||
]; | ||
|
||
if ($this->getSimilarity()) { | ||
$output['similarity'] = $this->getSimilarity(); | ||
} | ||
|
||
if ($this->getBoost()) { | ||
$output['boost'] = $this->getBoost(); | ||
} | ||
|
||
if ($this->getFilter()) { | ||
$output['filter'] = $this->getFilter()->toArray(); | ||
} | ||
|
||
return $output; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ElasticsearchDSL\SearchEndpoint; | ||
|
||
use ONGR\ElasticsearchDSL\BuilderInterface; | ||
use ONGR\ElasticsearchDSL\Knn\Knn; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
/** | ||
* Search suggest dsl endpoint. | ||
*/ | ||
class KnnEndpoint extends AbstractSearchEndpoint | ||
{ | ||
/** | ||
* Endpoint name | ||
*/ | ||
const NAME = 'knn'; | ||
|
||
public function add(BuilderInterface $builder, $key = null) | ||
{ | ||
if ($builder instanceof Knn) { | ||
return parent::add($builder, $key); | ||
} | ||
|
||
throw new \LogicException('Add Knn builder instead!'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize( | ||
NormalizerInterface $normalizer, | ||
$format = null, | ||
array $context = [] | ||
): array|string|int|float|bool { | ||
$knns = $this->getAll(); | ||
if (count($knns) === 1) { | ||
/** @var Knn $knn */ | ||
$knn = array_values($knns)[0]; | ||
return $knn->toArray(); | ||
} | ||
|
||
if (count($knns) > 1) { | ||
$output = []; | ||
/** @var Knn $knn */ | ||
foreach ($knns as $knn) { | ||
$output[] = $knn->toArray(); | ||
} | ||
return $output; | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.