-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_part.go
106 lines (93 loc) · 3.22 KB
/
index_part.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
/*
* go-leia
* Copyright (C) 2021 Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package leia
// IndexOption is the option function for adding options to a FieldIndexer
type IndexOption func(fieldIndexer *fieldIndexer)
// TransformerOption is the option for a FieldIndexer to apply transformation before indexing the value.
// The transformation is also applied to a query value that matches the indexed field.
func TransformerOption(transformer Transform) IndexOption {
return func(fieldIndexer *fieldIndexer) {
fieldIndexer.transformer = transformer
}
}
// TokenizerOption is the option for a FieldIndexer to split a value to be indexed into multiple parts.
// Each part is then indexed separately.
func TokenizerOption(tokenizer Tokenizer) IndexOption {
return func(fieldIndexer *fieldIndexer) {
fieldIndexer.tokenizer = tokenizer
}
}
// QueryPathComparable defines if two structs can be compared on query path.
type QueryPathComparable interface {
// Equals returns true if the two QueryPathComparable have the same search path.
Equals(other QueryPathComparable) bool
// QueryPath returns the QueryPath
QueryPath() QueryPath
}
// FieldIndexer is the public interface that defines functions for a field index instruction.
// A FieldIndexer is used when a document is indexed.
type FieldIndexer interface {
QueryPathComparable
// Tokenize may split up Keys and search terms. For example split a sentence into words.
Tokenize(value Scalar) []Scalar
// Transform is a function that alters the value to be indexed as well as any search criteria.
// For example LowerCase is a Transform function that transforms the value to lower case.
Transform(value Scalar) Scalar
}
// NewFieldIndexer creates a new fieldIndexer
func NewFieldIndexer(jsonPath QueryPath, options ...IndexOption) FieldIndexer {
fi := fieldIndexer{
queryPath: jsonPath,
}
for _, o := range options {
o(&fi)
}
return fi
}
type fieldIndexer struct {
queryPath QueryPath
transformer Transform
tokenizer Tokenizer
}
func (j fieldIndexer) Equals(other QueryPathComparable) bool {
return j.queryPath.Equals(other.QueryPath())
}
func (j fieldIndexer) QueryPath() QueryPath {
return j.queryPath
}
func (j fieldIndexer) Tokenize(scalar Scalar) []Scalar {
if j.tokenizer == nil {
return []Scalar{scalar}
}
if s, ok := scalar.(StringScalar); ok {
tokens := j.tokenizer(string(s))
result := make([]Scalar, len(tokens))
for i, t := range tokens {
result[i] = StringScalar(t)
}
return result
}
return []Scalar{scalar}
}
func (j fieldIndexer) Transform(value Scalar) Scalar {
if j.transformer == nil {
return value
}
return j.transformer(value)
}