Skip to content

Commit 847060f

Browse files
authored
feat(Firestore): add sample for multiple inequality filters (GoogleCloudPlatform#2029)
1 parent cb41867 commit 847060f

File tree

3 files changed

+28
-88
lines changed

3 files changed

+28
-88
lines changed

firestore/src/query_filter_range_invalid.php renamed to firestore/src/query_filter_compound_multi_ineq.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
22
/**
3-
* Copyright 2018 Google Inc.
3+
* Copyright 2024 Google Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -26,25 +26,36 @@
2626
use Google\Cloud\Firestore\FirestoreClient;
2727

2828
/**
29-
* An example of an invalid range query. @see https://cloud.google.com/firestore/docs/query-data/queries#compound_queries
29+
* Example of a query with range and inequality filters on multiple fields.
30+
* @see https://cloud.google.com/firestore/docs/query-data/multiple-range-fields
3031
*
3132
* @param string $projectId The Google Cloud Project ID
3233
*/
33-
function query_filter_range_invalid(string $projectId): void
34+
function query_filter_compound_multi_ineq(string $projectId): void
3435
{
3536
// Create the Cloud Firestore client
3637
$db = new FirestoreClient([
3738
'projectId' => $projectId,
3839
]);
39-
$citiesRef = $db->collection('samples/php/cities');
40-
# [START firestore_query_filter_range_invalid]
41-
$invalidRangeQuery = $citiesRef
42-
->where('state', '>=', 'CA')
43-
->where('population', '>', 1000000);
44-
# [END firestore_query_filter_range_invalid]
40+
$collection = $db->collection('samples/php/users');
41+
// Setup the data before querying for it
42+
$collection->document('person1')->set(['age' => 23, 'height' => 65]);
43+
$collection->document('person2')->set(['age' => 37, 'height' => 55]);
44+
$collection->document('person3')->set(['age' => 40, 'height' => 75]);
45+
$collection->document('person4')->set(['age' => 40, 'height' => 65]);
4546

46-
// This will throw an exception
47-
$invalidRangeQuery->documents();
47+
# [START firestore_query_filter_compound_multi_ineq]
48+
$chainedQuery = $collection
49+
->where('age', '>', 35)
50+
->where('height', '>', 60)
51+
->where('height', '<', 70);
52+
# [END firestore_query_filter_compound_multi_ineq]
53+
foreach ($chainedQuery->documents() as $document) {
54+
printf(
55+
'Document %s returned by age > 35 and height between 60 and 70' . PHP_EOL,
56+
$document->id()
57+
);
58+
}
4859
}
4960

5061
// The following 2 lines are only needed to run the samples

firestore/src/query_order_field_invalid.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

firestore/test/firestoreTest.php

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
namespace Google\Cloud\Samples\Firestore;
1919

20-
use Google\Cloud\Core\Exception\BadRequestException;
2120
use Google\Cloud\Core\Exception\FailedPreconditionException;
2221
use Google\Cloud\Firestore\FirestoreClient;
2322
use Google\Cloud\TestUtils\TestTrait;
@@ -275,6 +274,12 @@ public function testChainedQuery()
275274
$this->assertStringContainsString('Document SF returned by query state=CA and name=San Francisco', $output);
276275
}
277276

277+
public function testChainedInequalityQuery()
278+
{
279+
$output = $this->runFirestoreSnippet('query_filter_compound_multi_ineq');
280+
$this->assertStringContainsString('Document person4 returned by age > 35 and height between 60 and 70', $output);
281+
}
282+
278283
/**
279284
* @depends testQueryCreateExamples
280285
*/
@@ -298,18 +303,6 @@ public function testRangeQuery()
298303
$this->assertStringContainsString('Document SF returned by query CA<=state<=IN', $output);
299304
}
300305

301-
/**
302-
* @depends testQueryCreateExamples
303-
*/
304-
public function testInvalidRangeQuery()
305-
{
306-
$this->expectException(BadRequestException::class);
307-
$this->expectExceptionMessage(
308-
'Cannot have inequality filters on multiple properties'
309-
);
310-
$this->runFirestoreSnippet('query_filter_range_invalid');
311-
}
312-
313306
/**
314307
* @depends testQueryCreateExamples
315308
*/
@@ -509,18 +502,6 @@ public function testRangeOrderByQuery()
509502
$this->assertStringContainsString('Document BJ returned by range with order by query', $output);
510503
}
511504

512-
/**
513-
* @depends testRetrieveCreateExamples
514-
*/
515-
public function testInvalidRangeOrderByQuery()
516-
{
517-
$this->expectException(BadRequestException::class);
518-
$this->expectExceptionMessage(
519-
'inequality filter property and first sort order must be the same'
520-
);
521-
$this->runFirestoreSnippet('query_order_field_invalid');
522-
}
523-
524505
public function testDocumentRef()
525506
{
526507
$output = $this->runFirestoreSnippet('data_reference_document');

0 commit comments

Comments
 (0)