一直在尝试从我的云搜索域中提取搜索结果。网上的每个示例都是针对旧版 sdk 的。我尽力编译此代码,但出现错误:
“getaddrinfo ENOTFOUND cloudsearchdomain.us-east-1.amazonaws.com”
我猜需要在某个地方指定域。另外,查询格式对我来说有点晦涩
import {
CloudSearchDomainClient,
SearchCommand,
SearchCommandInput
} from '@aws-sdk/client-cloudsearch-domain';
export class SearchService {
private client: CloudSearchDomainClient;
constructor() {
this.client = new CloudSearchDomainClient({
region: 'us-east-1',
apiVersion: '2013-01-01'
});
}
async GetSearchResults(query: string): Promise<any> {
const params: SearchCommandInput = {
query: 'leg'
};
const command = new SearchCommand(params);
return await this.client.send(command);
}
}
在控制台上运行它、搜索包注释和在线文档之间找到了答案,我能够将其拼凑在一起(并且玩得很开心)
import {
CloudSearchDomainClient,
SearchCommand,
SearchCommandInput
} from '@aws-sdk/client-cloudsearch-domain';
export class SearchService {
private client: CloudSearchDomainClient;
constructor() {
this.client = new CloudSearchDomainClient({
region: 'us-east-1',
apiVersion: '2013-01-01',
endpoint:
'https://search-exercises-xxx.us-east-1.cloudsearch.amazonaws.com'
});
}
async GetSearchResults(query: string): Promise<any> {
const highlight = JSON.stringify({
id: { max_phrases: 3, format: 'text', pre_tag: '*#*', post_tag: '*%*' },
name: { max_phrases: 3, format: 'text', pre_tag: '*#*', post_tag: '*%*' }
});
const params: SearchCommandInput = {
query,
highlight,
return: '_all_fields,_score',
sort: '_score desc'
};
const command = new SearchCommand(params);
return await this.client.send(command);
}
}