-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
105 lines (92 loc) · 2.79 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
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
package search
import (
"context"
"html/template"
"strconv"
"github.com/blugelabs/bluge"
"github.com/blugelabs/bluge/search/highlight"
"golang.org/x/exp/maps"
)
// A pool contains the documents and the search index reader.
type Pool[T any] struct {
documents []T
fields map[string]func(T) string // field name => getter, all given fields are indexed
reader *bluge.Reader
}
type Result[T any] struct {
Document T `json:"document"`
Highlights map[string]template.HTML `json:"highlights"` // key: field name, value: full content or fragment
}
func MakePool[T any](documents []T, fields map[string]func(T) string) (*Pool[T], error) {
var fieldNames = maps.Keys(fields)
var batch = bluge.NewBatch()
for i, doc := range documents {
id := strconv.Itoa(i) // slice index becomes document ID
blugeDoc := bluge.NewDocument(id)
for name, get := range fields {
value := get(doc)
blugeDoc.AddField(bluge.NewTextField(name, value).WithAnalyzer(normalizeAnalyzer).SearchTermPositions().StoreValue())
}
blugeDoc.AddField(bluge.NewCompositeFieldIncluding("_all", fieldNames))
batch.Update(blugeDoc.ID(), blugeDoc)
}
config := bluge.InMemoryOnlyConfig()
config.DefaultSearchAnalyzer.TokenFilters = append(config.DefaultSearchAnalyzer.TokenFilters, normalizeTokenFilter{}) // for search queries, not for index
index, err := bluge.OpenWriter(config)
if err != nil {
return nil, err
}
defer index.Close()
if err := index.Batch(batch); err != nil {
return nil, err
}
reader, err := index.Reader()
if err != nil {
return nil, err
}
return &Pool[T]{
documents: documents,
fields: fields,
reader: reader,
}, nil
}
func (pool *Pool[T]) Close() error {
return pool.reader.Close()
}
func (pool *Pool[T]) Search(request *bluge.TopNSearch) ([]Result[T], error) {
request = request.IncludeLocations()
iterator, err := pool.reader.Search(context.Background(), request)
if err != nil {
return nil, err
}
highlighter := highlight.NewHTMLHighlighter()
var results []Result[T]
for match, err := iterator.Next(); match != nil && err == nil; match, err = iterator.Next() {
var index int
var highlights = make(map[string]template.HTML)
if err := match.VisitStoredFields(func(field string, value []byte) bool {
switch field {
case "_id":
if i, err := strconv.Atoi(string(value)); err == nil {
index = i
}
case "_all":
// no highlighting
default:
if locations, ok := match.Locations[field]; ok {
if fragment := highlighter.BestFragment(locations, value); len(fragment) > 0 {
highlights[field] = template.HTML(fragment)
}
}
}
return true
}); err != nil {
return nil, err
}
results = append(results, Result[T]{
Document: pool.documents[index],
Highlights: highlights,
})
}
return results, nil
}