/
cloudsearch.go
56 lines (49 loc) · 1.97 KB
/
cloudsearch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package searchengine
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudsearchdomain"
"os"
)
/*NewClient create a new cloudsearchdomain.CloudSearchDomain instance. The instance is configured using
aws environment variables REGION, PROFILE, AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID.
*/
func NewClient() *cloudsearchdomain.CloudSearchDomain {
cloudSearchEndpoint := os.Getenv("CLOUD_SEARCH_ENDPOINT")
if cloudSearchEndpoint == "" {
panic("CLOUD_SEARCH_ENDPOINT env is empty. It must contains the endpoint value")
}
region := os.Getenv("REGION")
if region == "" {
panic("REGION env variable is empty. It must contains the region of the CloudSearch instance")
}
profile := os.Getenv("PROFILE")
cred := credentials.NewEnvCredentials()
config := aws.Config{
Credentials: cred,
Region: aws.String(region),
Endpoint: aws.String(cloudSearchEndpoint),
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
Profile: profile,
Config: config,
}))
// Create a CloudSearchDomain client from just a session.
svc := cloudsearchdomain.New(sess)
return svc
}
/*SearchEngine used aws CloudSearch to search element into the database or can be used to get search suggestions.
*/
type SearchEngine interface {
// Suggest take a string in parameter, this string contains the query of the user. The method will return
// suggestions based on this phrase.
Suggest(string, int64) (string, error)
//SimpleSearch take a query search all text and text-array fields for the specified string.
//Search for phrases, individual terms, and prefixes.
SimpleSearch(string, int64) (*cloudsearchdomain.SearchOutput, error)
//StructuredSearch search specific fields, construct compound queries using
//Boolean operators, and use advanced features such as term boosting and
//proximity searching.
StructuredSearch(string, int64) (*cloudsearchdomain.SearchOutput, error)
}