-
Notifications
You must be signed in to change notification settings - Fork 8
/
search.go
44 lines (38 loc) · 1.11 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
package search
import (
"context"
"github.com/pkg/errors"
"github.com/sourcegraph/docsite/internal/search/index"
"github.com/sourcegraph/docsite/internal/search/query"
)
// Result is the result of a search.
type Result struct {
DocumentResults []DocumentResult // document results
Total int // total number of document results
}
// DocumentResult is the result of a search for a single document
type DocumentResult struct {
index.DocumentResult
SectionResults []SectionResult
}
func Search(ctx context.Context, query query.Query, index *index.Index) (*Result, error) {
result0, err := index.Search(ctx, query)
if err != nil {
return nil, err
}
result := &Result{
DocumentResults: make([]DocumentResult, len(result0.DocumentResults)),
Total: result0.Total,
}
for i, dr := range result0.DocumentResults {
srs, err := documentSectionResults(dr.Data, query)
if err != nil {
return nil, errors.WithMessagef(err, "document section results for %q", dr.ID)
}
result.DocumentResults[i] = DocumentResult{
DocumentResult: dr,
SectionResults: srs,
}
}
return result, nil
}