You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用新版CloudSearch Domain SDK遇域名解析错误及查询格式疑问

解决AWS CloudSearch Domain新版SDK查询错误及格式问题

错误原因解析

你遇到的getaddrinfo ENOTFOUND cloudsearchdomain.us-east-1.amazonaws.com错误,核心原因是没有指定你的CloudSearch域专属搜索端点。CloudSearch每个域都有唯一的定制化endpoint,而非通用的cloudsearchdomain.us-east-1.amazonaws.com,客户端无法通过通用地址定位到你的域。

代码修正及查询格式说明

1. 配置正确的域Endpoint

初始化CloudSearchDomainClient时,必须传入你域的专属搜索端点,这个地址可以在AWS CloudSearch控制台的域详情页"搜索端点"栏目复制。

2. 规范查询参数格式

新版SDK的SearchCommand支持多种查询语法,默认使用simple基础语法,你可以通过queryParser指定更灵活的语法类型,同时还能配置返回字段、分页等参数。

修改后的完整代码:

import {
  CloudSearchDomainClient,
  SearchCommand,
  SearchCommandInput
} from '@aws-sdk/client-cloudsearch-domain';

export class SearchService {
  private client: CloudSearchDomainClient;

  constructor() {
    // 替换为你的CloudSearch域的实际搜索端点
    const domainSearchEndpoint = 'search-your-domain-name-xxxxxx.us-east-1.cloudsearch.amazonaws.com';
    this.client = new CloudSearchDomainClient({
      region: 'us-east-1',
      endpoint: `https://${domainSearchEndpoint}`
    });
  }

  async GetSearchResults(query: string): Promise<any> {
    const params: SearchCommandInput = {
      query: query,
      queryParser: 'simple', // 可选语法:simple/structured/lucene/dismax
      size: 10, // 单页返回结果数量
      return: 'title,content,category' // 指定要返回的字段,需匹配你的域字段配置
    };

    try {
      const command = new SearchCommand(params);
      const response = await this.client.send(command);
      // 提取命中结果的字段数据,按需调整结构
      return response.hits?.hit.map(item => item.fields);
    } catch (error) {
      console.error('搜索请求失败:', error);
      throw error;
    }
  }
}

关键注意事项

  • 端点获取:登录AWS控制台进入你的CloudSearch域,在"域详情"页面找到"搜索端点",复制完整的地址(格式类似search-xxx-xxxx.us-east-1.cloudsearch.amazonaws.com)。
  • 查询语法选择:
    • simple:基础关键词匹配,支持AND/OR/NOT等逻辑词
    • structured:类SQL结构化查询,比如(title:'leg' OR content:'leg') AND status:'active'
    • lucene:Lucene原生语法,适合复杂查询场景
  • 权限验证:确保你的AWS凭证(环境变量、IAM角色等)拥有cloudsearch:search权限,否则会触发权限拒绝错误。

内容的提问来源于stack exchange,提问作者David Glass

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 03:23:29