github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/inverted/searcher_value_extractors.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package inverted 13 14 import ( 15 "bytes" 16 "encoding/binary" 17 "fmt" 18 "time" 19 20 "github.com/pkg/errors" 21 ) 22 23 func (s *Searcher) extractNumberValue(in interface{}) ([]byte, error) { 24 value, ok := in.(float64) 25 if !ok { 26 return nil, fmt.Errorf("expected value to be float64, got %T", in) 27 } 28 29 return LexicographicallySortableFloat64(value) 30 } 31 32 // assumes an untyped int and stores as string-formatted int64 33 func (s *Searcher) extractIntValue(in interface{}) ([]byte, error) { 34 value, ok := in.(int) 35 if !ok { 36 return nil, fmt.Errorf("expected value to be int, got %T", in) 37 } 38 39 return LexicographicallySortableInt64(int64(value)) 40 } 41 42 // assumes an untyped int and stores as string-formatted int64 43 func (s *Searcher) extractIntCountValue(in interface{}) ([]byte, error) { 44 value, ok := in.(int) 45 if !ok { 46 return nil, fmt.Errorf("expected value to be int, got %T", in) 47 } 48 49 return LexicographicallySortableUint64(uint64(value)) 50 } 51 52 // assumes an untyped bool and stores as bool64 53 func (s *Searcher) extractBoolValue(in interface{}) ([]byte, error) { 54 value, ok := in.(bool) 55 if !ok { 56 return nil, fmt.Errorf("expected value to be bool, got %T", in) 57 } 58 59 buf := bytes.NewBuffer(nil) 60 if err := binary.Write(buf, binary.LittleEndian, value); err != nil { 61 return nil, errors.Wrap(err, "encode bool as binary") 62 } 63 64 return buf.Bytes(), nil 65 } 66 67 // assumes a time.Time date and stores as string-formatted int64, if it 68 // encounters a string it tries to parse it as a time.Time 69 func (s *Searcher) extractDateValue(in interface{}) ([]byte, error) { 70 var asInt64 int64 71 72 switch t := in.(type) { 73 case string: 74 parsed, err := time.Parse(time.RFC3339, t) 75 if err != nil { 76 return nil, errors.Wrap(err, "trying parse time as RFC3339 string") 77 } 78 79 asInt64 = parsed.UnixNano() 80 81 case time.Time: 82 asInt64 = t.UnixNano() 83 84 default: 85 return nil, fmt.Errorf("expected value to be time.Time (or parseable string)"+ 86 ", got %T", in) 87 } 88 89 return LexicographicallySortableInt64(asInt64) 90 }