-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathapi_search.go
117 lines (103 loc) · 3.27 KB
/
api_search.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// SPDX-License-Identifier: Apache-2.0
//
// The OpenSearch Contributors require contributions made to
// this file be licensed under the Apache-2.0 license or a
// compatible open source license.
package opensearchapi
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/opensearch-project/opensearch-go/v4"
)
// Search executes a /_search request with the optional SearchReq
func (c Client) Search(ctx context.Context, req *SearchReq) (*SearchResp, error) {
if req == nil {
req = &SearchReq{}
}
var (
data SearchResp
err error
)
if data.response, err = c.do(ctx, req, &data); err != nil {
return &data, err
}
return &data, nil
}
// SearchReq represents possible options for the /_search request
type SearchReq struct {
Indices []string
Body io.Reader
Header http.Header
Params SearchParams
}
// GetRequest returns the *http.Request that gets executed by the client
func (r SearchReq) GetRequest() (*http.Request, error) {
var path string
if len(r.Indices) > 0 {
path = fmt.Sprintf("/%s/_search", strings.Join(r.Indices, ","))
} else {
path = "/_search"
}
return opensearch.BuildRequest(
"POST",
path,
r.Body,
r.Params.get(),
r.Header,
)
}
// SearchResp represents the returned struct of the /_search response
type SearchResp struct {
Took int `json:"took"`
Timeout bool `json:"timed_out"`
Shards ResponseShards `json:"_shards"`
Hits struct {
Total struct {
Value int `json:"value"`
Relation string `json:"relation"`
} `json:"total"`
MaxScore float32 `json:"max_score"`
Hits []SearchHit `json:"hits"`
} `json:"hits"`
Errors bool `json:"errors"`
Aggregations json.RawMessage `json:"aggregations"`
ScrollID *string `json:"_scroll_id,omitempty"`
Suggest map[string][]Suggest `json:"suggest,omitempty"`
response *opensearch.Response
}
// Inspect returns the Inspect type containing the raw *opensearch.Response
func (r SearchResp) Inspect() Inspect {
return Inspect{Response: r.response}
}
// SearchHit is a sub type of SearchResp containing information of the search hit with an unparsed Source field
type SearchHit struct {
Index string `json:"_index"`
ID string `json:"_id"`
Routing string `json:"_routing"`
Score float32 `json:"_score"`
Source json.RawMessage `json:"_source"`
Fields json.RawMessage `json:"fields"`
Type string `json:"_type"` // Deprecated field
Sort []any `json:"sort"`
Explanation *DocumentExplainDetails `json:"_explanation"`
SeqNo *int `json:"_seq_no"`
PrimaryTerm *int `json:"_primary_term"`
Highlight map[string][]string `json:"highlight"`
}
// Suggest is a sub type of SearchResp containing information of the suggest field
type Suggest struct {
Text string `json:"text"`
Offset int `json:"offset"`
Length int `json:"length"`
Options []struct {
Text string `json:"text"`
Score float32 `json:"score"`
Freq int `json:"freq"`
Highlighted string `json:"highlighted"`
CollateMatch bool `json:"collate_match"`
} `json:"options"`
}