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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?