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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度