From ea6b8eba881376a8268e8fdcf68e7503f1874206 Mon Sep 17 00:00:00 2001 From: Nate Wiebe Date: Fri, 5 Sep 2025 15:34:08 -0400 Subject: [PATCH] [Store][Qdrant] Allow setting filters on a query --- src/store/src/Bridge/Qdrant/Store.php | 5 +++ src/store/tests/Bridge/Qdrant/StoreTest.php | 45 +++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/store/src/Bridge/Qdrant/Store.php b/src/store/src/Bridge/Qdrant/Store.php index b3b53b799..8d9e207ff 100644 --- a/src/store/src/Bridge/Qdrant/Store.php +++ b/src/store/src/Bridge/Qdrant/Store.php @@ -65,6 +65,7 @@ public function add(VectorDocument ...$documents): void /** * @param array{ + * filter?: array, * limit?: positive-int, * offset?: positive-int * } $options @@ -77,6 +78,10 @@ public function query(Vector $vector, array $options = []): array 'with_vector' => true, ]; + if (\array_key_exists('filter', $options)) { + $payload['filter'] = $options['filter']; + } + if (\array_key_exists('limit', $options)) { $payload['limit'] = $options['limit']; } diff --git a/src/store/tests/Bridge/Qdrant/StoreTest.php b/src/store/tests/Bridge/Qdrant/StoreTest.php index cae2b0c9c..9cf29dbc3 100644 --- a/src/store/tests/Bridge/Qdrant/StoreTest.php +++ b/src/store/tests/Bridge/Qdrant/StoreTest.php @@ -228,4 +228,49 @@ public function testStoreCanQuery() $this->assertSame(1, $httpClient->getRequestsCount()); $this->assertCount(2, $results); } + + public function testStoreCanQueryWithFilters() + { + $httpClient = new MockHttpClient([ + new JsonMockResponse([ + 'result' => [ + 'points' => [ + [ + 'id' => Uuid::v4()->toRfc4122(), + 'vector' => [0.1, 0.2, 0.3], + 'payload' => ['foo' => 'bar'], + ], + [ + 'id' => Uuid::v4()->toRfc4122(), + 'vector' => [0.2, 0.1, 0.3], + 'payload' => ['foo' => ['bar', 'baz']], + ], + ], + ], + ], [ + 'http_code' => 200, + ]), + ], 'http://127.0.0.1:6333'); + + $store = new Store($httpClient, 'http://127.0.0.1:6333', 'test', 'test'); + + $results = $store->query(new Vector([0.1, 0.2, 0.3]), [ + 'filter' => [ + 'must' => [ + ['key' => 'foo', 'match' => ['value' => 'bar']], + ], + ], + ]); + + $this->assertSame(1, $httpClient->getRequestsCount()); + $this->assertCount(2, $results); + + foreach ($results as $result) { + $this->assertArrayHasKey('foo', $result->metadata); + $this->assertTrue( + 'bar' === $result->metadata['foo'] || (\is_array($result->metadata['foo']) && \in_array('bar', $result->metadata['foo'], true)), + "Value should be 'bar' or an array containing 'bar'" + ); + } + } }