dpjpo746884 2016-10-19 05:37
浏览 63
已采纳

Golang:使用名为Goes的ElasticSearch库,如何为bool应该方法编写可执行代码?

I'm using goes library (https://github.com/OwnLocal/goes), which is the wrapper for ElasticSearch in Golang.

In ElasticSearch queries, we can run like this:

{
  "query": {
    "match": {
      "user_id_1": "438018"
    }
  }
}

and it works.

For golang using goes, you can run it like as:

var query = map[string]interface{}{
        "query": map[string]interface{}{
            "match": map[string]interface{}{
                "user_id_1": "438018",
            },
        },
    }

Here is my question, you can use this query in elasticsearch

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "year": "2016"
          }
        },
        {
          "match": {
            "frequency": "7"
          }
        }
      ]
    }
  }
}

In the above code, it is the bool should clause written in documentation here (https://www.elastic.co/guide/en/elasticsearch/reference/current/_executing_searches.html)
So, I made query2 to search same way as elastic one.

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",
                    },
                },
            },
        },
    }

Error: duplicate key "match" in map literal

I am not so familiar with interface on golang.
I hope some has a idea for that. Thanks.

p.s.

Thanks Tranvu Xuannhat :) it helped a lot !

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",
                          },
                    },
                },
            },
        },
    }

This is what I did finally.

  • 写回答

1条回答 默认 最新

  • dqyym3667 2016-10-19 05:51
    关注

    I haven't done ElasticSearch with Golang before, but I see some mismatch between your original query and Go query. It should be array of map, each map has a key "match". Not a map with duplicate "match" (as the error) I think in your Golang query, it should be:

       "should":[]map[string]interface{} {
            {       
                    "match": map[string]interface{}{
                        "year": "2016",
                   },
            },
            {
                    "match": map[string]interface{}{
                        "frequency": "7",
                    },
            },
       },
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)