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",
                    },
            },
       },
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度