【问题标题】:how to convert elastic query aggregate filter into nest query in .Net core如何将弹性查询聚合过滤器转换为.Net核心中的嵌套查询
【发布时间】:2019-08-07 03:26:40
【问题描述】:

如何将此弹性搜索查询转换为嵌套查询。 查询给出了 Bellow 。 GET winlogbeat-6.6.0*/_search?size=0

{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "success ": {
      "filter": {
        "term": {
          "event_id": 4624 
        }
      }

    },
    "failed": {
      "filter": {
        "term": {
          "event_id": 4625 
        }
      }
    }
  }
}

在 Kibana 中想要的输出如下

    {
      "took" : 13120,
      "timed_out" : false,
      "_shards" : {
        "total" : 37,
        "successful" : 37,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 299924794,
        "max_score" : 0.0,
        "hits" : [ ]
      },
      "aggregations" : {
        "failed" : {
          "doc_count" : 351643
        },
        "success " : {
          "doc_count" : 40375274
        }
      }
    }

这是我的代码,我需要将其转换为 NEST 以获得所需的结果。 谢谢

【问题讨论】:

  • 你能分享一下你到目前为止所尝试的吗?
  • var queryWinlogbeat_6_6_0 = _elasticClient.Search(q => q .Index("winlogbeat-6.6.0*") .Size(0) .Aggregations(a => a .Filter("成功”, fa => fa .Filter(f => f.Term(o => o.event_id, 4624)) ) ) );此代码为我返回“成功”存储桶的正确记录,但如何对“失败”存储桶进行另一个过滤器
  • @NisarAhmad 您可以编辑问题以添加其他详细信息,例如您到目前为止所尝试的内容。这也可以让你格式化它

标签: elasticsearch asp.net-core nest


【解决方案1】:

你快到了,你只需要通过在聚合描述符上调用 .Filter(..) 来添加另一个案例

var searchResponse = await client.SearchAsync<Document>(s => s
    .Query(q => q.MatchAll())
    .Aggregations(a => a
        .Filter("success", success => success
            .Filter(filter => filter
                .Term(t => t.Field(f => f.EventId).Value(4624))))
        .Filter("failed", failed => failed
            .Filter(filter => filter
                .Term(t => t.Field(f => f.EventId).Value(4625))))));

public class Document
{
    public int Id { get; set; }
    public int EventId { get; set; }
}

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2022-08-09
    • 2020-11-09
    • 2020-01-29
    • 2020-09-05
    • 1970-01-01
    相关资源
    最近更新 更多