简体   繁体   中英

c# ElasticSearch NEST aggregation: group by

I have a problem with my ES NEST query. It 'make groups', but the item list is empty. I looked for examples and read what I found, but it's still empty in results.

This is my query:

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

Any idea? thnx

The issue is that your mapping is of type Text.

To make a terms aggregation work on a Text field you need to turn on fielddata for that field. https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#CO255-1

But what you probably really want is to use Keyword fields instead of Text, which automatically work with term aggregations. They are not analyzed by default. So the following change to your mappings should do the trick:

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

Now i should add that if Images is a list of strings, you do not need to make a Nested mapping. Lists of strings basically treated the same as individual strings in elasticsearch so you can probably do something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM