-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
73 lines (62 loc) · 1.85 KB
/
query.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package sajari
import (
"golang.org/x/net/context"
pb "code.sajari.com/protogen-go/sajari/api/query/v1"
querypb "code.sajari.com/protogen-go/sajari/engine/query/v1"
)
// Query returns a handler for running queries using the Client.
func (c *Client) Query() *Query {
return &Query{c}
}
// Query is a handler which runs queries on a collection.
type Query struct {
c *Client
}
// Search performs an engine search with the Request r, returning a set of Results and non-nil error
// if there was a problem.
func (q *Query) Search(ctx context.Context, r *Request) (*Results, error) {
pr, err := r.proto()
if err != nil {
return nil, err
}
resp, err := pb.NewQueryClient(q.c.ClientConn).Search(q.c.newContext(ctx), pr)
if err != nil {
return nil, err
}
return processResponse(resp.SearchResponse, resp.Tokens)
}
// AnalyseMulti performs Analysis on multiple records against the same query request.
func (q *Query) AnalyseMulti(ctx context.Context, ks []*Key, r Request) ([][]string, error) {
pr, err := r.proto()
if err != nil {
return nil, err
}
pbks, err := keys(ks).proto()
if err != nil {
return nil, err
}
resp, err := querypb.NewQueryClient(q.c.ClientConn).Analyse(q.c.newContext(ctx), &querypb.AnalyseRequest{
SearchRequest: pr.SearchRequest,
Keys: pbks,
})
if err != nil {
return nil, err
}
out := make([][]string, 0, len(resp.Terms))
for _, ts := range resp.Terms {
out = append(out, ts.Terms)
}
return out, multiErrorFromRecordStatusProto(resp.Status)
}
// Analyse returns the list of overlapping terms between the record identified by k and the search
// search request r.
func (q *Query) Analyse(ctx context.Context, k *Key, r Request) ([]string, error) {
out, err := q.AnalyseMulti(ctx, []*Key{k}, r)
if err != nil {
if me, ok := err.(MultiError); ok {
return nil, me[0]
}
return nil, err
}
return out[0], nil
}