-
Notifications
You must be signed in to change notification settings - Fork 1
/
tantivycontext.go
326 lines (296 loc) · 10.3 KB
/
tantivycontext.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package tantivy_go
// #include "bindings.h"
import "C"
import (
"errors"
"fmt"
"sync"
"unsafe"
)
type TantivyContext struct {
ptr *C.TantivyContext
lock sync.Mutex // tantivy writer commits should be executed exclusively
}
// NewTantivyContextWithSchema creates a new instance of TantivyContext with the provided schema.
//
// Parameters:
// - path: The path to the index as a string.
// - schema: A pointer to the Schema to be used.
//
// Returns:
// - *TantivyContext: A pointer to a newly created TantivyContext instance.
// - error: An error if the index creation fails.
func NewTantivyContextWithSchema(path string, schema *Schema) (*TantivyContext, error) {
cPath := C.CString(path)
defer C.string_free(cPath)
var errBuffer *C.char
ptr := C.context_create_with_schema(cPath, schema.ptr, &errBuffer)
if ptr == nil {
defer C.string_free(errBuffer)
return nil, errors.New(C.GoString(errBuffer))
}
return &TantivyContext{ptr: ptr}, nil
}
// AddAndConsumeDocuments adds and consumes the provided documents to the index.
//
// Parameters:
// - docs: A variadic parameter of pointers to Document to be added and consumed.
//
// Returns:
// - error: An error if adding and consuming the documents fails.
func (tc *TantivyContext) AddAndConsumeDocuments(docs ...*Document) error {
tc.lock.Lock()
defer tc.lock.Unlock()
if len(docs) == 0 {
return nil
}
var errBuffer *C.char
docsPtr := make([]*C.Document, len(docs))
for j, doc := range docs {
docsPtr[j] = doc.ptr
}
C.context_add_and_consume_documents(tc.ptr, &docsPtr[0], C.uintptr_t(len(docs)), &errBuffer)
for _, doc := range docs {
// Free the strings in the document
// This is necessary because the document is consumed by the index
// and the strings are not freed by the index
// We might clone strings on the Rust side to avoid that, but that would be inefficient
doc.FreeStrings()
}
return tryExtractError(errBuffer)
}
// DeleteDocuments deletes documents from the index based on the specified field and IDs.
//
// Parameters:
// - field: The field name to match against the document IDs.
// - deleteIds: A variadic parameter of document IDs to be deleted.
//
// Returns:
// - error: An error if deleting the documents fails.
func (tc *TantivyContext) DeleteDocuments(field string, deleteIds ...string) error {
tc.lock.Lock()
defer tc.lock.Unlock()
if len(deleteIds) == 0 {
return nil
}
cField := C.CString(field)
defer C.string_free(cField)
deleteIDsPtr := make([]*C.char, len(deleteIds))
for j, id := range deleteIds {
cID := C.CString(id)
defer C.free(unsafe.Pointer(cID))
deleteIDsPtr[j] = cID
}
cDeleteIds := (**C.char)(unsafe.Pointer(&deleteIDsPtr[0]))
var errBuffer *C.char
C.context_delete_documents(tc.ptr, cField, cDeleteIds, C.uintptr_t(len(deleteIds)), &errBuffer)
return tryExtractError(errBuffer)
}
// NumDocs returns the number of documents in the index.
//
// Returns:
// - uint64: The number of documents.
// - error: An error if retrieving the document count fails.
func (tc *TantivyContext) NumDocs() (uint64, error) {
var errBuffer *C.char
numDocs := C.context_num_docs(tc.ptr, &errBuffer)
if errBuffer != nil {
defer C.string_free(errBuffer)
return 0, errors.New(C.GoString(errBuffer))
}
return uint64(numDocs), nil
}
// Search performs a search query on the index and returns the search results.
//
// Parameters:
// - sCtx (SearchContext): The context for the search, containing query string,
// document limit, highlight option, and field weights.
//
// Returns:
// - *SearchResult: A pointer to the SearchResult containing the search results.
// - error: An error if the search fails.
func (tc *TantivyContext) Search(sCtx SearchContext) (*SearchResult, error) {
fieldNames, weights := sCtx.GetFieldAndWeights()
if len(fieldNames) == 0 {
return nil, fmt.Errorf("fieldNames must not be empty")
}
cQuery := C.CString(sCtx.GetQuery())
defer C.string_free(cQuery)
fieldNamesPtr := make([]*C.char, len(fieldNames))
for j, id := range fieldNames {
cId := C.CString(id)
defer C.free(unsafe.Pointer(cId))
fieldNamesPtr[j] = cId
}
fieldWeightsPtr := make([]C.float, len(fieldNames))
for j, weight := range weights {
fieldWeightsPtr[j] = C.float(weight)
}
var errBuffer *C.char
ptr := C.context_search(
tc.ptr,
(**C.char)(unsafe.Pointer(&fieldNamesPtr[0])),
(*C.float)(unsafe.Pointer(&fieldWeightsPtr[0])),
C.uintptr_t(len(fieldNames)),
cQuery,
&errBuffer,
pointerCType(sCtx.GetDocsLimit()),
C.bool(sCtx.WithHighlights()),
)
if ptr == nil {
defer C.string_free(errBuffer)
return nil, errors.New(C.GoString(errBuffer))
}
return &SearchResult{ptr: ptr}, nil
}
// SearchJson performs a simplified search query on the index and returns the search results.
//
// Parameters:
// - sCtx (SearchContext): The context for the search, containing query string,
// document limit, and highlight option.
//
// Returns:
// - *SearchResult: A pointer to the SearchResult containing the search results.
// - error: An error if the search fails.
func (tc *TantivyContext) SearchJson(sCtx SearchContext) (*SearchResult, error) {
// Ensure the query is valid
cQuery := C.CString(sCtx.GetQuery())
defer C.string_free(cQuery)
// Prepare the error buffer
var errBuffer *C.char
// Call the C function
ptr := C.context_search_json(
tc.ptr,
cQuery,
&errBuffer,
pointerCType(sCtx.GetDocsLimit()),
C.bool(sCtx.WithHighlights()),
)
if ptr == nil {
defer C.string_free(errBuffer)
return nil, errors.New(C.GoString(errBuffer))
}
return &SearchResult{ptr: ptr}, nil
}
func (tc *TantivyContext) Free() {
C.context_free(tc.ptr)
}
// RegisterTextAnalyzerNgram registers a text analyzer using N-grams with the index.
//
// Parameters:
// - tokenizerName (string): The name of the tokenizer to be used.
// - minGram (uintptr): The minimum length of the n-grams.
// - maxGram (uintptr): The maximum length of the n-grams.
// - prefixOnly (bool): Whether to generate only prefix n-grams.
//
// Returns:
// - error: An error if the registration fails.
func (tc *TantivyContext) RegisterTextAnalyzerNgram(tokenizerName string, minGram, maxGram uintptr, prefixOnly bool) error {
cTokenizerName := C.CString(tokenizerName)
defer C.string_free(cTokenizerName)
var errBuffer *C.char
C.context_register_text_analyzer_ngram(tc.ptr, cTokenizerName, C.uintptr_t(minGram), C.uintptr_t(maxGram), C.bool(prefixOnly), &errBuffer)
return tryExtractError(errBuffer)
}
// RegisterTextAnalyzerEdgeNgram registers a text analyzer using edge n-grams with the index.
//
// Parameters:
// - tokenizerName (string): The name of the tokenizer to be used.
// - minGram (uintptr): The minimum length of the edge n-grams.
// - maxGram (uintptr): The maximum length of the edge n-grams.
// - limit (uintptr): The maximum number of edge n-grams to generate.
//
// Returns:
// - error: An error if the registration fails.
func (tc *TantivyContext) RegisterTextAnalyzerEdgeNgram(tokenizerName string, minGram, maxGram uintptr, limit uintptr) error {
cTokenizerName := C.CString(tokenizerName)
defer C.string_free(cTokenizerName)
var errBuffer *C.char
C.context_register_text_analyzer_edge_ngram(tc.ptr, cTokenizerName, C.uintptr_t(minGram), C.uintptr_t(maxGram), C.uintptr_t(limit), &errBuffer)
return tryExtractError(errBuffer)
}
// RegisterTextAnalyzerSimple registers a simple text analyzer with the index.
//
// Parameters:
// - tokenizerName (string): The name of the tokenizer to be used.
// - textLimit (uintptr): The limit on the length of the text to be analyzed.
// - lang (string): The language code for the text analyzer.
//
// Returns:
// - error: An error if the registration fails.
func (tc *TantivyContext) RegisterTextAnalyzerSimple(tokenizerName string, textLimit uintptr, lang string) error {
cTokenizerName := C.CString(tokenizerName)
defer C.string_free(cTokenizerName)
cLang := C.CString(lang)
defer C.string_free(cLang)
var errBuffer *C.char
C.context_register_text_analyzer_simple(tc.ptr, cTokenizerName, C.uintptr_t(textLimit), cLang, &errBuffer)
return tryExtractError(errBuffer)
}
// RegisterTextAnalyzerJieba registers a jieba text analyzer with the index.
//
// Parameters:
// - tokenizerName (string): The name of the tokenizer to be used.
// - textLimit (uintptr): The limit on the length of the text to be analyzed.
//
// Returns:
// - error: An error if the registration fails.
func (tc *TantivyContext) RegisterTextAnalyzerJieba(tokenizerName string, textLimit uintptr) error {
cTokenizerName := C.CString(tokenizerName)
defer C.string_free(cTokenizerName)
var errBuffer *C.char
C.context_register_jieba_tokenizer(tc.ptr, cTokenizerName, C.uintptr_t(textLimit), &errBuffer)
return tryExtractError(errBuffer)
}
// RegisterTextAnalyzerRaw registers a raw text analyzer with the index.
//
// Parameters:
// - tokenizerName (string): The name of the raw tokenizer to be used.
//
// Returns:
// - error: An error if the registration fails.
func (tc *TantivyContext) RegisterTextAnalyzerRaw(tokenizerName string) error {
cTokenizerName := C.CString(tokenizerName)
defer C.string_free(cTokenizerName)
var errBuffer *C.char
C.context_register_text_analyzer_raw(tc.ptr, cTokenizerName, &errBuffer)
return tryExtractError(errBuffer)
}
// GetSearchResults extracts search results from a SearchResult and converts them into a slice of models.
//
// Parameters:
// - searchResult (*SearchResult): The search results to process.
// - schema (*Schema): The schema to use for converting documents to models.
// - f (func(json string) (T, error)): A function to convert JSON strings to models.
// - includeFields (...string): Optional list of fields to include in the result.
//
// Returns:
// - ([]T, error): A slice of models obtained from the search results, and an error if something goes wrong.
func GetSearchResults[T any](
searchResult *SearchResult,
schema *Schema,
f func(json string) (T, error),
includeFields ...string,
) ([]T, error) {
var models []T
defer searchResult.Free()
size, err := searchResult.GetSize()
if err != nil {
fmt.Println("Failed to get search result size:", err)
return nil, err
}
// Iterate through search results
for next := range size {
doc, err := searchResult.Get(next)
if err != nil {
break
}
model, err := ToModel(doc, schema, includeFields, f)
if err != nil {
return nil, err
}
models = append(models, model)
doc.Free()
}
return models, nil
}