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)

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大