简体   繁体   English

用于@searchable 注释的 AWS Amplify 过滤器

[英]AWS Amplify filter for @searchable annotation

Currently I am using a DynamoDB instance for my social media application.目前我正在为我的社交媒体应用程序使用 DynamoDB 实例。 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 在这里我发现我可以使用@searchable注释为一个用@model注释的表创建一个弹性搜索实例

In my GraphQL schema I only have one @model , since I only have one table.在我的 GraphQL 模式中,我只有一个@model ,因为我只有一个表。 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).有些数据不必添加到Elastic Search实例中(例如评论相关数据)。

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?我真的必须将我的架构拆分成多个表才能管理@searchable注释吗? Couldn't I decide If the row should be stored to the Elastic Search with help of the Partitionkey / Primarykey, acting like a filter?我不能决定是否应该在 Partitionkey / Primarykey 的帮助下将该行存储到 Elastic Search,就像一个过滤器?

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. amplify-cli的当前实现使用预定义的python Lambda ,一旦我们将@searchable指令添加到我们的模型之一,就会添加该Lambda

The Lambda code can not be edited and currently, there is no option to define a custom Lambda , you read about it Lambda代码无法编辑,目前尚无定义自定义Lambda的选项,您已阅读
https://github.com/aws-amplify/amplify-cli/issues/1113 https://github.com/aws-amplify/amplify-cli/issues/1113
https://github.com/aws-amplify/amplify-cli/issues/1022 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 如果您想要一个自定义Lambda ,可以在其中过滤到Elasticsearch实例的内容,则可以按照此处描述的步骤进行操作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 . 您可以获得的最接近的结果是在amplify\\backend\\api\\myapiname\\stacks\\中创建一个模板,您可以在其中管理与Elasticsearch相关的所有资源。 A good start point is to 一个好的起点是

  1. Add @searchable to one of your model in the schema.grapql @searchable添加到您的机型之一在schema.grapql
  2. Run amplify api gql-compile 运行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\\ 将生成的模板复制到构建文件夹\\amplify\\backend\\api\\myapiname\\build\\stacks\\SearchableStack.jsonamplify\\backend\\api\\myapiname\\stacks\\
  4. Remove the @searchable directive from the model added in step 1 从步骤1中添加的模型中删除@searchable指令
  5. Start editing your new template copied in step 3 开始编辑在步骤3中复制的新模板
  6. Add a Lambda and use it in the template as the resolver for the DynamoDB Stream 添加一个Lambda并将其在模板中用作DynamoDB流的解析器

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. 使用这种方法将使您完全控制与Elasticsearch服务相关的资源,但是,还需要自己完成所有操作。

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.现在也可以覆盖生成的流 function 代码。

thanks to the AWS Support for the information provided感谢 AWS Support 提供的信息

leaved a message on the related github issue as well https://github.com/aws-amplify/amplify-category-api/issues/437#issuecomment-1351556948在相关的 github 问题以及https://github.com/aws-amplify/amplify-category-api/issues/437#issuecomment-1351556948上留言

All you need is to run你只需要跑

  • amplify override api放大覆盖 api
  • edit the corresponding overrode.ts编辑对应的overrode.ts

change the code with the resources.opensearch.OpenSearchStreamingLambdaFunction.code使用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资源:[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 [2]AWS::Lambda::Function 代码 - 属性 - https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html#aws-properties-lambda-函数代码属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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