列表项上的 ElasticSearch 聚合

如何按列表项聚合(散列文本)


这是我的代码


public class Tweet

{

     public ulong id { get; set; }

     public string text { get; set; }

     [Nest.Nested]

     public List<hashtags> hashtags { get; set; }

}



public class hashtags

     public string hashtext { get; set; } 

}

我的索引数据示例如下:


 "hashtags" : [

        {

          "hashtext" : "aaaa"

        },

        {

          "hashtext" : "bbbb"

        },

        {

          "hashtext" : "ccccc"

        }

      ],

    }




.Aggregations(childAggs => childAggs

                .Nested("project_tags", n => n

                    .Path(p => p.hashtags)

                    .Aggregations(nestedAggs => nestedAggs

                        .Terms("by_tags", t => t

                            .Field(f => f.hashtags.First().hashtext)

                    )

                )

            ))

如何仅对 hashtext 属性进行聚合查询,例如我想获取所有带有 count 的 hashtext 文本


aaaa     3   times 

ccccc    5  times


红糖糍粑
浏览 108回答 1
1回答

米脂

只要将hashtextonhashtags映射为keyword数据类型(或映射为启用了 fielddatatext的数据类型,并附带执行此操作的警告),您所拥有的将起作用。这是一个工作示例private static void Main(){&nbsp; &nbsp; var defaultIndex = "my_index";&nbsp; &nbsp; var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));&nbsp; &nbsp; var settings = new ConnectionSettings(pool)&nbsp; &nbsp; &nbsp; &nbsp; .DefaultIndex(defaultIndex);&nbsp; &nbsp; var client = new ElasticClient(settings);&nbsp; &nbsp; if (client.IndexExists(defaultIndex).Exists)&nbsp; &nbsp; &nbsp; &nbsp; client.DeleteIndex(defaultIndex);&nbsp; &nbsp; var createIndexResponse = client.CreateIndex(defaultIndex, c => c&nbsp; &nbsp; &nbsp; &nbsp; .Mappings(m => m&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Map<Tweet>(mm => mm&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .AutoMap()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; );&nbsp; &nbsp; var bulkResponse = client.Bulk(b => b&nbsp; &nbsp; &nbsp; &nbsp; .IndexMany(new []&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Tweet&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id = 1,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = "foo",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hashtags = new List<hashtags>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new hashtags { hashtext = "aaaa" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new hashtags { hashtext = "bbbb" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new hashtags { hashtext = "cccc" }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Tweet&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id = 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = "bar",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hashtags = new List<hashtags>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new hashtags { hashtext = "aaaa" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new hashtags { hashtext = "bbbb" }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Tweet&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id = 3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = "baz",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hashtags = new List<hashtags>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new hashtags { hashtext = "aaaa" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new hashtags { hashtext = "cccc" }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .Refresh(Refresh.WaitFor)&nbsp; &nbsp; );&nbsp; &nbsp; var searchResponse = client.Search<Tweet>(s => s&nbsp; &nbsp; &nbsp; &nbsp; .Size(0)&nbsp; &nbsp; &nbsp; &nbsp; .Aggregations(childAggs => childAggs&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Nested("hashtags", n => n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Path(p => p.hashtags)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Aggregations(nestedAggs => nestedAggs&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Terms("by_tags", t => t&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Field(f => f.hashtags.First().hashtext)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; var hashtagBuckets = searchResponse.Aggregations.Nested("hashtags").Terms("by_tags").Buckets;&nbsp; &nbsp; foreach(var bucket in hashtagBuckets)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"{bucket.Key}\t{bucket.DocCount} times");&nbsp; &nbsp; }}public class Tweet{&nbsp; &nbsp; public ulong id { get; set; }&nbsp; &nbsp; public string text { get; set; }&nbsp; &nbsp; [Nested]&nbsp; &nbsp; public List<hashtags> hashtags { get; set; }}public class hashtags{&nbsp; &nbsp; [Keyword]&nbsp; &nbsp; public string hashtext { get; set; }}它将以下内容写入控制台aaaa&nbsp; 3 timesbbbb&nbsp; 2 timescccc&nbsp; 2 times
打开App,查看更多内容
随时随地看视频慕课网APP