doudui1850 2018-04-20 13:06
浏览 1682

GoLang,elasticsearch多个查询参数

I have a project in golang which fetches logs from elastiearch where our servers send logs. I have a problem with multiple query. I want to query by two fields, where both must be found and filter result to get all these logs between two timestamps. In the code below attaching deploymentName object into Filter()causes for no results at all.

import  ("github.com/olivere/elastic")


func main() {

client, err := elastic.NewClient(elastic.SetURL(*elasticUrl))


   //query by time
    timeQ := elastic.NewRangeQuery("@timestamp").From(from).To(to)

   //query by key named "component"
    componentQ := elastic.NewMatchQuery("component", *component)

    //query by key named "deploymentName", sam type as component
    deploymentQ := elastic.NewMatchQuery("deploymentName", deploymentName)

    //query object used for actual search later
    generalQ := elastic.NewBoolQuery().Should().
       Filter(timeQ).Filter(componentQ).Filter(deploymentQ)

        searchResult, err := client.Search().
                Index("some-index").
                From(from).Size(*chunk).
                Query(generalQ).
                Sort("@timestamp", true).
                Do(context.Background())
}

While if generalQuery is written like this, it works.

        //query object used for actual search later
    generalQ := elastic.NewBoolQuery().Should().
       Filter(timeQ).Filter(componentQ)
  • 写回答

1条回答 默认 最新

  • douxingti9307 2018-10-08 17:13
    关注

    I am currently new with Elasticsearch, but this way actually works and return the specified components and deployments in a given time range.

    timeQ := elastic.NewRangeQuery("@timestamp").From(from).To(End)
    componentQ := elastic.NewTermQuery("component", *component)
    deploymentQ := elastic.NewTermQuery("deploymentName", deploymentName)
    
    generalQ := elastic.NewBoolQuery()
    generalQ = generalQ.Must(timeQ).Must(componentQ).Must(deploymentQ)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?