-
Notifications
You must be signed in to change notification settings - Fork 4
/
search.go
52 lines (42 loc) · 1.58 KB
/
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
// Content managed by Project Forge, see [projectforge.md] for details.
package search
import (
"context"
"strings"
"github.com/pkg/errors"
"projectforge.dev/projectforge/app"
"projectforge.dev/projectforge/app/lib/search/result"
"projectforge.dev/projectforge/app/lib/telemetry"
"projectforge.dev/projectforge/app/util"
)
type Provider func(context.Context, *app.State, *Params, util.Logger) (result.Results, error)
func Search(ctx context.Context, as *app.State, params *Params, logger util.Logger) (result.Results, []error) {
ctx, span, logger := telemetry.StartSpan(ctx, "search", logger)
defer span.Complete()
if params.Q == "" {
return nil, nil
}
var allProviders []Provider
// $PF_SECTION_START(search_functions)$
projectFunc := func(ctx context.Context, as *app.State, p *Params, logger util.Logger) (result.Results, error) {
return as.Services.Projects.Search(ctx, p.Q, logger)
}
moduleFunc := func(ctx context.Context, as *app.State, p *Params, logger util.Logger) (result.Results, error) {
return as.Services.Modules.Search(ctx, p.Q, logger)
}
allProviders = append(allProviders, projectFunc, moduleFunc)
// $PF_SECTION_END(search_functions)$
if len(allProviders) == 0 {
return nil, []error{errors.New("no search providers configured")}
}
params.Q = strings.TrimSpace(params.Q)
results, errs := util.AsyncCollect(allProviders, func(item Provider) (result.Results, error) {
return item(ctx, as, params, logger)
})
ret := make(result.Results, 0, len(results)*len(results))
for _, x := range results {
ret = append(ret, x...)
}
ret.Sort()
return ret, errs
}