简体   繁体   中英

AWS Amplify filter for @searchable annotation

Currently I am using a DynamoDB instance for my social media application. While designing the schema I sticked to the "one table" rule. So I am putting every data in the same table like posts, users, comments etc.
Now I want to make flexible queries for my data. Here I found out that I could use the @searchable annotation to create an Elastic Search instance for a table which is annotated with @model

In my GraphQL schema I only have one @model , since I only have one table. My problem now is that I don't want to make everything in the table searchable, since that would be most likely very expensive. There are some data which don't have to be added to the Elastic Search instance (For example comment related data).

How could I handle it? Do I really have to split my schema down into multiple tables to be able to manage the @searchable annotation? Couldn't I decide If the row should be stored to the Elastic Search with help of the Partitionkey / Primarykey, acting like a filter?

The current implementation of the amplify-cli uses a predefined python Lambda that are added once we add the @searchable directive to one of our models.

The Lambda code can not be edited and currently, there is no option to define a custom Lambda , you read about it
https://github.com/aws-amplify/amplify-cli/issues/1113
https://github.com/aws-amplify/amplify-cli/issues/1022

If you want a custom Lambda where you can filter what goes to the Elasticsearch Instance, you can follow the steps described here https://github.com/aws-amplify/amplify-cli/issues/1113#issuecomment-476193632

The closest you can get is by creating a template in amplify\\backend\\api\\myapiname\\stacks\\ where you can manage all the resources related to Elasticsearch . A good start point is to

  1. Add @searchable to one of your model in the schema.grapql
  2. Run amplify api gql-compile
  3. Copy the generated template in the build folder, \\amplify\\backend\\api\\myapiname\\build\\stacks\\SearchableStack.json to amplify\\backend\\api\\myapiname\\stacks\\
  4. Remove the @searchable directive from the model added in step 1
  5. Start editing your new template copied in step 3
  6. Add a Lambda and use it in the template as the resolver for the DynamoDB Stream

Using this approach will give you total control of the resources related to the Elasticsearch service , but, will also require to do it all by your own.

Or, just go by creating a table for each model.

Hope it helps

It is now possible to override the generated streaming function code as well.

thanks to the AWS Support for the information provided

leaved a message on the related github issue as well https://github.com/aws-amplify/amplify-category-api/issues/437#issuecomment-1351556948

All you need is to run

  • amplify override api
  • edit the corresponding overrode.ts

change the code with the resources.opensearch.OpenSearchStreamingLambdaFunction.code

resources.opensearch.OpenSearchStreamingLambdaFunction.functionName = 'python_streaming_function';
resources.opensearch.OpenSearchStreamingLambdaFunction.handler = 'index.lambda_handler';
resources.opensearch.OpenSearchStreamingLambdaFunction.code = {
    zipFile: `
# python streaming function customized code goes here
`
}

Resources: [1] https://docs.amplify.aws/cli/graphql/override/#customize-amplify-generated-resources-for-searchable-opensearch-directive
[2]AWS::Lambda::Function Code - Properties - https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html#aws-properties-lambda-function-code-properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM