-
Notifications
You must be signed in to change notification settings - Fork 720
/
min_should.go
152 lines (144 loc) · 3.48 KB
/
min_should.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
package zutils
import (
"fmt"
"math"
"regexp"
"sort"
"strconv"
"strings"
)
type combination struct {
condition int
count int
}
var regex = regexp.MustCompile(`(\d+)<([-+]?\d+%?)`)
// CalculateMin
// calculate the MinimumShouldMatch value with given expr and sub query count.
func CalculateMin(subCount int, v interface{}) (res int, err error) {
if subCount == 0 {
return 1, nil
}
defer func() {
if err != nil {
return
}
if res <= 1 {
res = 1
return
}
if res >= subCount {
res = subCount
return
}
}()
switch x := v.(type) {
case int64, int, float64:
m := 0
switch val := v.(type) {
case int:
m = val
case int64:
m = int(val)
case float64:
m = int(math.Floor(val))
}
if m < 0 {
m = subCount + m
}
return m, nil
case []string:
conditions := make([]combination, len(x))
for i, str := range x {
match := regex.FindStringSubmatch(str)
if match != nil {
leftPart := match[1]
rightPart := match[2]
condition, err := strconv.ParseInt(leftPart, 10, 64)
if err != nil {
return 0, fmt.Errorf("cannot parse the condition value: %w", err)
}
count, err := getPartValue(subCount, rightPart)
if err != nil {
return 0, fmt.Errorf("cannot parse the clauses count: %w", err)
}
conditions[i] = combination{
condition: int(condition),
count: count,
}
} else {
return 0, fmt.Errorf("invalid MinimumShould value: %s", x)
}
}
sort.Slice(conditions, func(i, j int) bool {
return conditions[i].condition < conditions[j].condition
})
for i, condition := range conditions {
// only match first
if subCount <= condition.condition {
return subCount, nil
}
// we are the last one, matched
if i == len(conditions)-1 {
return condition.count, nil
}
// less than next, we matched
if subCount <= conditions[i+1].condition {
return condition.count, nil
}
}
return 0, fmt.Errorf("invalid MinimumShould value: %v", x)
case string:
combinations := strings.Split(x, " ")
if len(combinations) > 1 {
return CalculateMin(subCount, combinations)
}
// simple expr
if res, err := getPartValue(subCount, x); err == nil {
return res, nil
}
// complex, we use regex
match := regex.FindStringSubmatch(x)
if match != nil {
leftPart := match[1]
rightPart := match[2]
condition, err := strconv.ParseInt(leftPart, 10, 64)
if err != nil {
return 0, fmt.Errorf("cannot parse the condition value: %w", err)
}
count, err := getPartValue(subCount, rightPart)
if err != nil {
return 0, fmt.Errorf("cannot parse the clauses count: %w", err)
}
if subCount <= int(condition) {
return subCount, nil
}
return count, nil
} else {
return 0, fmt.Errorf("invalid MinimumShould value: %s", x)
}
default:
return 0, fmt.Errorf("invalid MinimumShouldMatch value")
}
}
func getPartValue(termCount int, part string) (int, error) {
if strings.Contains(part, "%") {
proportion, err := strconv.ParseInt(part[0:len(part)-1], 10, 64)
if err != nil {
return 0, fmt.Errorf("cannot parse a percent value: %w", err)
}
if proportion < 0 {
count := float64(termCount) * float64(-proportion) / 100
return termCount - int(count), nil
}
count := float64(termCount) * float64(proportion) / 100
return int(count), nil
}
count, err := strconv.ParseInt(part, 10, 64)
if err != nil {
return 0, fmt.Errorf("cannot parse a int value: %w", err)
}
if count < 0 {
return termCount + int(count), nil
}
return int(count), nil
}