Skip to content

Commit 190de85

Browse files
committed
added boost field setter and getter for Knn Search
1 parent 96d4f5a commit 190de85

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/Knn/Knn.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Knn implements BuilderInterface
3939
private $numCandidates;
4040

4141
/**
42-
* @var int
42+
* @var float|null
4343
*/
4444
private $boost;
4545

@@ -153,6 +153,22 @@ public function setSimilarity(float $similarity): void
153153
$this->similarity = $similarity;
154154
}
155155

156+
/**
157+
* @return float|null
158+
*/
159+
public function getBoost(): ?float
160+
{
161+
return $this->boost;
162+
}
163+
164+
/**
165+
* @param float $boost
166+
*/
167+
public function setBoost(float $boost): void
168+
{
169+
$this->boost = $boost;
170+
}
171+
156172
/**
157173
* @return BuilderInterface|null
158174
*/
@@ -193,6 +209,10 @@ public function toArray()
193209
$output['similarity'] = $this->getSimilarity();
194210
}
195211

212+
if ($this->getBoost()) {
213+
$output['boost'] = $this->getBoost();
214+
}
215+
196216
if ($this->getFilter()) {
197217
$output['filter'] = $this->getFilter()->toArray();
198218
}

tests/Functional/Knn/KnnTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
namespace ONGR\ElasticsearchDSL\Tests\Functional\Knn;
88

9-
use ONGR\ElasticsearchDSL\Aggregation\Bucketing\DateHistogramAggregation;
9+
use Composer\InstalledVersions;
10+
use Elastic\Elasticsearch\Client;
1011
use ONGR\ElasticsearchDSL\Knn\Knn;
1112
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
1213
use ONGR\ElasticsearchDSL\Search;
@@ -82,4 +83,24 @@ public function testKnnSearchWithFilter(): void
8283
$this->assertCount(1, $results['hits']['hits']);
8384
$this->assertEquals('doc_3', $results['hits']['hits'][0]['_id']);
8485
}
86+
87+
/**
88+
* Match all test
89+
*/
90+
public function testMultipleKnnSearchWithBoost(): void
91+
{
92+
$knn1 = new Knn('vector_field', [1, 2, 3], 1, 1);
93+
$knn1->setFilter(new TermQuery('label', 2));
94+
$knn1->setBoost(0.5);
95+
$knn2 = new Knn('vector_field', [1, 2, 4], 1, 1);
96+
$knn2->setFilter(new TermQuery('label', 2));
97+
$knn2->setBoost(0.1);
98+
99+
$search = new Search();
100+
$search->addKnn($knn1);
101+
$search->addKnn($knn2);
102+
$results = $this->executeSearch($search, true);
103+
$this->assertCount(1, $results['hits']['hits']);
104+
$this->assertEquals('doc_3', $results['hits']['hits'][0]['_id']);
105+
}
85106
}

0 commit comments

Comments
 (0)