c#嵌套过滤器以获取日期范围内或空值

hec6srdp  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(1)|浏览(427)

我正在开发一个能显示用户年龄的饼图的东西,把用户分成两部分
0-17
18-34
35-44
44-54
55+
无法使用的
在这里,我得到了基于日期范围的年龄;

var aggaResonse = client.Search<JSModel>(a => a
                  .Size(0)
                  .Query(q => q.Bool(b => b.Must(m => m
                                                            .DateRange(date => date
                                                            .Field(p => p.CreatedDate)
                                                            .GreaterThanOrEquals(start)
                                                            .LessThanOrEquals(end)),

                                                        m =>
                                                            m.Term(t => t.Field(f => f.StepType.Suffix("keyword")).Value("User"))

                                                      )
                  ))

                  .Aggregations(c => c.DateRange(nameof(AgeModel), x => x
                                                       .Field(f => f.BirthDate)
                                                       .Ranges(r => r.From(DateMath.Now.Subtract("17y")).To(DateMath.Now).Key(nameof(result.Years_0_17)),
                                                               r => r.From(DateMath.Now.Subtract("34y")).To(DateMath.Now.Subtract("18y")).Key(nameof(result.Years_18_34)),
                                                               r => r.From(DateMath.Now.Subtract("44y")).To(DateMath.Now.Subtract("35y")).Key(nameof(result.Years_35_44)),
                                                               r => r.From(DateMath.Now.Subtract("54y")).To(DateMath.Now.Subtract("45y")).Key(nameof(result.Years_45_54)),
                                                               r => r.From(DateMath.Now.Subtract("120y")).To(DateMath.Now.Subtract("55y")).Key(nameof(result.Years_55_Plus))

                                                               )

                                                 )
                        ));

            if (!aggaResonse.IsValid)
                return result;

            result.Years_0_17 = aggaResonse.Aggregations.Range(nameof(AgeModel)).Buckets.Single(c => c.Key == nameof(result.Years_0_17)).DocCount;
            result.Years_18_34 = aggaResonse.Aggregations.Range(nameof(AgeModel)).Buckets.Single(c => c.Key == nameof(result.Years_18_34)).DocCount;
            result.Years_35_44 = aggaResonse.Aggregations.Range(nameof(AgeModel)).Buckets.Single(c => c.Key == nameof(result.Years_35_44)).DocCount;
            result.Years_45_54 = aggaResonse.Aggregations.Range(nameof(AgeModel)).Buckets.Single(c => c.Key == nameof(result.Years_45_54)).DocCount;
            result.Years_55_Plus = aggaResonse.Aggregations.Range(nameof(AgeModel)).Buckets.Single(c => c.Key == nameof(result.Years_55_Plus)).DocCount;

            return result;

我需要的是有一个“不可用”的用户谁的出生日期Map为空切片;

result.Not_Available = ....;

对嵌套嵌套agg的以下最佳实践有何建议?
我想再做一次搜索,我想这不是最好的做法。

vom3gejh

vom3gejh1#

在挖掘了太多的文档之后,下面是我写的解决方案;
我在当前聚合中添加了一个“missing”属性;

&& c.Missing("DOBMissing", x => x.Field(f => f.BirthDate))

所以它变得像;

.Aggregations(c => c.DateRange(nameof(AgeModel), x => x
                                                       .Field(f => f.BirthDate)
                                                       .Ranges(r => r.From(DateMath.Now.Subtract("17y")).To(DateMath.Now).Key(nameof(result.Years_0_17)),
                                                               r => r.From(DateMath.Now.Subtract("34y")).To(DateMath.Now.Subtract("18y")).Key(nameof(result.Years_18_34)),
                                                               r => r.From(DateMath.Now.Subtract("44y")).To(DateMath.Now.Subtract("35y")).Key(nameof(result.Years_35_44)),
                                                               r => r.From(DateMath.Now.Subtract("54y")).To(DateMath.Now.Subtract("45y")).Key(nameof(result.Years_45_54)),
                                                               r => r.From(DateMath.Now.Subtract("120y")).To(DateMath.Now.Subtract("55y")).Key(nameof(result.Years_55_Plus))
                                                               )
                                                 ) &&
                                c.Missing("DOBMissing", x => x.Field(f => f.BirthDate))
            )

我访问了聚合的“缺失”部分,如下所示;

result.Not_Available = aggaResponse.Aggregations.Missing("DOBMissing").DocCount;

相关问题