handcraftedinthealps / ElasticsearchDSL

Fork of ongr-io/ElasticsearchDSL providing Symfony 5 support for different elasticsearch versions.
MIT License
19 stars 16 forks source link

Knn Search Support #27

Open hkulekci opened 1 year ago

hkulekci commented 1 year ago

Do we have a plan to add support for Knn Search, or is it possible with this library? I would like to hear your opinions.

ping @alexander-schranz

alexander-schranz commented 1 year ago

Nobody yet as I know did yet implement knn search or vector types. If somebody want to contribute it feel free, currently we have not yet planned it.

hkulekci commented 1 year ago

Thanks for the quick response! I'm interested in giving it a try and attempting to implement knn search support for the library. I hope I can implement it in short time. 🤗

hkulekci commented 11 months ago

Implemented a solution and here an example usage:

<?php
use Elastic\Elasticsearch\ClientBuilder;
use ONGR\ElasticsearchDSL\Knn\Knn;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;

include __DIR__ . '/../vendor/autoload.php';

$builder = ClientBuilder::create();
$builder->setElasticCloudId("....");
$builder->setApiKey('....');
$client = $builder->build();

$knn = new Knn(
    'my_vector',
    [0.5, 10, 8],
    10,
    100,
);

$matchAll = new MatchAllQuery();
$knn->setFilter($matchAll);

$search = new ONGR\ElasticsearchDSL\Search();
$search->addKnn($knn);

$params = [
    'index' => 'my-index',
    'body' => $search->toArray(),
];

$response = $client->search($params);
var_dump($response->asArray());

@alexander-schranz what do you think?