登录
首页 >  Golang >  Go问答

使用Golang构建弹性查询

来源:stackoverflow

时间:2024-02-10 19:21:24 169浏览 收藏

今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《使用Golang构建弹性查询》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!

问题内容

我是 goleng 和 elastic 的新手。 我有一个从 kibana 中取出的静态查询,现在我想使用这个查询根据客户将发送的参数构建动态查询代码

我如何根据用户参数添加或删除“event_state.status”。 我如何将此 json 转换为 map[string]interface {}. 我使用的客户端包是 https://github.com/elastic/go-elasticsearch

这是 json 查询:

{
  "sort": [
    {
      "@timestamp": {
        "order": "desc",
        "unmapped_type": "boolean"
      }
    }
  ],
  "from": 0,
  "size": 10000,
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "filter": [
              {
                "bool": {
                  "should": [
                    {
                      "match_phrase": {
                        "webhook_id": "12345"
                      }
                    }
                  ],
                  "minimum_should_match": 1
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "bool": {
                        "should": [
                          {
                            "match": {
                              "event_state.status": "rejected"
                            }
                          }
                        ],
                        "minimum_should_match": 1
                      }
                    },
                    {
                      "bool": {
                        "should": [
                          {
                            "match": {
                              "event_state.status": "succeeded"
                            }
                          }
                        ],
                        "minimum_should_match": 1
                      }
                    },
                    {
                      "bool": {
                        "should": [
                          {
                            "match": {
                              "event_state.status": "expired"
                            }
                          }
                        ],
                        "minimum_should_match": 1
                      }
                    },
                    {
                      "bool": {
                        "should": [
                          {
                            "match": {
                              "event_state.status": "inactive"
                            }
                          }
                        ],
                        "minimum_should_match": 1
                      }
                    }
                  ],
                  "minimum_should_match": 1
                }
              }
            ]
          }
        },
        {
          "range": {
            "created_at": {
              "format": "strict_date_optional_time",
              "gte": "2022-02-16T03:19:50.267Z",
              "lte": "2022-02-17T03:19:50.267Z"
            }
          }
        }
      ]
    }
  },
  "collapse": {
    "field": "uuid.keyword",
    "inner_hits": {
      "name": "order by attempt_count",
      "size": 1,
      "sort": [
        {
          "event_state.attempt_count": "desc"
        }
      ]
    }
  }
}

正确答案


您可以将查询字符串存储在原始字符串常量中,需要替换的部分可以使用格式化指令,例如 %s、%d、%v。

将原始字符串常量连同变量以正确的顺序传递给 fmt.Sprintf。

终于介绍完啦!小伙伴们,这篇关于《使用Golang构建弹性查询》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>