c#elasticsearch嵌套聚合:分组依据

k5ifujac  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(563)

我的es nest查询有问题。它是“make groups”,但项目列表为空。我找了一些例子,读了我的发现,但结果仍然是空的。
我的问题是:

public partial class ElasticSearchService
    {
        private const string groupBySubCategoryKey = "SubCategoryKey";

        public async Task SiteMap()
        {
            ISearchResponse<AdvertisementObjectEntityExtended> result = await Client.SearchAsync<AdvertisementObjectEntityExtended>(s => s
               .Index(ElasticClientFactorySettings.AdvertisementObjectIndex)
               .Aggregations(agr =>
                agr.Terms(groupBySubCategoryKey,
                    g => g.Field(f =>
                        f.SubCategoryKey))));

            var r = result.Aggregations.Terms(groupBySubCategoryKey);

} }

public static class ElasticClientFactory
    {
        public static async Task<ElasticClient> ClientAsync(ElasticSearchSettings settings)
        {
            Uri uri = new Uri($"{settings.EndPoint}");

            ConnectionSettings ConnectionSettings = new ConnectionSettings(uri)
                                                    .DefaultIndex(ElasticClientFactorySettings.AdvertisementObjectIndex)
                                                    .DefaultMappingFor<AdvertisementObjectEntityExtended>(i => i.IndexName(ElasticClientFactorySettings.AdvertisementObjectIndex))
                                                    .EnableHttpCompression()
                                                    .PrettyJson();

            ElasticClient client =  new ElasticClient(ConnectionSettings);

            CreateIndexResponse createIndexResponse = await client.Indices.CreateAsync(ElasticClientFactorySettings.AdvertisementObjectIndex, c => c
                .Map<AdvertisementObjectEntityExtended>(m => m
                    .AutoMap()
                    .Properties(p => p
                         .Text(t => t.Name(n => n.Id).Analyzer(AnalyzerSettings.No))
                         .Text(t => t.Name(n => n.UserUniq).Analyzer(AnalyzerSettings.NotAnalyzed))
                         .Text(t => t.Name(n => n.Uniq).Analyzer(AnalyzerSettings.NotAnalyzed))
                         .Text(t => t.Name(n => n.MainCategoryKey).Analyzer(AnalyzerSettings.NotAnalyzed))
                         .Text(t => t.Name(n => n.SubCategoryKey).Analyzer(AnalyzerSettings.NotAnalyzed))
                         .Nested<List<string>>(n => n.Name(nn => nn.Images)
                        )
                    )
                )
            );

            return client;
        }
    }

public partial class ElasticSearchService : IElasticSearchService
    {
          public  ElasticSearchService(IOptions<ElasticSearchSettings> settings)
        {
            Client = ElasticClientFactory.ClientAsync(settings.Value).Result;
        }
    }

你知道吗?thnx公司

vdgimpew

vdgimpew1#

问题是您的Map类型是text。
要使术语聚合在文本字段上工作,需要为该字段启用fielddata。https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#co255-1
但您可能真正想要的是使用关键字字段而不是文本,这会自动处理术语聚合。默认情况下不会对其进行分析。因此,对Map所做的以下更改应该可以做到这一点:

.Keyword(t => t.Name(n => n.Id))
    .Keyword(t => t.Name(n => n.UserUniq))
    .Keyword(t => t.Name(n => n.Uniq))
    .Keyword(t => t.Name(n => n.MainCategoryKey))
    .Keyword(t => t.Name(n => n.SubCategoryKey))

现在我要补充的是,如果images是一个字符串列表,则不需要进行嵌套Map。字符串列表基本上与elasticsearch中的单个字符串相同,因此您可以执行以下操作:

.Keyword(t => t.Name(n => nn.Images))

相关问题