forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
853 lines (770 loc) · 24 KB
/
search.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
// Copyright 2012 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
/*
Package search provides a client for App Engine's search service.
Indexes contains documents, and a document's contents are a mapping from case-
sensitive field names to values. In Go, documents are represented by struct
pointers, and the valid types for a struct's fields are:
- string,
- search.Atom,
- search.HTML,
- time.Time (stored with millisecond precision),
- float64 (value between -2,147,483,647 and 2,147,483,647 inclusive),
- appengine.GeoPoint.
Documents can also be represented by any type implementing the FieldLoadSaver
interface.
Example code:
type Doc struct {
Author string
Comment string
Creation time.Time
}
index, err := search.Open("comments")
if err != nil {
return err
}
newID, err := index.Put(c, "", &Doc{
Author: "gopher",
Comment: "the truth of the matter",
Creation: time.Now(),
})
if err != nil {
return err
}
Searching an index for a query will result in an iterator. As with an iterator
from package datastore, pass a destination struct to Next to decode the next
result. Next will return Done when the iterator is exhausted.
for t := index.Search(c, "Comment:truth", nil); ; {
var doc Doc
id, err := t.Next(&doc)
if err == search.Done {
break
}
if err != nil {
return err
}
fmt.Fprintf(w, "%s -> %#v\n", id, doc)
}
Call List to iterate over documents.
for t := index.List(c, nil); ; {
var doc Doc
id, err := t.Next(&doc)
if err == search.Done {
break
}
if err != nil {
return err
}
fmt.Fprintf(w, "%s -> %#v\n", id, doc)
}
A single document can also be retrieved by its ID. Pass a destination struct
to Get to hold the resulting document.
var doc Doc
err := index.Get(c, id, &doc)
if err != nil {
return err
}
Queries are expressed as strings, plus some optional parameters. The query
language is described at
https://cloud.google.com/appengine/docs/go/search/query_strings
Note that in Go, field names come from the struct field definition and begin
with an upper case letter.
*/
package search
// TODO: let Put specify the document language: "en", "fr", etc. Also: order_id?? storage??
// TODO: Index.GetAll (or Iterator.GetAll)?
// TODO: struct <-> protobuf tests.
// TODO: enforce Python's MIN_NUMBER_VALUE and MIN_DATE (which would disallow a zero
// time.Time)? _MAXIMUM_STRING_LENGTH?
import (
"errors"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/golang/protobuf/proto"
"google.golang.org/appengine"
"google.golang.org/appengine/internal"
pb "google.golang.org/appengine/internal/search"
)
var (
// ErrInvalidDocumentType is returned when methods like Put, Get or Next
// are passed a dst or src argument of invalid type.
ErrInvalidDocumentType = errors.New("search: invalid document type")
// ErrNoSuchDocument is returned when no document was found for a given ID.
ErrNoSuchDocument = errors.New("search: no such document")
)
// ErrFieldMismatch is returned when a field is to be loaded into a different
// than the one it was stored from, or when a field is missing or unexported in
// the destination struct.
type ErrFieldMismatch struct {
FieldName string
Reason string
}
func (e *ErrFieldMismatch) Error() string {
return fmt.Sprintf("search: cannot load field %q: %s", e.FieldName, e.Reason)
}
// Atom is a document field whose contents are indexed as a single indivisible
// string.
type Atom string
// HTML is a document field whose contents are indexed as HTML. Only text nodes
// are indexed: "foo<b>bar" will be treated as "foobar".
type HTML string
// validIndexNameOrDocID is the Go equivalent of Python's
// _ValidateVisiblePrintableAsciiNotReserved.
func validIndexNameOrDocID(s string) bool {
if strings.HasPrefix(s, "!") {
return false
}
for _, c := range s {
if c < 0x21 || 0x7f <= c {
return false
}
}
return true
}
var (
fieldNameRE = regexp.MustCompile(`^[A-Z][A-Za-z0-9_]*$`)
languageRE = regexp.MustCompile(`^[a-z]{2}$`)
)
// validFieldName is the Go equivalent of Python's _CheckFieldName.
func validFieldName(s string) bool {
return len(s) <= 500 && fieldNameRE.MatchString(s)
}
// validDocRank checks that the ranks is in the range [0, 2^31).
func validDocRank(r int) bool {
return 0 <= r && r <= (1<<31-1)
}
// validLanguage checks that a language looks like ISO 639-1.
func validLanguage(s string) bool {
return languageRE.MatchString(s)
}
// validFloat checks that f is in the range [-2147483647, 2147483647].
func validFloat(f float64) bool {
return -(1<<31-1) <= f && f <= (1<<31-1)
}
// Index is an index of documents.
type Index struct {
spec pb.IndexSpec
}
// orderIDEpoch forms the basis for populating OrderId on documents.
var orderIDEpoch = time.Date(2011, 1, 1, 0, 0, 0, 0, time.UTC)
// Open opens the index with the given name. The index is created if it does
// not already exist.
//
// The name is a human-readable ASCII string. It must contain no whitespace
// characters and not start with "!".
func Open(name string) (*Index, error) {
if !validIndexNameOrDocID(name) {
return nil, fmt.Errorf("search: invalid index name %q", name)
}
return &Index{
spec: pb.IndexSpec{
Name: &name,
},
}, nil
}
// Put saves src to the index. If id is empty, a new ID is allocated by the
// service and returned. If id is not empty, any existing index entry for that
// ID is replaced.
//
// The ID is a human-readable ASCII string. It must contain no whitespace
// characters and not start with "!".
//
// src must be a non-nil struct pointer or implement the FieldLoadSaver
// interface.
func (x *Index) Put(c appengine.Context, id string, src interface{}) (string, error) {
fields, meta, err := saveDoc(src)
if err != nil {
return "", err
}
d := &pb.Document{
Field: fields,
OrderId: proto.Int32(int32(time.Since(orderIDEpoch).Seconds())),
}
if meta != nil {
if meta.Rank != 0 {
if !validDocRank(meta.Rank) {
return "", fmt.Errorf("search: invalid rank %d, must be [0, 2^31)", meta.Rank)
}
*d.OrderId = int32(meta.Rank)
}
}
if id != "" {
if !validIndexNameOrDocID(id) {
return "", fmt.Errorf("search: invalid ID %q", id)
}
d.Id = proto.String(id)
}
req := &pb.IndexDocumentRequest{
Params: &pb.IndexDocumentParams{
Document: []*pb.Document{d},
IndexSpec: &x.spec,
},
}
res := &pb.IndexDocumentResponse{}
if err := c.Call("search", "IndexDocument", req, res, nil); err != nil {
return "", err
}
if len(res.Status) > 0 {
if s := res.Status[0]; s.GetCode() != pb.SearchServiceError_OK {
return "", fmt.Errorf("search: %s: %s", s.GetCode(), s.GetErrorDetail())
}
}
if len(res.Status) != 1 || len(res.DocId) != 1 {
return "", fmt.Errorf("search: internal error: wrong number of results (%d Statuses, %d DocIDs)",
len(res.Status), len(res.DocId))
}
return res.DocId[0], nil
}
// Get loads the document with the given ID into dst.
//
// The ID is a human-readable ASCII string. It must be non-empty, contain no
// whitespace characters and not start with "!".
//
// dst must be a non-nil struct pointer or implement the FieldLoadSaver
// interface.
//
// ErrFieldMismatch is returned when a field is to be loaded into a different
// type than the one it was stored from, or when a field is missing or
// unexported in the destination struct. ErrFieldMismatch is only returned if
// dst is a struct pointer. It is up to the callee to decide whether this error
// is fatal, recoverable, or ignorable.
func (x *Index) Get(c appengine.Context, id string, dst interface{}) error {
if id == "" || !validIndexNameOrDocID(id) {
return fmt.Errorf("search: invalid ID %q", id)
}
req := &pb.ListDocumentsRequest{
Params: &pb.ListDocumentsParams{
IndexSpec: &x.spec,
StartDocId: proto.String(id),
Limit: proto.Int32(1),
},
}
res := &pb.ListDocumentsResponse{}
if err := c.Call("search", "ListDocuments", req, res, nil); err != nil {
return err
}
if res.Status == nil || res.Status.GetCode() != pb.SearchServiceError_OK {
return fmt.Errorf("search: %s: %s", res.Status.GetCode(), res.Status.GetErrorDetail())
}
if len(res.Document) != 1 || res.Document[0].GetId() != id {
return ErrNoSuchDocument
}
metadata := &DocumentMetadata{
Rank: int(res.Document[0].GetOrderId()),
}
return loadDoc(dst, res.Document[0].Field, nil, metadata)
}
// Delete deletes a document from the index.
func (x *Index) Delete(c appengine.Context, id string) error {
req := &pb.DeleteDocumentRequest{
Params: &pb.DeleteDocumentParams{
DocId: []string{id},
IndexSpec: &x.spec,
},
}
res := &pb.DeleteDocumentResponse{}
if err := c.Call("search", "DeleteDocument", req, res, nil); err != nil {
return err
}
if len(res.Status) != 1 {
return fmt.Errorf("search: internal error: wrong number of results (%d)", len(res.Status))
}
if s := res.Status[0]; s.GetCode() != pb.SearchServiceError_OK {
return fmt.Errorf("search: %s: %s", s.GetCode(), s.GetErrorDetail())
}
return nil
}
// List lists all of the documents in an index. The documents are returned in
// increasing ID order.
func (x *Index) List(c appengine.Context, opts *ListOptions) *Iterator {
t := &Iterator{
c: c,
index: x,
count: -1,
listInclusive: true,
more: moreList,
limit: -1,
}
if opts != nil {
t.listStartID = opts.StartID
if opts.Limit > 0 {
t.limit = opts.Limit
}
t.idsOnly = opts.IDsOnly
}
return t
}
func moreList(t *Iterator) error {
req := &pb.ListDocumentsRequest{
Params: &pb.ListDocumentsParams{
IndexSpec: &t.index.spec,
},
}
if t.listStartID != "" {
req.Params.StartDocId = &t.listStartID
req.Params.IncludeStartDoc = &t.listInclusive
}
if t.limit > 0 {
req.Params.Limit = proto.Int32(int32(t.limit))
}
if t.idsOnly {
req.Params.KeysOnly = &t.idsOnly
}
res := &pb.ListDocumentsResponse{}
if err := t.c.Call("search", "ListDocuments", req, res, nil); err != nil {
return err
}
if res.Status == nil || res.Status.GetCode() != pb.SearchServiceError_OK {
return fmt.Errorf("search: %s: %s", res.Status.GetCode(), res.Status.GetErrorDetail())
}
t.listRes = res.Document
t.listStartID, t.listInclusive, t.more = "", false, nil
if len(res.Document) != 0 {
if id := res.Document[len(res.Document)-1].GetId(); id != "" {
t.listStartID, t.more = id, moreList
}
}
return nil
}
// ListOptions are the options for listing documents in an index. Passing a nil
// *ListOptions is equivalent to using the default values.
type ListOptions struct {
// StartID is the inclusive lower bound for the ID of the returned
// documents. The zero value means all documents will be returned.
StartID string
// Limit is the maximum number of documents to return. The zero value
// indicates no limit.
Limit int
// IDsOnly indicates that only document IDs should be returned for the list
// operation; no document fields are populated.
IDsOnly bool
}
// Search searches the index for the given query.
func (x *Index) Search(c appengine.Context, query string, opts *SearchOptions) *Iterator {
t := &Iterator{
c: c,
index: x,
searchQuery: query,
more: moreSearch,
limit: -1,
}
if opts != nil {
if opts.Limit > 0 {
t.limit = opts.Limit
}
t.fields = opts.Fields
t.idsOnly = opts.IDsOnly
t.sort = opts.Sort
t.exprs = opts.Expressions
}
return t
}
func moreSearch(t *Iterator) error {
req := &pb.SearchRequest{
Params: &pb.SearchParams{
IndexSpec: &t.index.spec,
Query: &t.searchQuery,
CursorType: pb.SearchParams_SINGLE.Enum(),
FieldSpec: &pb.FieldSpec{
Name: t.fields,
},
},
}
if t.limit > 0 {
req.Params.Limit = proto.Int32(int32(t.limit))
}
if t.idsOnly {
req.Params.KeysOnly = &t.idsOnly
}
if t.sort != nil {
if err := sortToProto(t.sort, req.Params); err != nil {
return err
}
}
for _, e := range t.exprs {
req.Params.FieldSpec.Expression = append(req.Params.FieldSpec.Expression, &pb.FieldSpec_Expression{
Name: proto.String(e.Name),
Expression: proto.String(e.Expr),
})
}
if t.searchCursor != nil {
req.Params.Cursor = t.searchCursor
}
res := &pb.SearchResponse{}
if err := t.c.Call("search", "Search", req, res, nil); err != nil {
return err
}
if res.Status == nil || res.Status.GetCode() != pb.SearchServiceError_OK {
return fmt.Errorf("search: %s: %s", res.Status.GetCode(), res.Status.GetErrorDetail())
}
t.searchRes = res.Result
t.count = int(*res.MatchedCount)
if res.Cursor != nil {
t.searchCursor, t.more = res.Cursor, moreSearch
} else {
t.searchCursor, t.more = nil, nil
}
return nil
}
// SearchOptions are the options for searching an index. Passing a nil
// *SearchOptions is equivalent to using the default values.
type SearchOptions struct {
// Limit is the maximum number of documents to return. The zero value
// indicates no limit.
Limit int
// IDsOnly indicates that only document IDs should be returned for the search
// operation; no document fields are populated.
IDsOnly bool
// Sort controls the ordering of search results.
Sort *SortOptions
// Fields specifies which document fields to include in the results. If omitted,
// all document fields are returned. No more than 100 fields may be specified.
Fields []string
// Expressions specifies additional computed fields to add to each returned
// document.
Expressions []FieldExpression
// TODO: cursor, offset, maybe others.
}
// FieldExpression defines a custom expression to evaluate for each result.
type FieldExpression struct {
// Name is the name to use for the computed field.
Name string
// Expr is evaluated to provide a custom content snippet for each document.
// See https://cloud.google.com/appengine/docs/go/search/options for
// the supported expression syntax.
Expr string
}
// SortOptions control the ordering and scoring of search results.
type SortOptions struct {
// Expressions is a slice of expressions representing a multi-dimensional
// sort.
Expressions []SortExpression
// Scorer, when specified, will cause the documents to be scored according to
// search term frequency.
Scorer Scorer
// Limit is the maximum number of objects to score and/or sort. Limit cannot
// be more than 10,000. The zero value indicates a default limit.
Limit int
}
// SortExpression defines a single dimension for sorting a document.
type SortExpression struct {
// Expr is evaluated to provide a sorting value for each document.
// See https://cloud.google.com/appengine/docs/go/search/options for
// the supported expression syntax.
Expr string
// Reverse causes the documents to be sorted in ascending order.
Reverse bool
// The default value to use when no field is present or the expresion
// cannot be calculated for a document. For text sorts, Default must
// be of type string; for numeric sorts, float64.
Default interface{}
}
// A Scorer defines how a document is scored.
type Scorer interface {
toProto(*pb.ScorerSpec)
}
type enumScorer struct {
enum pb.ScorerSpec_Scorer
}
func (e enumScorer) toProto(spec *pb.ScorerSpec) {
spec.Scorer = e.enum.Enum()
}
var (
// MatchScorer assigns a score based on term frequency in a document.
MatchScorer Scorer = enumScorer{pb.ScorerSpec_MATCH_SCORER}
// RescoringMatchScorer assigns a score based on the quality of the query
// match. It is similar to a MatchScorer but uses a more complex scoring
// algorithm based on match term frequency and other factors like field type.
// Please be aware that this algorithm is continually refined and can change
// over time without notice. This means that the ordering of search results
// that use this scorer can also change without notice.
RescoringMatchScorer Scorer = enumScorer{pb.ScorerSpec_RESCORING_MATCH_SCORER}
)
func sortToProto(sort *SortOptions, params *pb.SearchParams) error {
for _, e := range sort.Expressions {
spec := &pb.SortSpec{
SortExpression: proto.String(e.Expr),
}
if e.Reverse {
spec.SortDescending = proto.Bool(false)
}
if e.Default != nil {
switch d := e.Default.(type) {
case float64:
spec.DefaultValueNumeric = &d
case string:
spec.DefaultValueText = &d
default:
return fmt.Errorf("search: invalid Default type %T for expression %q", d, e.Expr)
}
}
params.SortSpec = append(params.SortSpec, spec)
}
spec := &pb.ScorerSpec{}
if sort.Limit > 0 {
spec.Limit = proto.Int32(int32(sort.Limit))
params.ScorerSpec = spec
}
if sort.Scorer != nil {
sort.Scorer.toProto(spec)
params.ScorerSpec = spec
}
return nil
}
// Iterator is the result of searching an index for a query or listing an
// index.
type Iterator struct {
c appengine.Context
index *Index
err error
listRes []*pb.Document
listStartID string
listInclusive bool
searchRes []*pb.SearchResult
searchQuery string
searchCursor *string
sort *SortOptions
fields []string
exprs []FieldExpression
more func(*Iterator) error
count int
limit int // items left to return; -1 for unlimited.
idsOnly bool
}
// Done is returned when a query iteration has completed.
var Done = errors.New("search: query has no more results")
// Count returns an approximation of the number of documents matched by the
// query. It is only valid to call for iterators returned by Search.
func (t *Iterator) Count() int { return t.count }
// Next returns the ID of the next result. When there are no more results,
// Done is returned as the error.
//
// dst must be a non-nil struct pointer, implement the FieldLoadSaver
// interface, or be a nil interface value. If a non-nil dst is provided, it
// will be filled with the indexed fields. dst is ignored if this iterator was
// created with an IDsOnly option.
func (t *Iterator) Next(dst interface{}) (string, error) {
if t.err == nil && len(t.listRes)+len(t.searchRes) == 0 && t.more != nil {
t.err = t.more(t)
}
if t.err != nil {
return "", t.err
}
var doc *pb.Document
var exprs []*pb.Field
switch {
case len(t.listRes) != 0:
doc = t.listRes[0]
t.listRes = t.listRes[1:]
case len(t.searchRes) != 0:
doc = t.searchRes[0].Document
exprs = t.searchRes[0].Expression
t.searchRes = t.searchRes[1:]
default:
return "", Done
}
if doc == nil {
return "", errors.New("search: internal error: no document returned")
}
if !t.idsOnly && dst != nil {
metadata := &DocumentMetadata{
Rank: int(doc.GetOrderId()),
}
if err := loadDoc(dst, doc.Field, exprs, metadata); err != nil {
return "", err
}
}
if t.limit > 0 {
t.limit--
if t.limit == 0 {
t.more = nil // prevent further fetches
}
}
return doc.GetId(), nil
}
// saveDoc converts from a struct pointer or FieldLoadSaver to protobufs.
func saveDoc(src interface{}) ([]*pb.Field, *DocumentMetadata, error) {
var err error
var fields []Field
var meta *DocumentMetadata
switch x := src.(type) {
case FieldLoadSaver:
fields, meta, err = x.Save()
default:
fields, err = SaveStruct(src)
}
if err != nil {
return nil, nil, err
}
f, err := fieldsToProto(fields)
return f, meta, err
}
func fieldsToProto(src []Field) ([]*pb.Field, error) {
// Maps to catch duplicate time or numeric fields.
timeFields, numericFields := make(map[string]bool), make(map[string]bool)
dst := make([]*pb.Field, 0, len(src))
for _, f := range src {
if !validFieldName(f.Name) {
return nil, fmt.Errorf("search: invalid field name %q", f.Name)
}
fieldValue := &pb.FieldValue{}
switch x := f.Value.(type) {
case string:
fieldValue.Type = pb.FieldValue_TEXT.Enum()
fieldValue.StringValue = proto.String(x)
case Atom:
fieldValue.Type = pb.FieldValue_ATOM.Enum()
fieldValue.StringValue = proto.String(string(x))
case HTML:
fieldValue.Type = pb.FieldValue_HTML.Enum()
fieldValue.StringValue = proto.String(string(x))
case time.Time:
if timeFields[f.Name] {
return nil, fmt.Errorf("search: duplicate time field %q", f.Name)
}
timeFields[f.Name] = true
fieldValue.Type = pb.FieldValue_DATE.Enum()
fieldValue.StringValue = proto.String(strconv.FormatInt(x.UnixNano()/1e6, 10))
case float64:
if numericFields[f.Name] {
return nil, fmt.Errorf("search: duplicate numeric field %q", f.Name)
}
if !validFloat(x) {
return nil, fmt.Errorf("search: numeric field %q with invalid value %f", f.Name, x)
}
numericFields[f.Name] = true
fieldValue.Type = pb.FieldValue_NUMBER.Enum()
fieldValue.StringValue = proto.String(strconv.FormatFloat(x, 'e', -1, 64))
case appengine.GeoPoint:
if !x.Valid() {
return nil, fmt.Errorf(
"search: GeoPoint field %q with invalid value %v",
f.Name, x)
}
fieldValue.Type = pb.FieldValue_GEO.Enum()
fieldValue.Geo = &pb.FieldValue_Geo{
Lat: proto.Float64(x.Lat),
Lng: proto.Float64(x.Lng),
}
default:
return nil, fmt.Errorf("search: unsupported field type: %v", reflect.TypeOf(f.Value))
}
if f.Language != "" {
switch f.Value.(type) {
case string, HTML:
if !validLanguage(f.Language) {
return nil, fmt.Errorf("search: invalid language for field %q: %q", f.Name, f.Language)
}
fieldValue.Language = proto.String(f.Language)
default:
return nil, fmt.Errorf("search: setting language not supported for field %q of type %T", f.Name, f.Value)
}
}
if p := fieldValue.StringValue; p != nil && !utf8.ValidString(*p) {
return nil, fmt.Errorf("search: %q field is invalid UTF-8: %q", f.Name, *p)
}
dst = append(dst, &pb.Field{
Name: proto.String(f.Name),
Value: fieldValue,
})
}
return dst, nil
}
// loadDoc converts from protobufs and document metadata to a struct pointer or
// FieldLoadSaver/FieldMetadataLoadSaver. Two slices of fields may be provided:
// src represents the document's stored fields; exprs is the derived expressions
// requested by the developer. The latter may be empty.
func loadDoc(dst interface{}, src, exprs []*pb.Field, meta *DocumentMetadata) (err error) {
fields, err := protoToFields(src)
if err != nil {
return err
}
if len(exprs) > 0 {
exprFields, err := protoToFields(exprs)
if err != nil {
return err
}
// Mark each field as derived.
for i := range exprFields {
exprFields[i].Derived = true
}
fields = append(fields, exprFields...)
}
switch x := dst.(type) {
case FieldLoadSaver:
return x.Load(fields, meta)
default:
return LoadStruct(dst, fields)
}
}
func protoToFields(fields []*pb.Field) ([]Field, error) {
dst := make([]Field, 0, len(fields))
for _, field := range fields {
fieldValue := field.GetValue()
f := Field{
Name: field.GetName(),
}
switch fieldValue.GetType() {
case pb.FieldValue_TEXT:
f.Value = fieldValue.GetStringValue()
f.Language = fieldValue.GetLanguage()
case pb.FieldValue_ATOM:
f.Value = Atom(fieldValue.GetStringValue())
case pb.FieldValue_HTML:
f.Value = HTML(fieldValue.GetStringValue())
f.Language = fieldValue.GetLanguage()
case pb.FieldValue_DATE:
sv := fieldValue.GetStringValue()
millis, err := strconv.ParseInt(sv, 10, 64)
if err != nil {
return nil, fmt.Errorf("search: internal error: bad time.Time encoding %q: %v", sv, err)
}
f.Value = time.Unix(0, millis*1e6)
case pb.FieldValue_NUMBER:
sv := fieldValue.GetStringValue()
x, err := strconv.ParseFloat(sv, 64)
if err != nil {
return nil, err
}
f.Value = x
case pb.FieldValue_GEO:
geoValue := fieldValue.GetGeo()
geoPoint := appengine.GeoPoint{geoValue.GetLat(), geoValue.GetLng()}
if !geoPoint.Valid() {
return nil, fmt.Errorf("search: internal error: invalid GeoPoint encoding: %v", geoPoint)
}
f.Value = geoPoint
default:
return nil, fmt.Errorf("search: internal error: unknown data type %s", fieldValue.GetType())
}
dst = append(dst, f)
}
return dst, nil
}
func namespaceMod(m proto.Message, namespace string) {
set := func(s **string) {
if *s == nil {
*s = &namespace
}
}
switch m := m.(type) {
case *pb.IndexDocumentRequest:
set(&m.Params.IndexSpec.Namespace)
case *pb.ListDocumentsRequest:
set(&m.Params.IndexSpec.Namespace)
case *pb.DeleteDocumentRequest:
set(&m.Params.IndexSpec.Namespace)
case *pb.SearchRequest:
set(&m.Params.IndexSpec.Namespace)
}
}
func init() {
internal.RegisterErrorCodeMap("search", pb.SearchServiceError_ErrorCode_name)
internal.NamespaceMods["search"] = namespaceMod
}