Appengine搜索语言

huangapple go评论67阅读模式
英文:

Appengine search language

问题

我正在尝试实现search.FieldLoadSaver接口,以便能够选择字段语言。

func (p *Product) Save() ([]search.Field, error) {
    var fields []search.Field

    // 添加 product.ID
    fields = append(fields, search.Field{Name: "ID", Value: search.Atom(p.ID)})

    // 添加 product.Name
    fields = append(fields, search.Field{Name: "Name", Value: p.Name, Language: "en"})

    return fields, nil
}

我得到了这个错误:errors.errorString{s:"search: INVALID_REQUEST: invalid language . Languages should be two letters."}

看起来Python devserver将空的语言字段视为错误。

编辑:所以问题是我放置了多个具有相同名称并且将语言设置为空的字段。看起来这是不允许的,所以当你使用多个具有相同名称的字段时,请确保也设置语言。

英文:

I'm trying to implement search.FieldLoadSaver interface to be able to pick field language.

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
}

And i'm getting this error : errors.errorString{s:"search: INVALID_REQUEST: invalid language . Languages should be two letters."}

It seems that python devserver handles empty language field as an error.

EDIT: so the problem was that i was putting multiple fields with the same name and setting language to be empty. It appears that this isn't allowed, so when you're using multiple fields with same name, make sure you're putting language also.

答案1

得分: 1

我不确定你的问题是什么,但是你可以在这里看到你认为的(似乎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"。

然而,正如在搜索 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 是一个字符串。否则使用 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.
英文:

I'm not sure what exactly your question is but here you can see that what you think (It seems that python devserver handles empty language field as an error.) is not true.

Code snippet from that documentation

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
}

As you can see, if you specify no language, it defaults to "en"

However, as can be seen in the 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.

In particular This class should not be directly instantiated.

There are some other Field subclasses that you should be using instead. For your current example you should use (assuming that p.id is a string. Otherwise use NumberField)

class TextField(Field):
  """A Field that has text content.

and

class AtomField(Field):
  """A Field that has content to be treated as a single token for indexing.

huangapple
  • 本文由 发表于 2014年11月19日 19:18:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/27015545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定