elasticsearch - Golang : Using ElasticSearch library called Goes, 如何为 bool should 方法编写可执行代码?

标签 elasticsearch go

我正在使用 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/

相关文章:

elasticsearch - 如何在Elasticsearch中使用短语提示器?

elasticsearch - 对所有数组元素的elasticsearch查询

go - 在同一个共享 channel 上并发选择

http - 为什么端口是字符串而不是整数?

go - 如果我们无法从传递给该例程的 channel 收听,如何停止 goroutine

elasticsearch - 从经典的ASP调用elasticsearch可以进行简单的搜索,但是无法使复杂的搜索起作用

search - 如何使用elasticsearch搜索产品

java - 如何在 ElasticSearch 中设置高斯衰减函数的下限?

Go-Gin 多次读取请求体

go - 在 Go HTTP 处理程序中使用未导出的结构键获取上下文值时为 nil