Skip to content

Commit 9e5fa5d

Browse files
committed
feature #474 [Store][Qdrant] Allow setting filters on a query (natewiebe13)
This PR was squashed before being merged into the main branch. Discussion ---------- [Store][Qdrant] Allow setting filters on a query | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | License | MIT Allow setting filters on queries Commits ------- ea6b8eb [Store][Qdrant] Allow setting filters on a query
2 parents 6d17be2 + ea6b8eb commit 9e5fa5d

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/store/src/Bridge/Qdrant/Store.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function add(VectorDocument ...$documents): void
6565

6666
/**
6767
* @param array{
68+
* filter?: array<string, mixed>,
6869
* limit?: positive-int,
6970
* offset?: positive-int
7071
* } $options
@@ -77,6 +78,10 @@ public function query(Vector $vector, array $options = []): array
7778
'with_vector' => true,
7879
];
7980

81+
if (\array_key_exists('filter', $options)) {
82+
$payload['filter'] = $options['filter'];
83+
}
84+
8085
if (\array_key_exists('limit', $options)) {
8186
$payload['limit'] = $options['limit'];
8287
}

src/store/tests/Bridge/Qdrant/StoreTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,49 @@ public function testStoreCanQuery()
228228
$this->assertSame(1, $httpClient->getRequestsCount());
229229
$this->assertCount(2, $results);
230230
}
231+
232+
public function testStoreCanQueryWithFilters()
233+
{
234+
$httpClient = new MockHttpClient([
235+
new JsonMockResponse([
236+
'result' => [
237+
'points' => [
238+
[
239+
'id' => Uuid::v4()->toRfc4122(),
240+
'vector' => [0.1, 0.2, 0.3],
241+
'payload' => ['foo' => 'bar'],
242+
],
243+
[
244+
'id' => Uuid::v4()->toRfc4122(),
245+
'vector' => [0.2, 0.1, 0.3],
246+
'payload' => ['foo' => ['bar', 'baz']],
247+
],
248+
],
249+
],
250+
], [
251+
'http_code' => 200,
252+
]),
253+
], 'http://127.0.0.1:6333');
254+
255+
$store = new Store($httpClient, 'http://127.0.0.1:6333', 'test', 'test');
256+
257+
$results = $store->query(new Vector([0.1, 0.2, 0.3]), [
258+
'filter' => [
259+
'must' => [
260+
['key' => 'foo', 'match' => ['value' => 'bar']],
261+
],
262+
],
263+
]);
264+
265+
$this->assertSame(1, $httpClient->getRequestsCount());
266+
$this->assertCount(2, $results);
267+
268+
foreach ($results as $result) {
269+
$this->assertArrayHasKey('foo', $result->metadata);
270+
$this->assertTrue(
271+
'bar' === $result->metadata['foo'] || (\is_array($result->metadata['foo']) && \in_array('bar', $result->metadata['foo'], true)),
272+
"Value should be 'bar' or an array containing 'bar'"
273+
);
274+
}
275+
}
231276
}

0 commit comments

Comments
 (0)