我正在尝试实现search.FieldLoadSaver接口,以便能够选择字段语言。
func (p *Product) Save() ([]search.Field, error) {
var fields []search.Field
// Add product.ID
fields = append(fields, search.Field{Name: "ID", Value: search.Atom(p.ID)})
// Add product.Name
fields = append(fields, search.Field{Name: "Name", Value: p.Name, Language: "en"})
return fields, nil
}
我收到此错误:errors.errorString {s:“search:INVALID_REQUEST:语言无效。语言应为两个字母。”}
似乎python devserver将空语言字段处理为错误。
编辑:所以问题在于我将多个字段设置为相同的名称并将设置语言设置为空。看来这是不允许的,所以当你使用同名的多个字段时,请确保你也使用语言。
答案 0 :(得分:1)
我不确定你的问题是什么,但here你可以看到你的想法(似乎python devserver处理空语言字段是一个错误。)不是真。
该文档中的代码段
type Field struct {
// Name is the field name. A valid field name matches /[A-Z][A-Za-z0-9_]*/.
// A field name cannot be longer than 500 characters.
Name string
// Value is the field value. The valid types are:
// - string,
// - search.Atom,
// - search.HTML,
// - time.Time (stored with millisecond precision),
// - float64,
// - appengine.GeoPoint.
Value interface{}
// Language is a two-letter ISO 693-1 code for the field's language,
// defaulting to "en" if nothing is specified. It may only be specified for
// fields of type string and search.HTML.
Language string
// Derived marks fields that were calculated as a result of a
// FieldExpression provided to Search. This field is ignored when saving a
// document.
Derived bool
}
如您所见,如果您未指定语言,则默认为“en”
但是,正如search api source code:
中所示class Field(object):
"""An abstract base class which represents a field of a document.
This class should not be directly instantiated.
"""
TEXT, HTML, ATOM, DATE, NUMBER, GEO_POINT = ('TEXT', 'HTML', 'ATOM', 'DATE',
'NUMBER', 'GEO_POINT')
_FIELD_TYPES = frozenset([TEXT, HTML, ATOM, DATE, NUMBER, GEO_POINT])
def __init__(self, name, value, language=None):
"""Initializer.
特别是This class should not be directly instantiated.
您应该使用其他一些Field子类。对于您当前的示例,您应该使用(假设p.id是一个字符串。否则使用NumberField)
class TextField(Field):
"""A Field that has text content.
和
class AtomField(Field):
"""A Field that has content to be treated as a single token for indexing.