@@ -3,7 +3,6 @@ package search
33import (
44 "encoding/json"
55 "fmt"
6- "strconv"
76 "strings"
87 "time"
98
@@ -51,7 +50,8 @@ func toDSL(req *idl.SearchReq) ([]byte, error) {
5150 }
5251 must = append (must , map [string ]interface {}{
5352 "bool" : map [string ]interface {}{
54- "should" : conds ,
53+ "should" : conds ,
54+ "minimum_should_match" : 1 ,
5555 },
5656 })
5757 }
@@ -109,23 +109,46 @@ func unionMulti(must []interface{}, mustNot []interface{}, field string, words s
109109}
110110
111111func 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
118114 }
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 ()
125127 }
128+
129+ must = append (must , map [string ]interface {}{
130+ "range" : map [string ]interface {}{
131+ "CallFromInbound.OccurredAt" : timeRange ,
132+ },
133+ })
126134 return must
127135}
128136
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+
129152func parseCond (field string , word string ) map [string ]interface {} {
130153 if strings .ContainsAny (word , "?*" ) {
131154 return newCond ("wildcard" , field , word )
0 commit comments