-
Notifications
You must be signed in to change notification settings - Fork 16
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
Knn Search support #28
Changes from all commits
61850ef
2e303e0
48b8e3d
96d4f5a
7ab7a83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 <info@nfq.com> | ||
* | ||
* 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; | ||
} | ||
} |
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 <info@nfq.com> | ||
* | ||
* 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 []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Elasticsearch started supporting multiple Knn query with version 8.7. So, to be able to support the versions between 8.7 and 8.4, I just put these if controls. It will return the Knn query itself if the container has just one Knn query. The container has multiple Knn query, it will return an array of Knn queries. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
KNN support is moved from _knn_search to _search endpoint in version 8.4.0, so for this reason, I change the elasticsearch version of Github CI as 8.4.0. Knn query support will be provided for elasticsearch 8.4.0 and above. :|