duan5362 2015-02-26 07:37
浏览 29
已采纳

如何在沙发床视图中访问值?

I have a widget.json file which is loaded into a document in couchbase:

{
  "type": "widget",
  "name": "clicker",
  "description": "clicks!"
}

I also have a couchbase design document, couchbase.ddoc, for a bucket. It is registered with the name "views":

{
  "_id": "_design/accesscheck",
  "language": "javascript",
  "views": {
    "all_widgets": {
      "map": "function(doc, meta) { if(doc.type == 'widget') { emit(meta.id, doc.name); } }"
    }
  }
}

and some golang code, using the couchbase golang API, to fetch it:

opts := map[string]interface{}{
  "stale": false
}

data, _ := bucket.View("views", "all_widgets", opts)

and at this point i still have A Few Questions:

  1. what is the best way to determine what i can do with the "data" variable? I suspect it's a list of integer-indexed rows, each of which contains a key/value map where the value can be of different types. I see plenty of trivial examples for map[string]interface{}, but this seems to have an additional level of indirection. Easily understandable in C, IMHO, but the interface{}{} is puzzling to me.
  2. probably an extension of the above answer, but how can i effectively search using the design document? I would rather have the Couchbase server doing the sifting.
  3. some of the Get() examples pass in a struct type i.e.

    type User struct {
      Name string `json:"name"`
      Id   string `json:"id"`
    }
    
    err = bucket.Get("1", &user)
    if err != nil {
        log.Fatalf("Failed to get data from the cluster (%s)
    ", err)
    }
    fmt.Printf("Got back a user with a name of (%s) and id (%s)
    ", user.Name, user.Id)
    

Is it possible to do something like this with a View()?

  1. does somebody know of a good way to flexibly hook the View mechanism into a net/http REST handler? Just hoping..

I didn't find these questions covered in the golang client API examples or documentation. I probably missed something. If someone has links please let me know.

Thanks for any help!

  • 写回答

1条回答 默认 最新

  • dongshang6790 2015-02-27 02:53
    关注

    Customize View Result In Go

    If you are using github.com/couchbaselabs/go-couchbase, you can use bucket.ViewCustom to solve your problem. It accepts a item which view result parsed to.

    The bucket.View is just calling bucket.ViewCustom with predefined struct and returns it.

    An executed view result is a json object like below;

    {
        "total_rows": 123,
        "rows": [
            {
                "id": "id of document",
                "key": {key you emitted},
                "value": {value you emitted},
                "doc": {the document}
            },
            ...
        ]
    }
    

    As we are know this structure, we can set struct type manually to bucket.ViewCustom.

    In your case, you can write custom view struct for "all_widgets" like this;

    type AllWidgetsView struct {
        Total int `json:"total_rows"`
        Rows []struct {
            ID string    `json:"id"`      // document id
            Key string   `json:"string"`  // key you emitted, the 'meta.id'
            Value string `json:"value"`   // value you emitted, the 'doc.name'
        } `json:"rows"`
    }
    

    And use bucket.ViewCustom to retrieve the value.

    var result AllWidgetsView
    opts := map[string]interface{}{}
    viewErr := bucket.ViewCustom("views", "all_widgets", opts, &result)
    if viewErr != nil {
        // handle error
    }
    

    If this pattern of code appears frequently, you can refactor it.


    Effective searching

    For the question 2, it's not related to golang but Couchbase itself.

    View has some feature that bucket has not. View results are sorted by key so you can specify startkey option to where the result start. You can use this attribute and make view for searching. See Querying views for more information.

    But you need more detailed search like full-text search, you should use ElasticSearch plugin or N1QL to do it. (note the N1QL is preview and not officially released yet)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集