应用引擎搜索语言



我正在尝试实现搜索。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
}

我收到此错误:错误.错误字符串:"搜索:INVALID_REQUEST:无效语言。语言应该是两个字母。

似乎python开发服务器将空语言字段处理为错误。

编辑:所以问题是我放置了多个具有相同名称的字段并将语言设置为空。这似乎是不允许的,因此当您使用具有相同名称的多个字段时,请确保也输入语言。

我不确定你的问题到底是什么,但在这里你可以看到你的想法(似乎python开发服务器将空语言字段处理为错误)是不正确的。

该文档中的代码片段

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"

但是,从搜索 api 源代码中可以看出:

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 是一个字符串。否则使用数字字段)

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.

最新更新