Skip to content

Commit 851289e

Browse files
committed
ensure floats
1 parent 6e00d2d commit 851289e

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/Core/Query/Helper.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ public function knn(string $field, array $vector, ?int $topK = null, ?string $pr
590590
return $this->qparser(
591591
'knn',
592592
$params,
593-
).'['.implode(', ', $vector).']';
593+
).$this->getFloatList($vector);
594594
}
595595

596596
/**
@@ -650,7 +650,7 @@ public function vectorSimilarity(string $field, array $vector, float $minReturn,
650650
return $this->qparser(
651651
'vectorSimilarity',
652652
$params,
653-
).'['.implode(', ', $vector).']';
653+
).$this->getFloatList($vector);
654654
}
655655

656656
/**
@@ -680,4 +680,21 @@ protected function getCommonVectorParams (string $field, ?string $preFilter = nu
680680

681681
return $params;
682682
}
683+
684+
/**
685+
* Get a float list as a string.
686+
*
687+
* @param float[] $values
688+
*
689+
* @return string
690+
*/
691+
protected function getFloatList(array $values): string
692+
{
693+
return '['.implode(', ', array_map(function ($value) {
694+
if ($value == (int) $value) {
695+
return number_format($value, 1, '.', '');
696+
}
697+
return (string) $value;
698+
}, $values)).']';
699+
}
683700
}

tests/Core/Query/HelperTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,12 +779,12 @@ public function testKnn(): void
779779
public function testKnnTextToVector(): void
780780
{
781781
$this->assertSame(
782-
'{!knn_text_to_vector f=vector model=a-model, topK=10}hello world query',
782+
'{!knn_text_to_vector f=vector model=a-model topK=10}hello world query',
783783
$this->helper->knnTextToVector('a-model', 'vector', 'hello world query', 10)
784784
);
785785

786786
$this->assertSame(
787-
'{!knn_text_to_vector f=vector preFilter=category:AAA preFilter=inStock:true model=a-model, topK=10}hello world query',
787+
'{!knn_text_to_vector f=vector preFilter=category:AAA preFilter=inStock:true model=a-model topK=10}hello world query',
788788
$this->helper->knnTextToVector('a-model', 'vector', 'hello world query', 10, 'category:AAA', 'inStock:true')
789789
);
790790
}

0 commit comments

Comments
 (0)