(ctx context.Context, query string, opts map[string]any)
| 39 | func (m *MeilisearchKB) Name() string { return m.name } |
| 40 | |
| 41 | func (m *MeilisearchKB) Retrieve(ctx context.Context, query string, opts map[string]any) (types.RetrievalResult, error) { |
| 42 | topK := effectiveTopK(m.cfg, opts, 8) |
| 43 | base := strings.TrimRight(strings.TrimSpace(m.cfg.Endpoint), "/") |
| 44 | uid := url.PathEscape(strings.TrimSpace(m.cfg.Index)) |
| 45 | u, err := url.Parse(base + "/indexes/" + uid + "/search") |
| 46 | if err != nil { |
| 47 | return types.RetrievalResult{}, err |
| 48 | } |
| 49 | q := u.Query() |
| 50 | q.Set("q", query) |
| 51 | q.Set("limit", fmt.Sprintf("%d", topK)) |
| 52 | u.RawQuery = q.Encode() |
| 53 | |
| 54 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) |
| 55 | if err != nil { |
| 56 | return types.RetrievalResult{}, err |
| 57 | } |
| 58 | for k, v := range m.cfg.Headers { |
| 59 | if strings.TrimSpace(k) != "" && strings.TrimSpace(v) != "" { |
| 60 | req.Header.Set(k, v) |
| 61 | } |
| 62 | } |
| 63 | if req.Header.Get("Authorization") == "" { |
| 64 | req.Header.Set("Authorization", "Bearer "+strings.TrimSpace(m.cfg.APIKey)) |
| 65 | } |
| 66 | |
| 67 | resp, err := m.client.Do(req) |
| 68 | if err != nil { |
| 69 | return types.RetrievalResult{}, err |
| 70 | } |
| 71 | defer resp.Body.Close() |
| 72 | b, err := io.ReadAll(resp.Body) |
| 73 | if err != nil { |
| 74 | return types.RetrievalResult{}, err |
| 75 | } |
| 76 | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 77 | return types.RetrievalResult{}, fmt.Errorf("meilisearch %s: %s: %s", m.name, resp.Status, truncateForErr(b, 512)) |
| 78 | } |
| 79 | return parseMeilisearchHits(b) |
| 80 | } |
| 81 | |
| 82 | func parseMeilisearchHits(b []byte) (types.RetrievalResult, error) { |
| 83 | var root struct { |
nothing calls this directly
no test coverage detected