field.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2014 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package search
  5. // Field is a name/value pair. A search index's document can be loaded and
  6. // saved as a sequence of Fields.
  7. type Field struct {
  8. // Name is the field name. A valid field name matches /[A-Za-z][A-Za-z0-9_]*/.
  9. Name string
  10. // Value is the field value. The valid types are:
  11. // - string,
  12. // - search.Atom,
  13. // - search.HTML,
  14. // - time.Time (stored with millisecond precision),
  15. // - float64,
  16. // - GeoPoint.
  17. Value interface{}
  18. // Language is a two-letter ISO 639-1 code for the field's language,
  19. // defaulting to "en" if nothing is specified. It may only be specified for
  20. // fields of type string and search.HTML.
  21. Language string
  22. // Derived marks fields that were calculated as a result of a
  23. // FieldExpression provided to Search. This field is ignored when saving a
  24. // document.
  25. Derived bool
  26. }
  27. // Facet is a name/value pair which is used to add categorical information to a
  28. // document.
  29. type Facet struct {
  30. // Name is the facet name. A valid facet name matches /[A-Za-z][A-Za-z0-9_]*/.
  31. // A facet name cannot be longer than 500 characters.
  32. Name string
  33. // Value is the facet value.
  34. //
  35. // When being used in documents (for example, in
  36. // DocumentMetadata.Facets), the valid types are:
  37. // - search.Atom,
  38. // - float64.
  39. //
  40. // When being used in SearchOptions.Refinements or being returned
  41. // in FacetResult, the valid types are:
  42. // - search.Atom,
  43. // - search.Range.
  44. Value interface{}
  45. }
  46. // DocumentMetadata is a struct containing information describing a given document.
  47. type DocumentMetadata struct {
  48. // Rank is an integer specifying the order the document will be returned in
  49. // search results. If zero, the rank will be set to the number of seconds since
  50. // 2011-01-01 00:00:00 UTC when being Put into an index.
  51. Rank int
  52. // Facets is the set of facets for this document.
  53. Facets []Facet
  54. }
  55. // FieldLoadSaver can be converted from and to a slice of Fields
  56. // with additional document metadata.
  57. type FieldLoadSaver interface {
  58. Load([]Field, *DocumentMetadata) error
  59. Save() ([]Field, *DocumentMetadata, error)
  60. }
  61. // FieldList converts a []Field to implement FieldLoadSaver.
  62. type FieldList []Field
  63. // Load loads all of the provided fields into l.
  64. // It does not first reset *l to an empty slice.
  65. func (l *FieldList) Load(f []Field, _ *DocumentMetadata) error {
  66. *l = append(*l, f...)
  67. return nil
  68. }
  69. // Save returns all of l's fields as a slice of Fields.
  70. func (l *FieldList) Save() ([]Field, *DocumentMetadata, error) {
  71. return *l, nil, nil
  72. }
  73. var _ FieldLoadSaver = (*FieldList)(nil)