簡體   English   中英

c# ElasticSearch NEST 聚合:group by

[英]c# ElasticSearch NEST aggregation: group by

我的 ES NEST 查詢有問題。 它“創建組”,但項目列表為空。 我查找了示例並閱讀了我發現的內容,但結果仍然是空的。

這是我的查詢:

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;
        }
    }

任何的想法? 謝謝

問題是您的映射屬於文本類型。

要在文本字段上進行術語聚合,您需要為該字段打開 fielddata。 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#CO255-1

但是您可能真正想要的是使用關鍵字字段而不是文本,它會自動與術語聚合一起使用。 默認情況下不分析它們。 因此,對映射的以下更改應該可以解決問題:

    .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))

現在我應該補充一點,如果圖像是字符串列表,則不需要進行嵌套映射。 字符串列表基本上與 elasticsearch 中的單個字符串相同,因此您可以執行以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM