New client-cloudsearch-domain SDK domain spec

69 Views Asked by At

Been trying to pull search results from my cloudsearch domain. Every example online is for the older sdk. I did my best to compile this code, but I'm getting an error:

"getaddrinfo ENOTFOUND cloudsearchdomain.us-east-1.amazonaws.com"

I'm guessing the domain needs to be specified somewhere. Also, the query format is a bit obscure to me

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);
  }
}
1

There are 1 best solutions below

0
David Glass On

Figured it out between running it on the console, searching the package comments and online docs I was able to piece it together (and had some fun)

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);
  }
}