英文:
how do you access the values in a couchbase view?
问题
我有一个widget.json文件,它被加载到couchbase中的一个文档中:
{
"type": "widget",
"name": "clicker",
"description": "clicks!"
}
我还有一个couchbase设计文档,名为couchbase.ddoc,用于一个bucket。它以名称"views"注册:
{
"_id": "_design/accesscheck",
"language": "javascript",
"views": {
"all_widgets": {
"map": "function(doc, meta) { if(doc.type == 'widget') { emit(meta.id, doc.name); } }"
}
}
}
还有一些使用couchbase golang API的golang代码来获取它:
opts := map[string]interface{}{
"stale": false
}
data, _ := bucket.View("views", "all_widgets", opts)
到目前为止,我还有几个问题:
-
如何确定可以对"data"变量做什么?我怀疑它是一个整数索引行的列表,每个行包含一个键/值映射,其中值可以是不同类型的。我看到了很多map[string]interface{}的简单示例,但这似乎有一个额外的间接层。在C中很容易理解,但interface{}{}对我来说很困惑。
-
可能是上面问题的延伸,但我如何有效地使用设计文档进行搜索?我宁愿让Couchbase服务器进行筛选。
-
一些Get()的示例传入了一个结构体类型,例如:
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)\n", err) } fmt.Printf("Got back a user with a name of (%s) and id (%s)\n", user.Name, user.Id)
是否可以在View()中做类似的事情?
- 有人知道如何将View机制灵活地连接到net/http REST处理程序中吗?希望有人能帮忙。。
我在golang客户端API示例或文档中没有找到这些问题的答案。我可能错过了一些东西。如果有人有链接,请告诉我。
谢谢任何帮助!
英文:
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:
-
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.
-
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.
-
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)\n", err) } fmt.Printf("Got back a user with a name of (%s) and id (%s)\n", user.Name, user.Id)
Is it possible to do something like this with a View()?
- 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
得分: 1
在Go中自定义视图结果
如果你正在使用github.com/couchbaselabs/go-couchbase
,你可以使用bucket.ViewCustom
来解决你的问题。它接受一个将视图结果解析为项的参数。
bucket.View
只是调用bucket.ViewCustom
并返回预定义的结构体。
执行的视图结果是一个如下的JSON对象:
{
"total_rows": 123,
"rows": [
{
"id": "文档的ID",
"key": {你发射的键},
"value": {你发射的值},
"doc": {文档}
},
...
]
}
由于我们了解这个结构,我们可以手动设置结构体类型给bucket.ViewCustom
。
在你的情况下,你可以为"all_widgets"编写自定义视图结构体,像这样:
type AllWidgetsView struct {
Total int `json:"total_rows"`
Rows []struct {
ID string `json:"id"` // 文档ID
Key string `json:"key"` // 你发射的键,即'meta.id'
Value string `json:"value"` // 你发射的值,即'doc.name'
} `json:"rows"`
}
然后使用bucket.ViewCustom
来获取值。
var result AllWidgetsView
opts := map[string]interface{}{}
viewErr := bucket.ViewCustom("views", "all_widgets", opts, &result)
if viewErr != nil {
// 处理错误
}
如果这种代码模式经常出现,你可以进行重构。
有效的搜索
对于问题2,这与golang无关,而是与Couchbase本身有关。
视图具有一些bucket没有的功能。视图结果按键排序,因此你可以指定startkey
选项来指定结果的起始位置。你可以使用这个属性来创建用于搜索的视图。详细信息请参见查询视图。
但是,如果你需要更详细的搜索,比如全文搜索,你应该使用ElasticSearch插件或者N1QL
来实现。(注意,N1QL
是预览版,尚未正式发布)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论