@@ -3,7 +3,6 @@ package search
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
- "strconv"
7
6
"strings"
8
7
"time"
9
8
@@ -51,7 +50,8 @@ func toDSL(req *idl.SearchReq) ([]byte, error) {
51
50
}
52
51
must = append (must , map [string ]interface {}{
53
52
"bool" : map [string ]interface {}{
54
- "should" : conds ,
53
+ "should" : conds ,
54
+ "minimum_should_match" : 1 ,
55
55
},
56
56
})
57
57
}
@@ -109,23 +109,46 @@ func unionMulti(must []interface{}, mustNot []interface{}, field string, words s
109
109
}
110
110
111
111
func unionTime (must []interface {}, start , end string ) []interface {} {
112
- dates := strings .Split (start , "T" )
113
- st , err := time .ParseInLocation ("2006-01-02" , dates [0 ], time .Local )
114
- if err == nil {
115
- must = append (must , map [string ]interface {}{
116
- "range" : newCond ("CallFromInbound.OccurredAt" , "gte" , strconv .FormatInt (st .UnixNano (), 10 )),
117
- })
112
+ if start == "" && end == "" {
113
+ return must
118
114
}
119
- dates = strings .Split (end , "T" )
120
- ed , err := time .ParseInLocation ("2006-01-02" , dates [0 ], time .Local )
121
- if err == nil {
122
- must = append (must , map [string ]interface {}{
123
- "range" : newCond ("CallFromInbound.OccurredAt" , "lte" , strconv .FormatInt (ed .UnixNano ()+ 86400 * 1e9 , 10 )),
124
- })
115
+
116
+ timeRange := map [string ]int64 {}
117
+ if start != "" {
118
+ st , _ := parseTime (start )
119
+ timeRange ["gte" ] = st .UnixNano ()
120
+ }
121
+ if end != "" {
122
+ ed , hasTime := parseTime (end )
123
+ if ! hasTime {
124
+ ed = ed .Add (24 * time .Hour )
125
+ }
126
+ timeRange ["lt" ] = ed .UnixNano ()
125
127
}
128
+
129
+ must = append (must , map [string ]interface {}{
130
+ "range" : map [string ]interface {}{
131
+ "CallFromInbound.OccurredAt" : timeRange ,
132
+ },
133
+ })
126
134
return must
127
135
}
128
136
137
+ func parseTime (str string ) (time.Time , bool ) {
138
+ hasTime := true
139
+ if strings .Index (str , "T" ) < 0 {
140
+ str += "T00:00:00"
141
+ hasTime = false
142
+ }
143
+
144
+ layout := "2006-01-02T15:04:05"
145
+ t , err := time .ParseInLocation (layout , str , time .Local )
146
+ if err != nil {
147
+ return time .Now (), hasTime
148
+ }
149
+ return t , hasTime
150
+ }
151
+
129
152
func parseCond (field string , word string ) map [string ]interface {} {
130
153
if strings .ContainsAny (word , "?*" ) {
131
154
return newCond ("wildcard" , field , word )
0 commit comments