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

Feauture request: Support for multiple references to the same collection in a particular collection #1780

Open
dbuedo opened this issue Jun 7, 2024 · 0 comments
Labels

Comments

@dbuedo
Copy link

dbuedo commented Jun 7, 2024

Description

Currently is not possible to have multiple references to the same collection in a particular collection, therefore is not possible to have an N:M relation with the same collection. This ability is helpful for different use cases, for example:

  • A user have multiple managers and manage multiple users itself.
  • The typical Twitter followers use case: A user has followers and follow others itself
Following relationship

I would like to be able to make two kind of queries, for a certain user:

  • Return all the user documents of their followers.
  • Return all the user documents of the users they follow.

Steps to reproduce

typesense-following.txt

Actual Behavior

Currently, when you create a collection two references to the same collection, the collection is created as usual, but when you try to use include_fields, it works as it only one of the reference was taken into account.
For example, if you have:

 Users:   {
         "name": "users",
         "fields": [
           {"name": "id", "type": "string" },
           {"name": "name", "type": "string" }
         ]
       }

Following: {
         "name": "following",
         "fields": [
           {"name": "follower_id", "type": "string", "reference": "users.id" },
           {"name": "followed_id", "type": "string", "reference": "users.id" }
         ]
       }

Notice that the Following collection is created with the two references even if only one of them is taken into account. Failing with an explicit message about the restriction would be better, in my opinion.

Then you search with:

           {
              "collection": "following",
              "q": "*",
              "filter_by": "follower_id:=3",
              "include_fields": "$users(*)"
            },
            {
              "collection": "following",
              "q": "*",
              "filter_by": "followed_id:=3",
              "include_fields": "$users(*)"
            }

The results are like if include_fields always join using the follower_id, and there is no way to retrieve the followed user.

Expected Behavior

I would like to be able to follow both references. Maybe an option could be something like having the possibility to set a name for the reference:

Collection: {
         "name": "following",
         "fields": [
           {"name": "follower_id", "type": "string", "reference": "users.id as follower" },
           {"name": "followed_id", "type": "string", "reference": "users.id as followed" }
         ]
       }

Search: 
          {
              "collection": "following",
              "q": "*",
              "filter_by": "follower_id:=3",
              "include_fields": "$users[followed](*)"
            },
            {
              "collection": "following",
              "q": "*",
              "filter_by": "followed_id:=3",
              "include_fields": "$users[follower](*)"
            }

But this is just an idea, as I ignore the implementation details.

Metadata

Typesense Version: 26.0

OS: any

Source: Slack thread

@kishorenc kishorenc added the join label Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants