Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection Group feature not implemented #5286

Open
enniopruvo opened this issue May 19, 2022 · 2 comments
Open

Collection Group feature not implemented #5286

enniopruvo opened this issue May 19, 2022 · 2 comments
Labels
type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@enniopruvo
Copy link

When all_descendants is false, selects only collections that are immediate children of the parent specified in the containing RunQueryRequest.
When all_descendants is true, selects all descendant collections.
CollectionSelector

If the collectionId is from all descendants, so descendent of what? Descente of parent path name.
Currently when use collectionGroup by default parent path name is the root (projects/{projectId}/databases/(default)/documents).
Why the parent path name can't be a document reference path and the query works only inside that path?

Environment details

  • OS: Ubuntu server
  • PHP version: 7.3
  • Package name and version: "google/cloud-firestore": "^1.14.1",

Steps to reproduce

I have 3 collections named people one for organizations, another for schools inside which is inside organizations, and the last inside classroom collection.

Collection people path examples:

organizations/{id}/people
organizations/{id}/schools/{id}/people
organizations/{id}/schools/{id}/classroom/{id}/people

Code example

use Google\Cloud\Firestore\FirestoreClient;

$firestore = new FirestoreClient;
$query = $firestore->collectionGroup('people');
dump($query)

Following there is a dump of the query from example code below which we can see that the parentName property has the value projects/gcp-project/databases/(default)/documents .

image

At this point the query will work to all people collections for the entire project, but I want that query to work only for people collections inside specifique organization.

I have modified the method collectionGroup in Google\Cloud\Firestore\FirestoreClient to test and it works. (I've modified it to tests only, not to production).

Original method code:

public function collectionGroup($id)
    {
        if (strpos($id, '/') !== false) {
            throw new \InvalidArgumentException(
                'Collection ID may not contain a slash.'
            );
        }

        return new Query(
            $this->connection,
            $this->valueMapper,
            $this->fullName($this->projectId, $this->database),
            [
                'from' => [
                    [
                        'collectionId' => $id,
                        'allDescendants' => true
                    ]
                ]
            ]
        );
    }

Modified method code:

public function collectionGroup($id, $relativeName = null) // added $relativeName by default null
    {
        if (strpos($id, '/') !== false) {
            throw new \InvalidArgumentException(
                'Collection ID may not contain a slash.'
            );
        }

        return new Query(
            $this->connection,
            $this->valueMapper,
            $this->fullName($this->projectId, $this->database, $relativeName), // added a third parameter
            [
                'from' => [
                    [
                        'collectionId' => $id,
                        'allDescendants' => true
                    ]
                ]
            ]
        );
    }
use Google\Cloud\Firestore\FirestoreClient;

$firestore = new FirestoreClient;
$query = $firestore->collectionGroup(
    'people',
    'organizations/955ac838-cda7-4cfe-b55b-9a0be4cd548b'
);
dump($query);

Dump of query with modified collectionGroup

image

This query works perfectly.

My conclusition is that feature is not implemented yet.

@yoshi-automation yoshi-automation added triage me I really want to be triaged. 🚨 This issue needs some love. labels May 20, 2022
@dwsupplee dwsupplee added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. and removed triage me I really want to be triaged. 🚨 This issue needs some love. labels May 24, 2022
@vishwarajanand
Copy link
Contributor

@enniopruvo thanks for posting this issue.
It seems you want to scope down your search to ONLY a particular path. I reckon that's not doable so easily. If you have path as one of the key-value pairs in the document, you could achieve this! Also note there's no string contains method, so the path needs to match exactly unless you store path value in the form of an array. Does this help unblock you?

The way you suggested doesn't seem to be a path that other google client libraries such as dotnet, others follow. I am a bit reluctant to make that code change as yet but I will surely raise that up internally. Will update the thread in either case.

In case you are looking to search people documents across subcollection, feel free to refer #2542 (comment) where I explained how having a single field index could help you achieve the same.

@vishwarajanand
Copy link
Contributor

Hi, we will review this issue after H2, during a rewrite of the component features.

@vishwarajanand vishwarajanand removed their assignment Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
4 participants