Elasticsearch(ElasticClient嵌套)统计过滤后的数据

Count filtered data in Elasticsearch(ElasticClient nest)

我正在使用 ElasticClient 2.4.4。

我有一个问题

var sample = client.Search<TestMenuItem>(s => s
            .Query(q =>
                q.Term(p => p.RegionSegments, "s1")
                && q.Term(p => p.RegionSegments, "s2")
                 && q.Term(p => p.RegionSegments, "s3")
            ));

比我想计算 RegionSegments = s1 有多少项目。 据我所知,我应该使用聚合但找不到很好的例子如何解决这个 issue.Any 想法?

如果您只需要对 s1s2s3 RegionSegment 文档中的每一个进行计数,那么您可以对所有

执行 multi_search for this, specifying a count search_type ]
var multiSearchResponse = client.MultiSearch(ms => ms
    // this is the default index for multi_search. Can override
    // on each Search call if needed
    .Index<TestMenuItem>()
    // this is the default type for mulit_search. Can override
    // on each search call if needed
    .Type<TestMenuItem>()
    // this is the default search_type. Can override on
    // each search call if needed
    .SearchType(SearchType.Count)
    .Search<TestMenuItem>("s1", s => s
        .Query(q => +q.Term(p => p.RegionSegments, "s1"))
    )
    .Search<TestMenuItem>("s2", s => s
        .Query(q => +q.Term(p => p.RegionSegments, "s2"))
    )
    .Search<TestMenuItem>("s3", s => s
        .Query(q => +q.Term(p => p.RegionSegments, "s3"))
    )
);

这会产生以下请求

POST http://localhost:9200/testmenuitem-index/testmenuitem/_msearch?search_type=count 
{}
{"query":{"bool":{"filter":[{"term":{"regionSegments":{"value":"s1"}}}]}}}
{}
{"query":{"bool":{"filter":[{"term":{"regionSegments":{"value":"s2"}}}]}}}
{}
{"query":{"bool":{"filter":[{"term":{"regionSegments":{"value":"s3"}}}]}}}

获取每个搜索结果的总数类似于以下内容

var s1Total = multiSearchResponse.GetResponse<TestMenuItem>("s1").Total;