douwei2966 2014-11-19 11:18
浏览 11
已采纳

Appengine搜索语言

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条回答 默认 最新

  • dpo69086 2014-11-19 12:00
    关注

    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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败