我正在使用 gos 库 ( https://github.com/OwnLocal/goes ),它是 Golang 中 ElasticSearch 的包装器。
在 ElasticSearch 查询中,我们可以这样运行:
{
"query": {
"match": {
"user_id_1": "438018"
}
}
}
而且有效。
对于使用 gos 的 golang,你可以像这样运行它:
var query = map[string]interface{}{
"query": map[string]interface{}{
"match": map[string]interface{}{
"user_id_1": "438018",
},
},
}
这是我的问题,你可以在elasticsearch中使用这个查询
{
"query": {
"bool": {
"should": [
{
"match": {
"year": "2016"
}
},
{
"match": {
"frequency": "7"
}
}
]
}
}
}
在上面的代码中,这里是文档中写的bool should子句(https://www.elastic.co/guide/en/elasticsearch/reference/current/_executing_searches.html)
因此,我将 query2 设为与弹性查询相同的搜索方式。
var query2 = map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"should": map[string]interface{}{
"match": map[string]interface{}{
"year": "2016",
},
"match": map[string]interface{}{
"frequency": "7",
},
},
},
},
}
错误: map 文字中的重复键“匹配”
我对golang的接口(interface)不是很熟悉。
我希望有人对此有想法。谢谢。
附:
谢谢 Tranvu Xuannhat :) 它帮助了很多!
var query2 = map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"should": []map[string]interface{}{
map[string]interface{}{
"match": map[string]interface{}{
"year": "2016",
},
},
map[string]interface{}{
"match": map[string]interface{}{
"frequency": "7",
},
},
},
},
},
}
这就是我最后所做的。
最佳答案
我以前没有用 Golang 做过 ElasticSearch,但我发现你的原始查询和 Go 查询有些不匹配。它应该是 map 数组,每个 map 都有一个“匹配”键。不是具有重复“匹配”的 map (作为错误) 我认为在您的 Golang 查询中,它应该是:
"should":[]map[string]interface{} {
{
"match": map[string]interface{}{
"year": "2016",
},
},
{
"match": map[string]interface{}{
"frequency": "7",
},
},
},
关于elasticsearch - Golang : Using ElasticSearch library called Goes, 如何为 bool should 方法编写可执行代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40123002/