Amplify-cli: Search by Parent Entity Fields

Created on 8 Aug 2020  路  4Comments  路  Source: aws-amplify/amplify-cli

Which Category is your question related to?
Searchable and connection directives.

Amplify CLI Version
4.21.0

What AWS Services are you utilizing?
AWS Amplify

Provide additional details e.g. code snippets
In a schema like this where a user has multiple posts:

type User
  @model
  @searchable {
  id: ID!
  username: String!
  profilePic: S3Object
  coverPic: S3Object
  name: String
  location: String
  posts: [Post!]! @connection(keyName: "byUser", fields: ["id"])
}

type Post
  @model
  @key(name: "byUser", fields: ["userID"])
  @searchable {
  id: ID!
  username: String!
  title: String!
  image: S3Object
  userID: ID!
  user: User @connection(fields: ["userID"])
}

I want to search posts by User's name or some other field belonging to a user. Is this possible?
Query for search which is generated by default gives me possibility to search by user's ID, but not by other fields belonging to user.

graphql-transformer pending-response question

Most helpful comment

@aleksvidak You can search by users and then get their related posts.

By default the @searchable directive stores each model in its own Elasticsearch index. Elasticsearch isn't a relational database and can't easily do joins. There is a way to have parent / child documents in Elasticsearch, but it requires all documents to be in the same index, which isn't the case here. The other option is to denormalise your data - store the user name or other field within the post model. This then requires dynamodb stream triggers and custom logic to keep the data in sync. between users and posts. The easiest is as suggested above, search by user, then get the related posts.

All 4 comments

Hi @aleksvidak

Can you post the queries generated for the searchPosts ?

@aleksvidak You can search by users and then get their related posts.

By default the @searchable directive stores each model in its own Elasticsearch index. Elasticsearch isn't a relational database and can't easily do joins. There is a way to have parent / child documents in Elasticsearch, but it requires all documents to be in the same index, which isn't the case here. The other option is to denormalise your data - store the user name or other field within the post model. This then requires dynamodb stream triggers and custom logic to keep the data in sync. between users and posts. The easiest is as suggested above, search by user, then get the related posts.

@RossWilliams thank you, makes sense. Thought maybe there is some hidden magic I didn't see in the first place. Keep up the good work! 馃檪

Thanks @RossWilliams for the elaborate answer.

I am closing this issue for now, if you get stuck with this in future , feel free to comment and I will reopen this issue again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ReidWeb picture ReidWeb  路  3Comments

jeanpaulcozzatti picture jeanpaulcozzatti  路  3Comments

kangks picture kangks  路  3Comments

mwarger picture mwarger  路  3Comments

amlcodes picture amlcodes  路  3Comments