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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
package search
import (
"bytes"
"context"
"encoding/gob"
"log"
"log/slog"
"os"
"path"
"searchix/internal/file"
"searchix/internal/options"
"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/analysis/analyzer/custom"
"github.com/blevesearch/bleve/v2/analysis/analyzer/keyword"
"github.com/blevesearch/bleve/v2/analysis/analyzer/web"
"github.com/blevesearch/bleve/v2/analysis/token/camelcase"
"github.com/blevesearch/bleve/v2/analysis/tokenizer/letter"
"github.com/blevesearch/bleve/v2/analysis/tokenizer/single"
"github.com/blevesearch/bleve/v2/document"
"github.com/blevesearch/bleve/v2/mapping"
index "github.com/blevesearch/bleve_index_api"
"github.com/pkg/errors"
)
type WriteIndex struct {
index bleve.Index
indexMapping *mapping.IndexMappingImpl
}
const ExpectedIndexExtension = ".bleve"
func NewIndexer(indexPath string, force bool) (*WriteIndex, error) {
var err error
bleve.SetLog(log.Default())
exists, err := file.Exists(indexPath)
if err != nil {
return nil, errors.WithMessagef(
err,
"could not check if index exists at path %s",
indexPath,
)
}
if exists {
if !force {
return nil, errors.Errorf(
"index file %s already exists (use --force to replace)",
indexPath,
)
}
if path.Ext(indexPath) != ExpectedIndexExtension {
return nil, errors.Errorf(
"cowardly refusing to delete path %s (it doesn't end in '%s')",
indexPath,
ExpectedIndexExtension,
)
}
err := os.RemoveAll(indexPath)
if err != nil {
return nil, errors.WithMessagef(err, "could not remove index file %s", indexPath)
}
}
indexMapping := bleve.NewIndexMapping()
indexMapping.StoreDynamic = false
indexMapping.IndexDynamic = false
indexMapping.TypeField = "BleveType"
textFieldMapping := bleve.NewTextFieldMapping()
textFieldMapping.Store = false
descriptionFieldMapping := bleve.NewTextFieldMapping()
descriptionFieldMapping.Store = false
descriptionFieldMapping.Analyzer = web.Name
err = indexMapping.AddCustomAnalyzer("option_name", map[string]interface{}{
"type": custom.Name,
"tokenizer": letter.Name,
"token_filters": []string{
camelcase.Name,
},
})
if err != nil {
return nil, errors.WithMessage(err, "could not add custom analyser")
}
err = indexMapping.AddCustomAnalyzer("loc", map[string]interface{}{
"type": keyword.Name,
"tokenizer": letter.Name,
"token_filters": []string{
camelcase.Name,
},
})
if err != nil {
return nil, errors.WithMessage(err, "could not add custom analyser")
}
err = indexMapping.AddCustomAnalyzer("keyword_single", map[string]interface{}{
"type": keyword.Name,
"tokenizer": single.Name,
})
if err != nil {
return nil, errors.WithMessage(err, "could not add custom analyser")
}
keywordFieldMapping := bleve.NewKeywordFieldMapping()
keywordFieldMapping.Analyzer = "keyword_single"
nameMapping := bleve.NewTextFieldMapping()
nameMapping.Analyzer = "option_name"
nameMapping.IncludeTermVectors = true
nameMapping.Store = false
nixValueMapping := bleve.NewDocumentStaticMapping()
nixValueMapping.AddFieldMappingsAt("Text", textFieldMapping)
nixValueMapping.AddFieldMappingsAt("Markdown", textFieldMapping)
locFieldMapping := bleve.NewKeywordFieldMapping()
locFieldMapping.Analyzer = "loc"
locFieldMapping.IncludeTermVectors = true
locFieldMapping.Store = false
optionMapping := bleve.NewDocumentStaticMapping()
optionMapping.AddFieldMappingsAt("Name", keywordFieldMapping)
optionMapping.AddFieldMappingsAt("Source", keywordFieldMapping)
optionMapping.AddFieldMappingsAt("Loc", locFieldMapping)
optionMapping.AddFieldMappingsAt("RelatedPackages", textFieldMapping)
optionMapping.AddFieldMappingsAt("Description", textFieldMapping)
optionMapping.AddSubDocumentMapping("Default", nixValueMapping)
optionMapping.AddSubDocumentMapping("Example", nixValueMapping)
indexMapping.AddDocumentMapping("option", optionMapping)
idx, err := bleve.New(indexPath, indexMapping)
if err != nil {
return nil, errors.WithMessagef(err, "unable to create index at path %s", indexPath)
}
return &WriteIndex{
idx,
indexMapping,
}, nil
}
func (i *WriteIndex) ImportOptions(
ctx context.Context,
objects <-chan *options.NixOption,
) <-chan error {
var err error
errs := make(chan error)
go func() {
defer close(errs)
batch := i.index.NewBatch()
outer:
for opt := range objects {
select {
case <-ctx.Done():
slog.Debug("context cancelled")
break outer
default:
}
doc := document.NewDocument(opt.Source + "/" + opt.Name)
err = i.indexMapping.MapDocument(doc, opt)
if err != nil {
errs <- errors.WithMessagef(err, "could not map document for option: %s", opt.Name)
continue
}
var data bytes.Buffer
enc := gob.NewEncoder(&data)
err = enc.Encode(opt)
if err != nil {
errs <- errors.WithMessage(err, "could not store option in search index")
continue
}
field := document.NewTextFieldWithIndexingOptions("_data", nil, data.Bytes(), index.StoreField)
newDoc := doc.AddField(field)
// slog.Debug("adding option to index", "name", opt.Name)
err = batch.IndexAdvanced(newDoc)
if err != nil {
errs <- errors.WithMessagef(err, "could not index option %s", opt.Name)
continue
}
}
size := batch.Size()
slog.Debug("flushing batch", "size", size)
err := i.index.Batch(batch)
if err != nil {
errs <- errors.WithMessagef(err, "could not flush batch")
}
}()
return errs
}
func (i *WriteIndex) Close() error {
err := i.index.Close()
if err != nil {
return errors.WithMessagef(err, "could not close index")
}
return nil
}
|