【问题标题】:NEST Filter for ElasticSearch用于 ElasticSearch 的 NEST 过滤器
【发布时间】:2023-03-15 22:05:01
【问题描述】:

我正在使用 NEST 查询 ElasticSearch,并且正在寻找创建条件过滤器。我需要查询匹配类别以及可选的标题和章节字段。我正在汇总以获取唯一值。问题是过滤器似乎在过滤器字段上执行 OR。任何想法我在这个例子中做错了什么?

FilterContainer filter = new FilterContainer();

filter = Filter<Page>.Term("category", "1575");

if (title != null)
{
    filter &= Filter<Page>.Term("title", title);
}

if (chapter != null)
{
    filter &= Filter<Page>.Term("chapter", chapter);
}

var result = client.Search<Page>(s => s
    .Index(index)
    .Filter(filter)
    .Size(0)
    .Aggregations(a => a
        .Terms("my_agg", st => st
            .Field("title")
        )
    )
);

var myAgg = result.Aggs.Terms("my_agg");
IList<KeyItem> lst = myAgg.Items;
return lst;

【问题讨论】:

    标签: elasticsearch aggregation nest


    【解决方案1】:

    这将为您提供所有行的结果,因为您的过滤器根本不会用于聚合。因此,过滤器似乎正在执行 OR 操作。根据您的要求,您需要使用过滤器聚合,其中在聚合内指定过滤器。请查看http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/search-aggregations-bucket-filter-aggregation.html 了解更多信息。

    出于您的目的,以下是用于创建搜索请求和处理响应的更新代码。

    var result = client.Search<Page>(s => s
        .Index(index)
        .Size(0)
        .Aggregations(fa => fa
            .Filter("filtered_aggs", f => f
                .Filter(fd => filter)
                    .Aggregations(ta => ta
                        .Terms("my_agg", st => st
                            .Field("title")
                        )
                    )
                )
            )
        )
    );
    
    var myAgg = result.Aggs.Nested("filtered_aggs").Terms("my_agg");
    IList<KeyItem> lst = myAgg.Items;
    return lst;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-06
      • 2016-10-26
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 2016-10-11
      • 1970-01-01
      相关资源
      最近更新 更多