美文网首页
AWS Lambda笔记-数据检索CloudSearch-14【

AWS Lambda笔记-数据检索CloudSearch-14【

作者: lazy_zhu | 来源:发表于2020-09-01 22:22 被阅读0次

AWS提供了一个非常方便的内容搜索服务(CloudSearch),用户可以快速构建自己的内容搜索,完善的管理功能和搜索功能具备高度可伸缩能力。CloudSearch是构建在SOLR(一种开放源码的、基于Lucene Java的搜索服务器)上的托管服务。如果需要构建更复杂的搜索服务功能可以使用Elasticsearch服务。

  1. 创建CloudSearch搜索域
  2. 测试创建的搜索域
  3. 创建Suggester
  4. 创建搜索API

工程说明

工程首先通过最简单的CloudSearch配置,完成搜索域,再通过上传测试数据初步体验搜索的乐趣。而真实的应用场景是通过一个API网关暴露给用户搜索,例如:/serach?q=搜索内容,与实际应用结合。


工程关系配置

1. 创建CloudSearch搜索域

创建CloudSearch最直接的方式是通过控制台创建,需要注意下,这边需要使用DynamoDB中注册用户表UserTable的数据做为测试数据,所以在创建CloudSearch时需要选择同一个Region。

创建搜索域
创建搜索实例时有很多选择,search.m1.small到search.m3.2xlarge系列,每个实例的硬件配置不一样,而且每个小时价格也不尽相同。当前测试数据不多选择最小实例,最少次数。CloudSearch提供免费试用最长30天,所以练习完成后需要及时关闭搜索域。
选择计算实例
免费限制和提醒
采用纯手工配置索引字段,选择的是DaynamoDB的UserTable表中字段。userid,email,username,注意这里的Name名称只能用小写字母或数字。由于UserTable表中的字段是字符串类型,所以Type类型选择text文本。Type类型:int,double,literal,date等。这边更快捷的方法是直接选择“Analyze sample item(s) from Amazon DynamoDB”让CloudSearch确定域中的可索引字段。
选择手工配置索引字段
配置索引字段
同其他AWS服务一样,还必须为CloudSearch定义访问策略。该策略允许公共访问Search和Suggester服务。但只允许当前AWS所有者上传的文档。
定义访问策略
{
 "Version": "2012-10-17",
   "Statement": [
    {
    "Effect": "Allow",
     "Principal": {
     "AWS":["*"]
    },
     "Action": [
            "cloudsearch:search",
            "cloudsearch:suggest"
           ]
    }
  ]
}
创建完毕

最后一步确认后,需要等待10分钟左右,等待创建的CloudSearch状态从:LOADING -> PROCESSING即可。

2. 测试创建的搜索域

为测试方便,直接从DynamoDB的UserTable表导入测试数据。点击“Upload Documents”,选择“Item(s) from Amazon DynamoDB” 若没找到DynamoDB Table,需要确认表是否存在,可能是当前的Region没有Table。


image.png

上一步完成后,点击“Run a Test Search”就可以直接Search相关内容。这里搜索“testuser7” 检索到图中相关数据。


image.png

3. 创建Suggester

Suggester是CloudSearch比较智能的功能之一,可用于构建自动完成系统。为username字段创建一个suggester,方便在后续检索字段中使用。在左侧菜单栏中选择“Suggester”,点击“Add Suggester”。这里的“username_suggester” 和一个“Source Field”字段一一匹配。在“Fuzzy Matching”选中某项,即关键字不以给定的参数开头,也可以容忍录入错误并搜索出更多相似的记录。


Suggester创建

创建完成之后,CloudSearch会再一次索引文档,需要花费一些时间。

4. 创建搜索API

将CloudSearch的搜索结果直接显示给客户端,我们可以使用类似S3上传图片的方式。使用API网关作为CloudSearch AWS API与我们面向公众的API之间的代理。
创建API,需要先创建一个“/search”路径的资源。将下面代码添加到CloudFormation模版的Resources中。

"SearchResource": {
  //在 API 中创建资源
  "Type": "AWS::ApiGateway::Resource",
  "Properties": {
    "PathPart": "search",
    //在其中创建此资源的RestApi资源的ID
    "RestApiId": {
      "Ref": "RestApi"
    },
    //创建子资源,则为父资源的ID,没有父资源的资源,指定RestApi根资源ID
    "ParentId": {
      "Fn::GetAtt": [
        "RestApi",
        "RootResourceId"
      ]
    }
  }
}

要将CloudSearch与API网关集成,我们需要知道为CloudSearch域自动生成的子域。这个子域的信息在CloudSearch 的Dashboard中找到,即“Search Endpoint”和“Document Endpoint”,这两个“search-” 和 “doc-” 开头不一样,其他内容相同。


子域信息

在CloudFormation中定义一个变量用于传递Search Endpoint值。

"CloudSearchDomain": {
  "Type": "String",
  "Description": "Endpoint Name for CloudSearch domain"
}

然后在build.gradle传递值。在build.gradle中找到“conventionMapping.stackParams”继续添加“CloudSearchDomain: serverlessbook-qchgogrsncf7jh3l5aoxqn72lu” , 这里的“serverlessbook-qchgogrsncf7jh3l5aoxqn72lu”只是Search Endpoint中间一段,其余部分在代码中拼接。
添加APIGateway的Method内容,需要注意的内容都在参数中添加。

"SearchGetMethod": {
  "Type": "AWS::ApiGateway::Method",
  "Properties": {
    "HttpMethod": "GET",
    "RestApiId": {
      "Ref": "RestApi"
    },
    "ResourceId": {
      "Ref": "SearchResource"
    },
    //APIGateway接受的请求参数
    "RequestParameters": {
      //查询参数,/serach?q=查询内容
      "method.request.querystring.q": "q"
    },
    "AuthorizationType": "NONE",
    //方法在收到请求时调用的后端系统
    "Integration": {
      //指定AWS类型,
      "Type": "AWS",
      "Uri": {
        "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:search-${CloudSearchDomain}.cloudsearch:path//2013-01-01/suggest"
      },
      "IntegrationHttpMethod": "GET",
      //集成所需的凭证
      "Credentials": {
        "Fn::GetAtt": [
          "ApiGatewayProxyRole",
          "Arn"
        ]
      },
      //后端请求一起发送的请求参数
      "RequestParameters": {

        //API网关级别添加suggester参数,并将请求参数传递给CloudSearch API。
        "integration.request.querystring.suggester": "'username_suggester'",
        "integration.request.querystring.q": "method.request.querystring.q"
      },
      "RequestTemplates": {
      },
      "PassthroughBehavior": "WHEN_NO_TEMPLATES",
      "IntegrationResponses": [
        {
          "SelectionPattern": ".*",
          "StatusCode": "200"
        }
      ]
    },
    "MethodResponses": [
      {
        "StatusCode": "200"
      }
    ]
  }
}

最后在APIDeployment中添加SearchGetMethod,防止在方法创建之前部署API。

"ApiDeployment": {
      "DependsOn": [
        "TestGetMethod",
        "SearchGetMethod"
      ]
......
}

部署程序./gradlew deploy, 在浏览器中搜索“https://serverless.自己的域名.com/search?q=testuser724”即可搜到相关内容,步骤非常简单快捷。

搜索结果

Github代码地址:https://github.com/zhujinhuant/serverlessbook/tree/master/serverlessbook-14

相关文章

网友评论

      本文标题:AWS Lambda笔记-数据检索CloudSearch-14【

      本文链接:https://www.haomeiwen.com/subject/gcmbsktx.html